Skip to content

Commit

Permalink
cleanup code and prevent getting flagged on multiplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
btwonion committed Mar 28, 2024
1 parent 3e2f915 commit c890274
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
43 changes: 22 additions & 21 deletions src/main/java/dev/nyon/bbm/asm/BoatMixin.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package dev.nyon.bbm.asm;

import dev.nyon.bbm.config.Config;
import dev.nyon.bbm.config.ConfigKt;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.vehicle.Boat;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -20,29 +21,22 @@ class BoatMixin {
@Unique
private int wallHitCooldown;

@Inject(
method = "<init>(Lnet/minecraft/world/entity/EntityType;Lnet/minecraft/world/level/Level;)V",
at = @At("RETURN")
)
private void initStepHeight(
EntityType<? extends Boat> entityType,
Level level,
CallbackInfo ci
) {
instance.setMaxUpStep(ConfigKt.config.getStepHeight());
}

@Inject(
method = "tick",
at = @At("HEAD")
)
private void checkWall(CallbackInfo ci) {
Config config = ConfigKt.getActiveConfig();
if (config == null) {
instance.setMaxUpStep(0);
return;
}

if (failsPlayerCondition()) return;
if (wallHitCooldown > 0) wallHitCooldown--;
else if (instance.horizontalCollision) {
wallHitCooldown = ConfigKt.config.getWallHitCooldownTicks();
instance.setDeltaMovement(instance.getDeltaMovement()
.add(0, ConfigKt.config.getStepHeight(), 0));
wallHitCooldown = config.getWallHitCooldownTicks();
instance.addDeltaMovement(new Vec3(0, config.getStepHeight(), 0));
}
}

Expand All @@ -52,10 +46,12 @@ else if (instance.horizontalCollision) {
)
private void boostUnderwater(CallbackInfo ci) {
if (!instance.isUnderWater()) return;
Config config = ConfigKt.getActiveConfig();
if (config == null) return;

if (failsPlayerCondition()) return;
if (!ConfigKt.config.getBoostUnderwater()) return;
instance.setDeltaMovement(instance.getDeltaMovement()
.add(0, ConfigKt.config.getStepHeight() / 2, 0));
if (!config.getBoostUnderwater()) return;
instance.addDeltaMovement(new Vec3(0, config.getStepHeight() / 2, 0));
}

@ModifyConstant(
Expand All @@ -66,12 +62,17 @@ private void boostUnderwater(CallbackInfo ci) {
)
)
private float changeEjectTime(float constant) {
return ConfigKt.config.getPlayerEjectTicks();
Config config = ConfigKt.getActiveConfig();
if (config == null) return constant;
return config.getPlayerEjectTicks();
}

@Unique
private boolean failsPlayerCondition() {
if (!ConfigKt.config.getOnlyForPlayers()) return false;
Config config = ConfigKt.getActiveConfig();
if (config == null) return true;

if (!config.getOnlyForPlayers()) return false;
return instance.getPassengers()
.stream()
.noneMatch(entity -> entity.getType() == EntityType.PLAYER);
Expand Down
9 changes: 8 additions & 1 deletion src/main/kotlin/dev/nyon/bbm/BetterBoatMovement.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package dev.nyon.bbm

import dev.nyon.bbm.config.Config
import dev.nyon.bbm.config.serverConfig
import dev.nyon.konfig.config.config
import dev.nyon.konfig.config.loadConfig
import net.fabricmc.api.EnvType
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking
Expand All @@ -18,10 +20,15 @@ object BetterBoatMovement : ModInitializer {
when (FabricLoader.getInstance().environmentType) {
EnvType.CLIENT -> {
ClientPlayNetworking.registerGlobalReceiver(Config.packetType.id) { _, _, buf, _ ->
internalConfig = Config.packetType.read(buf)
serverConfig = Config.packetType.read(buf)
}

ClientPlayConnectionEvents.DISCONNECT.register { _, _ ->
serverConfig = null
}
}
EnvType.SERVER -> {
serverConfig = internalConfig
ServerPlayConnectionEvents.INIT.register { handler, _ ->
ServerPlayNetworking.send(handler.player, internalConfig)
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/dev/nyon/bbm/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import net.fabricmc.fabric.api.networking.v1.FabricPacket
import net.fabricmc.fabric.api.networking.v1.PacketType
import net.minecraft.client.Minecraft
import net.minecraft.network.FriendlyByteBuf
import net.minecraft.resources.ResourceLocation

lateinit var config: Config
var serverConfig: Config? = null

@Serializable
data class Config(
Expand Down Expand Up @@ -39,3 +41,8 @@ data class Config(
return packetType
}
}

fun getActiveConfig(): Config? {
if (Minecraft.getInstance().isSingleplayer) return config
return serverConfig
}

0 comments on commit c890274

Please sign in to comment.