From fa2322aa064545ef6e71ca4edd6d6a36d62ef6f7 Mon Sep 17 00:00:00 2001 From: Saravanan S Date: Sat, 27 Dec 2025 23:32:12 +0530 Subject: [PATCH 1/4] Added Create:TFMG Engine Controller Support --- build.gradle | 2 + .../stratodonut/drivebywire/WireBlocks.java | 8 ++ .../drivebywire/WireCreativeTabs.java | 4 + .../blocks/TFMGEngineControllerHubBlock.java | 90 +++++++++++++++++++ ...TFMGEngineControllerWireServerHandler.java | 54 +++++++++++ .../tfmg/MixinTFMGEngineControllerBlock.java | 48 ++++++++++ .../MixinTFMGEngineControllerBlockEntity.java | 66 ++++++++++++++ .../mixinducks/TFMGControllerDuck.java | 4 + .../tfmg_engine_controller_hub.json | 5 ++ .../assets/drivebywire/lang/en_us.json | 2 + .../item/tfmg_engine_controller_hub.json | 3 + src/main/resources/drivebywire.mixins.json | 4 +- 12 files changed, 289 insertions(+), 1 deletion(-) create mode 100644 src/main/java/edn/stratodonut/drivebywire/blocks/TFMGEngineControllerHubBlock.java create mode 100644 src/main/java/edn/stratodonut/drivebywire/compat/TFMGEngineControllerWireServerHandler.java create mode 100644 src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlock.java create mode 100644 src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlockEntity.java create mode 100644 src/main/java/edn/stratodonut/drivebywire/mixinducks/TFMGControllerDuck.java create mode 100644 src/main/resources/assets/drivebywire/blockstates/tfmg_engine_controller_hub.json create mode 100644 src/main/resources/assets/drivebywire/models/item/tfmg_engine_controller_hub.json diff --git a/build.gradle b/build.gradle index 4a4a104..cf7ae44 100644 --- a/build.gradle +++ b/build.gradle @@ -227,6 +227,8 @@ dependencies { implementation fg.deobf("maven.modrinth:create-tweaked-controllers:1.20.1-1.2.5") + implementation fg.deobf("maven.modrinth:create-tfmg:1.0.2f-forge") + //runtimeOnly fg.deobf("maven.modrinth:eureka:9gbnRz82") runtimeOnly fg.deobf("maven.modrinth:vlib:1.20.1-0.1.0+forge") runtimeOnly fg.deobf("org.valkyrienskies:clockwork-forge:1.20.1-0.4.0-forge-a764c6701b") {transitive = false } diff --git a/src/main/java/edn/stratodonut/drivebywire/WireBlocks.java b/src/main/java/edn/stratodonut/drivebywire/WireBlocks.java index 7d3f227..f4f2e92 100644 --- a/src/main/java/edn/stratodonut/drivebywire/WireBlocks.java +++ b/src/main/java/edn/stratodonut/drivebywire/WireBlocks.java @@ -6,6 +6,7 @@ import com.simibubi.create.foundation.data.SharedProperties; import com.tterrag.registrate.util.entry.BlockEntry; import edn.stratodonut.drivebywire.blocks.ControllerHubBlock; +import edn.stratodonut.drivebywire.blocks.TFMGEngineControllerHubBlock; import edn.stratodonut.drivebywire.blocks.TweakedControllerHubBlock; import edn.stratodonut.drivebywire.blocks.WireNetworkBackupBlock; import net.minecraft.resources.ResourceLocation; @@ -40,5 +41,12 @@ public class WireBlocks { .simpleItem() .register(); + public static final BlockEntry TFMG_ENGINE_CONTROLLER_HUB = + REGISTRATE.block("tfmg_engine_controller_hub", TFMGEngineControllerHubBlock::new) + .initialProperties(SharedProperties::copperMetal) + .transform(axeOrPickaxe()) + .simpleItem() + .register(); + public static void register() {} } diff --git a/src/main/java/edn/stratodonut/drivebywire/WireCreativeTabs.java b/src/main/java/edn/stratodonut/drivebywire/WireCreativeTabs.java index ab0f49d..95de230 100644 --- a/src/main/java/edn/stratodonut/drivebywire/WireCreativeTabs.java +++ b/src/main/java/edn/stratodonut/drivebywire/WireCreativeTabs.java @@ -1,6 +1,7 @@ package edn.stratodonut.drivebywire; import com.tterrag.registrate.util.entry.RegistryEntry; +import edn.stratodonut.drivebywire.blocks.TFMGEngineControllerHubBlock; import edn.stratodonut.drivebywire.blocks.TweakedControllerHubBlock; import net.minecraft.core.registries.Registries; import net.minecraft.network.chat.Component; @@ -39,6 +40,9 @@ public static boolean include(Object thing) { if (!ModList.get().isLoaded("create_tweaked_controllers")) { if (thing instanceof TweakedControllerHubBlock) return false; } + if (!ModList.get().isLoaded("tfmg")) { + if (thing instanceof TFMGEngineControllerHubBlock) return false; + } return true; } diff --git a/src/main/java/edn/stratodonut/drivebywire/blocks/TFMGEngineControllerHubBlock.java b/src/main/java/edn/stratodonut/drivebywire/blocks/TFMGEngineControllerHubBlock.java new file mode 100644 index 0000000..0827a83 --- /dev/null +++ b/src/main/java/edn/stratodonut/drivebywire/blocks/TFMGEngineControllerHubBlock.java @@ -0,0 +1,90 @@ +package edn.stratodonut.drivebywire.blocks; + +import edn.stratodonut.drivebywire.WireSounds; +import edn.stratodonut.drivebywire.compat.TFMGEngineControllerWireServerHandler; +import edn.stratodonut.drivebywire.mixinducks.TFMGControllerDuck; +import edn.stratodonut.drivebywire.util.HubItem; +import edn.stratodonut.drivebywire.wire.MultiChannelWireSource; +import net.minecraft.core.BlockPos; +import net.minecraft.network.chat.Component; +import net.minecraft.sounds.SoundSource; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.BlockGetter; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.phys.BlockHitResult; +import net.minecraft.world.phys.shapes.CollisionContext; +import net.minecraft.world.phys.shapes.VoxelShape; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public class TFMGEngineControllerHubBlock extends Block implements MultiChannelWireSource{ + private static final List channels = List.of( + TFMGEngineControllerWireServerHandler.STEER_LEFT, + TFMGEngineControllerWireServerHandler.STEER_RIGHT, + + TFMGEngineControllerWireServerHandler.SHIFT_REVERSE, + TFMGEngineControllerWireServerHandler.SHIFT_NEUTRAL, + TFMGEngineControllerWireServerHandler.SHIFT_1, + TFMGEngineControllerWireServerHandler.SHIFT_2, + TFMGEngineControllerWireServerHandler.SHIFT_3, + TFMGEngineControllerWireServerHandler.SHIFT_4, + TFMGEngineControllerWireServerHandler.SHIFT_5, + TFMGEngineControllerWireServerHandler.SHIFT_6, + + TFMGEngineControllerWireServerHandler.PEDAL_CLUTCH, + TFMGEngineControllerWireServerHandler.PEDAL_BRAKE, + TFMGEngineControllerWireServerHandler.PEDAL_GAS + ); + + public TFMGEngineControllerHubBlock(Properties props) { + super(props); + } + + @Override + public @NotNull InteractionResult use( + @NotNull BlockState state, + @NotNull Level level, + @NotNull BlockPos pos, + @NotNull Player player, + @NotNull InteractionHand hand, + @NotNull BlockHitResult hit + ) { + ItemStack stack = player.getItemInHand(hand); + + if (stack.getItem() instanceof TFMGControllerDuck) { + HubItem.putHub(stack, pos); + if (!level.isClientSide) { + level.playSound(null, pos, WireSounds.PLUG_IN.get(), SoundSource.BLOCKS, 1, 1); + player.displayClientMessage( + Component.literal("TFMG Controller connected!"), true + ); + } + return InteractionResult.SUCCESS; + } + + return super.use(state, level, pos, player, hand, hit); + } + + @Override + public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext ctx) { + return ControllerHubBlock.BOTTOM_AABB; + } + + @Override + public List wire$getChannels() { + return channels; + } + + @Override + public @NotNull String wire$nextChannel(String current, boolean forward) { + int idx = channels.indexOf(current); + if (idx == -1) return channels.get(0); + return channels.get(Math.floorMod(idx + (forward ? 1 : -1), channels.size())); + } +} diff --git a/src/main/java/edn/stratodonut/drivebywire/compat/TFMGEngineControllerWireServerHandler.java b/src/main/java/edn/stratodonut/drivebywire/compat/TFMGEngineControllerWireServerHandler.java new file mode 100644 index 0000000..fd47be9 --- /dev/null +++ b/src/main/java/edn/stratodonut/drivebywire/compat/TFMGEngineControllerWireServerHandler.java @@ -0,0 +1,54 @@ +package edn.stratodonut.drivebywire.compat; + +import edn.stratodonut.drivebywire.wire.ShipWireNetworkManager; +import net.minecraft.core.BlockPos; +import net.minecraft.world.level.Level; + +public class TFMGEngineControllerWireServerHandler { + + public static final String STEER_LEFT = "tfmg_steer_left"; + public static final String STEER_RIGHT = "tfmg_steer_right"; + + public static final String ENGINE_STARTED = "tfmg_engine_started"; + + public static final String SHIFT_REVERSE = "tfmg_shift_reverse"; + public static final String SHIFT_NEUTRAL = "tfmg_shift_neutral"; + public static final String SHIFT_1 = "tfmg_shift_1"; + public static final String SHIFT_2 = "tfmg_shift_2"; + public static final String SHIFT_3 = "tfmg_shift_3"; + public static final String SHIFT_4 = "tfmg_shift_4"; + public static final String SHIFT_5 = "tfmg_shift_5"; + public static final String SHIFT_6 = "tfmg_shift_6"; + + public static final String PEDAL_CLUTCH = "tfmg_pedal_clutch"; + public static final String PEDAL_BRAKE = "tfmg_pedal_brake"; + public static final String PEDAL_GAS = "tfmg_pedal_gas"; + + public static void set(Level level, BlockPos pos, String channel, boolean active) { + ShipWireNetworkManager.trySetSignalAt( + level, + pos, + channel, + active ? 15 : 0 + ); + } + + public static void reset(Level level, BlockPos pos) { + set(level, pos, STEER_LEFT, false); + set(level, pos, STEER_RIGHT, false); + + set(level, pos, SHIFT_REVERSE, false); + set(level, pos, SHIFT_NEUTRAL, false); + set(level, pos, SHIFT_1, false); + set(level, pos, SHIFT_2, false); + set(level, pos, SHIFT_3, false); + set(level, pos, SHIFT_4, false); + set(level, pos, SHIFT_5, false); + set(level, pos, SHIFT_6, false); + + set(level, pos, PEDAL_CLUTCH, false); + set(level, pos, PEDAL_BRAKE, false); + set(level, pos, PEDAL_GAS, false); + } + +} diff --git a/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlock.java b/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlock.java new file mode 100644 index 0000000..af1290e --- /dev/null +++ b/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlock.java @@ -0,0 +1,48 @@ +package edn.stratodonut.drivebywire.mixin.compat.tfmg; + +import com.drmangotea.tfmg.content.engines.engine_controller.EngineControllerBlock; +import edn.stratodonut.drivebywire.compat.TFMGEngineControllerWireServerHandler; +import edn.stratodonut.drivebywire.wire.MultiChannelWireSource; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Pseudo; +import org.spongepowered.asm.mixin.Unique; +import javax.annotation.Nonnull; +import java.util.List; + +@Pseudo +@Mixin(EngineControllerBlock.class) +public abstract class MixinTFMGEngineControllerBlock implements MultiChannelWireSource { + + @Unique + private static final List wire$channels = List.of( + TFMGEngineControllerWireServerHandler.STEER_LEFT, + TFMGEngineControllerWireServerHandler.STEER_RIGHT, + + TFMGEngineControllerWireServerHandler.SHIFT_REVERSE, + TFMGEngineControllerWireServerHandler.SHIFT_NEUTRAL, + TFMGEngineControllerWireServerHandler.SHIFT_1, + TFMGEngineControllerWireServerHandler.SHIFT_2, + TFMGEngineControllerWireServerHandler.SHIFT_3, + TFMGEngineControllerWireServerHandler.SHIFT_4, + TFMGEngineControllerWireServerHandler.SHIFT_5, + TFMGEngineControllerWireServerHandler.SHIFT_6, + + TFMGEngineControllerWireServerHandler.PEDAL_CLUTCH, + TFMGEngineControllerWireServerHandler.PEDAL_BRAKE, + TFMGEngineControllerWireServerHandler.PEDAL_GAS + ); + + @Override + public List wire$getChannels() { + return wire$channels; + } + + @Override + public @Nonnull String wire$nextChannel(String current, boolean forward) { + int i = wire$channels.indexOf(current); + if (i == -1) return wire$channels.get(0); + return wire$channels.get( + Math.floorMod(i + (forward ? 1 : -1), wire$channels.size()) + ); + } +} diff --git a/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlockEntity.java b/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlockEntity.java new file mode 100644 index 0000000..44c67e9 --- /dev/null +++ b/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlockEntity.java @@ -0,0 +1,66 @@ +package edn.stratodonut.drivebywire.mixin.compat.tfmg; + +import com.drmangotea.tfmg.content.engines.engine_controller.EngineControllerBlockEntity; +import com.drmangotea.tfmg.content.engines.upgrades.TransmissionUpgrade.TransmissionState; +import edn.stratodonut.drivebywire.compat.TFMGEngineControllerWireServerHandler; +import net.minecraft.world.level.Level; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Pseudo; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Pseudo +@Mixin(EngineControllerBlockEntity.class) +public abstract class MixinTFMGEngineControllerBlockEntity { + + @Inject(method = "tick", at = @At("TAIL"), remap = false) + private void drivebywire$emitSignals(CallbackInfo ci) { + EngineControllerBlockEntity self = (EngineControllerBlockEntity)(Object)this; + Level level = self.getLevel(); + if (level == null || level.isClientSide) return; + + var pos = self.getBlockPos(); + + boolean steerLeft = self.isPressed(2); + boolean steerRight = self.isPressed(3); + + boolean shiftReverse = self.shift == TransmissionState.REVERSE; + boolean shiftNeutral = self.shift == TransmissionState.NEUTRAL; + boolean shift1 = self.shift == TransmissionState.SHIFT_1; + boolean shift2 = self.shift == TransmissionState.SHIFT_2; + boolean shift3 = self.shift == TransmissionState.SHIFT_3; + boolean shift4 = self.shift == TransmissionState.SHIFT_4; + boolean shift5 = self.shift == TransmissionState.SHIFT_5; + boolean shift6 = self.shift == TransmissionState.SHIFT_6; + + boolean pedalClutch = self.clutch; + boolean pedalBrake = self.brake; + boolean pedalGas = self.gas; + + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.STEER_LEFT, steerLeft); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.STEER_RIGHT, steerRight); + + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_REVERSE, shiftReverse); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_NEUTRAL, shiftNeutral); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_1, shift1); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_2, shift2); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_3, shift3); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_4, shift4); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_5, shift5); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_6, shift6); + + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.PEDAL_CLUTCH, pedalClutch); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.PEDAL_BRAKE, pedalBrake); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.PEDAL_GAS, pedalGas); + } + + @Inject(method = "remove", at = @At("HEAD"), remap = false) + private void drivebywire$reset(CallbackInfo ci) { + EngineControllerBlockEntity self = (EngineControllerBlockEntity)(Object)this; + Level level = self.getLevel(); + if (level != null && !level.isClientSide) { + TFMGEngineControllerWireServerHandler.reset(level, self.getBlockPos()); + } + } +} diff --git a/src/main/java/edn/stratodonut/drivebywire/mixinducks/TFMGControllerDuck.java b/src/main/java/edn/stratodonut/drivebywire/mixinducks/TFMGControllerDuck.java new file mode 100644 index 0000000..3e64adc --- /dev/null +++ b/src/main/java/edn/stratodonut/drivebywire/mixinducks/TFMGControllerDuck.java @@ -0,0 +1,4 @@ +package edn.stratodonut.drivebywire.mixinducks; + +public interface TFMGControllerDuck { +} diff --git a/src/main/resources/assets/drivebywire/blockstates/tfmg_engine_controller_hub.json b/src/main/resources/assets/drivebywire/blockstates/tfmg_engine_controller_hub.json new file mode 100644 index 0000000..9a99693 --- /dev/null +++ b/src/main/resources/assets/drivebywire/blockstates/tfmg_engine_controller_hub.json @@ -0,0 +1,5 @@ +{ + "variants": { + "": { "model": "drivebywire:block/hub" } + } +} \ No newline at end of file diff --git a/src/main/resources/assets/drivebywire/lang/en_us.json b/src/main/resources/assets/drivebywire/lang/en_us.json index b011897..33982ac 100644 --- a/src/main/resources/assets/drivebywire/lang/en_us.json +++ b/src/main/resources/assets/drivebywire/lang/en_us.json @@ -4,6 +4,7 @@ "block.drivebywire.backup_block" : "Network Backup Block", "block.drivebywire.controller_hub" : "Linked Controller Hub", "block.drivebywire.tweaked_controller_hub" : "Tweaked Controller Hub", + "block.drivebywire.tfmg_engine_controller_hub": "TFMG Engine Controller Hub", "item.drivebywire.wire" : "Cable", "item.drivebywire.wire_cutter" : "Cable Cutter", @@ -12,6 +13,7 @@ "block.drivebywire.backup_block.tooltip.summary" : "Saves the ship network when put in a schematic, and loads when shipified", "block.drivebywire.controller_hub.tooltip.summary" : "Bind your controller to this block, and control this network from far away!", "block.drivebywire.tweaked_controller_hub.tooltip.summary" : "Bind your controller to this block, and control this network from far away!", + "block.drivebywire.tfmg_engine_controller_hub.tooltip.summary" : "Bind your controller to this block, and control this network from far away!", "drivebywire.ponder.wires.header" : "Cable networks 101", "drivebywire.ponder.wires.text_1" : "Disclaimer: Cable networks only work on assembled VS2 ships!", diff --git a/src/main/resources/assets/drivebywire/models/item/tfmg_engine_controller_hub.json b/src/main/resources/assets/drivebywire/models/item/tfmg_engine_controller_hub.json new file mode 100644 index 0000000..d2729a9 --- /dev/null +++ b/src/main/resources/assets/drivebywire/models/item/tfmg_engine_controller_hub.json @@ -0,0 +1,3 @@ +{ + "parent": "drivebywire:block/hub" +} \ No newline at end of file diff --git a/src/main/resources/drivebywire.mixins.json b/src/main/resources/drivebywire.mixins.json index 2d27cce..1ed42f2 100644 --- a/src/main/resources/drivebywire.mixins.json +++ b/src/main/resources/drivebywire.mixins.json @@ -11,7 +11,9 @@ "compat.tweaked.MixinTweakedController", "compat.tweaked.MixinTweakedControllerAxisPacket", "compat.tweaked.MixinTweakedControllerButtonPacket", - "compat.tweaked.MixinTweakedControllerStopLecternPacket" + "compat.tweaked.MixinTweakedControllerStopLecternPacket", + "compat.tfmg.MixinTFMGEngineControllerBlock", + "compat.tfmg.MixinTFMGEngineControllerBlockEntity" ], "client": [ "client.MixinSnqHandler" From e69e45261a021ba36447d5c8c974444d83d6101f Mon Sep 17 00:00:00 2001 From: Saravanan S Date: Sat, 27 Dec 2025 23:43:47 +0530 Subject: [PATCH 2/4] Added Engine Started Channel --- .../drivebywire/blocks/TFMGEngineControllerHubBlock.java | 2 ++ .../compat/TFMGEngineControllerWireServerHandler.java | 2 ++ .../mixin/compat/tfmg/MixinTFMGEngineControllerBlock.java | 2 ++ .../compat/tfmg/MixinTFMGEngineControllerBlockEntity.java | 4 ++++ 4 files changed, 10 insertions(+) diff --git a/src/main/java/edn/stratodonut/drivebywire/blocks/TFMGEngineControllerHubBlock.java b/src/main/java/edn/stratodonut/drivebywire/blocks/TFMGEngineControllerHubBlock.java index 0827a83..4caf2aa 100644 --- a/src/main/java/edn/stratodonut/drivebywire/blocks/TFMGEngineControllerHubBlock.java +++ b/src/main/java/edn/stratodonut/drivebywire/blocks/TFMGEngineControllerHubBlock.java @@ -28,6 +28,8 @@ public class TFMGEngineControllerHubBlock extends Block implements MultiChannelW TFMGEngineControllerWireServerHandler.STEER_LEFT, TFMGEngineControllerWireServerHandler.STEER_RIGHT, + TFMGEngineControllerWireServerHandler.ENGINE_STARTED, + TFMGEngineControllerWireServerHandler.SHIFT_REVERSE, TFMGEngineControllerWireServerHandler.SHIFT_NEUTRAL, TFMGEngineControllerWireServerHandler.SHIFT_1, diff --git a/src/main/java/edn/stratodonut/drivebywire/compat/TFMGEngineControllerWireServerHandler.java b/src/main/java/edn/stratodonut/drivebywire/compat/TFMGEngineControllerWireServerHandler.java index fd47be9..2ff003d 100644 --- a/src/main/java/edn/stratodonut/drivebywire/compat/TFMGEngineControllerWireServerHandler.java +++ b/src/main/java/edn/stratodonut/drivebywire/compat/TFMGEngineControllerWireServerHandler.java @@ -37,6 +37,8 @@ public static void reset(Level level, BlockPos pos) { set(level, pos, STEER_LEFT, false); set(level, pos, STEER_RIGHT, false); + set(level, pos, ENGINE_STARTED, false); + set(level, pos, SHIFT_REVERSE, false); set(level, pos, SHIFT_NEUTRAL, false); set(level, pos, SHIFT_1, false); diff --git a/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlock.java b/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlock.java index af1290e..2317abb 100644 --- a/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlock.java +++ b/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlock.java @@ -18,6 +18,8 @@ public abstract class MixinTFMGEngineControllerBlock implements MultiChannelWire TFMGEngineControllerWireServerHandler.STEER_LEFT, TFMGEngineControllerWireServerHandler.STEER_RIGHT, + TFMGEngineControllerWireServerHandler.ENGINE_STARTED, + TFMGEngineControllerWireServerHandler.SHIFT_REVERSE, TFMGEngineControllerWireServerHandler.SHIFT_NEUTRAL, TFMGEngineControllerWireServerHandler.SHIFT_1, diff --git a/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlockEntity.java b/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlockEntity.java index 44c67e9..771f1bf 100644 --- a/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlockEntity.java +++ b/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlockEntity.java @@ -25,6 +25,8 @@ public abstract class MixinTFMGEngineControllerBlockEntity { boolean steerLeft = self.isPressed(2); boolean steerRight = self.isPressed(3); + boolean engineStarted = self.engineStarted; + boolean shiftReverse = self.shift == TransmissionState.REVERSE; boolean shiftNeutral = self.shift == TransmissionState.NEUTRAL; boolean shift1 = self.shift == TransmissionState.SHIFT_1; @@ -41,6 +43,8 @@ public abstract class MixinTFMGEngineControllerBlockEntity { TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.STEER_LEFT, steerLeft); TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.STEER_RIGHT, steerRight); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.ENGINE_STARTED, engineStarted); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_REVERSE, shiftReverse); TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_NEUTRAL, shiftNeutral); TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_1, shift1); From 4f3c7885d6de93e5147138e4f4f296579bf9bd7e Mon Sep 17 00:00:00 2001 From: Saravanan S Date: Sun, 15 Feb 2026 16:42:48 +0530 Subject: [PATCH 3/4] Fixed issue on server --- .../stratodonut/drivebywire/WirePackets.java | 6 +- .../client/TFMGClientControllerTracker.java | 20 +++++++ .../drivebywire/compat/TFMGSteeringState.java | 30 ++++++++++ ...FMGEngineControllerClientHandlerMixin.java | 30 ++++++++++ ...MGEngineControllerClientSteeringMixin.java | 49 +++++++++++++++++ .../MixinTFMGEngineControllerBlockEntity.java | 55 ++++++++----------- .../network/TFMGSteeringInputPacket.java | 45 +++++++++++++++ .../server/TFMGSteeringInputCache.java | 26 +++++++++ src/main/resources/drivebywire.mixins.json | 4 +- 9 files changed, 230 insertions(+), 35 deletions(-) create mode 100644 src/main/java/edn/stratodonut/drivebywire/client/TFMGClientControllerTracker.java create mode 100644 src/main/java/edn/stratodonut/drivebywire/compat/TFMGSteeringState.java create mode 100644 src/main/java/edn/stratodonut/drivebywire/mixin/client/TFMGEngineControllerClientHandlerMixin.java create mode 100644 src/main/java/edn/stratodonut/drivebywire/mixin/client/TFMGEngineControllerClientSteeringMixin.java create mode 100644 src/main/java/edn/stratodonut/drivebywire/network/TFMGSteeringInputPacket.java create mode 100644 src/main/java/edn/stratodonut/drivebywire/server/TFMGSteeringInputCache.java diff --git a/src/main/java/edn/stratodonut/drivebywire/WirePackets.java b/src/main/java/edn/stratodonut/drivebywire/WirePackets.java index 6e9a2b8..a73aff4 100644 --- a/src/main/java/edn/stratodonut/drivebywire/WirePackets.java +++ b/src/main/java/edn/stratodonut/drivebywire/WirePackets.java @@ -22,7 +22,9 @@ public enum WirePackets { CREATE_CONNECTION(WireAddConnectionPacket.class, WireAddConnectionPacket::new, PLAY_TO_SERVER), REMOVE_CONNECTION(WireRemoveConnectionPacket.class, WireRemoveConnectionPacket::new, PLAY_TO_SERVER), REQUEST_SYNC(WireNetworkRequestSyncPacket.class, WireNetworkRequestSyncPacket::new, PLAY_TO_SERVER), - LINK_NETWORKS(WireLinkNetworksPacket .class, WireLinkNetworksPacket::new, PLAY_TO_SERVER); + LINK_NETWORKS(WireLinkNetworksPacket .class, WireLinkNetworksPacket::new, PLAY_TO_SERVER), + + TFMG_STEERING_INPUT(TFMGSteeringInputPacket.class, TFMGSteeringInputPacket::new, PLAY_TO_SERVER); // DO NOT TOUCH ANYTHING BELOW THIS LINE, THANKS CREATE @@ -47,6 +49,8 @@ public static void registerPackets() { for (WirePackets packet : values()) packet.packetType.register(); + + System.out.println("[DBW] Packets registered"); } public static SimpleChannel getChannel() { diff --git a/src/main/java/edn/stratodonut/drivebywire/client/TFMGClientControllerTracker.java b/src/main/java/edn/stratodonut/drivebywire/client/TFMGClientControllerTracker.java new file mode 100644 index 0000000..5187dee --- /dev/null +++ b/src/main/java/edn/stratodonut/drivebywire/client/TFMGClientControllerTracker.java @@ -0,0 +1,20 @@ +package edn.stratodonut.drivebywire.client; + +import net.minecraft.core.BlockPos; + +public final class TFMGClientControllerTracker { + + private static BlockPos current; + + public static void set(BlockPos pos) { + current = pos; + } + + public static void clear() { + current = null; + } + + public static BlockPos get() { + return current; + } +} diff --git a/src/main/java/edn/stratodonut/drivebywire/compat/TFMGSteeringState.java b/src/main/java/edn/stratodonut/drivebywire/compat/TFMGSteeringState.java new file mode 100644 index 0000000..7c33ef3 --- /dev/null +++ b/src/main/java/edn/stratodonut/drivebywire/compat/TFMGSteeringState.java @@ -0,0 +1,30 @@ +package edn.stratodonut.drivebywire.compat; + +import net.minecraft.core.BlockPos; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class TFMGSteeringState { + + private static final Map LEFT = new ConcurrentHashMap<>(); + private static final Map RIGHT = new ConcurrentHashMap<>(); + + public static void set(BlockPos pos, boolean left, boolean right) { + LEFT.put(pos, left); + RIGHT.put(pos, right); + } + + public static boolean isLeft(BlockPos pos) { + return LEFT.getOrDefault(pos, false); + } + + public static boolean isRight(BlockPos pos) { + return RIGHT.getOrDefault(pos, false); + } + + public static void clear(BlockPos pos) { + LEFT.remove(pos); + RIGHT.remove(pos); + } +} diff --git a/src/main/java/edn/stratodonut/drivebywire/mixin/client/TFMGEngineControllerClientHandlerMixin.java b/src/main/java/edn/stratodonut/drivebywire/mixin/client/TFMGEngineControllerClientHandlerMixin.java new file mode 100644 index 0000000..4c35d25 --- /dev/null +++ b/src/main/java/edn/stratodonut/drivebywire/mixin/client/TFMGEngineControllerClientHandlerMixin.java @@ -0,0 +1,30 @@ +package edn.stratodonut.drivebywire.mixin.client; + +import com.drmangotea.tfmg.content.engines.engine_controller.EngineControllerClientHandler; +import edn.stratodonut.drivebywire.client.TFMGClientControllerTracker; +import net.minecraft.core.BlockPos; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@OnlyIn(Dist.CLIENT) +@Mixin(EngineControllerClientHandler.class) +public class TFMGEngineControllerClientHandlerMixin { + + static { + System.out.println("[DBW] TFMG EngineControllerClientHandler mixin loaded"); + } + + @Inject(method = "activateInLectern", at = @At("HEAD"), remap = false) + private static void drivebywire$onActivate(BlockPos pos, CallbackInfo ci) { + TFMGClientControllerTracker.set(pos); + } + + @Inject(method = "deactivateInLectern", at = @At("HEAD"), remap = false) + private static void drivebywire$onDeactivate(CallbackInfo ci) { + TFMGClientControllerTracker.clear(); + } +} diff --git a/src/main/java/edn/stratodonut/drivebywire/mixin/client/TFMGEngineControllerClientSteeringMixin.java b/src/main/java/edn/stratodonut/drivebywire/mixin/client/TFMGEngineControllerClientSteeringMixin.java new file mode 100644 index 0000000..17ee980 --- /dev/null +++ b/src/main/java/edn/stratodonut/drivebywire/mixin/client/TFMGEngineControllerClientSteeringMixin.java @@ -0,0 +1,49 @@ +package edn.stratodonut.drivebywire.mixin.client; + +import com.drmangotea.tfmg.content.engines.engine_controller.EngineControllerBlockEntity; +import edn.stratodonut.drivebywire.WirePackets; +import edn.stratodonut.drivebywire.client.TFMGClientControllerTracker; +import edn.stratodonut.drivebywire.network.TFMGSteeringInputPacket; +import net.minecraft.client.Minecraft; +import net.minecraft.core.BlockPos; +import net.minecraft.world.level.Level; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Pseudo; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@OnlyIn(Dist.CLIENT) +@Pseudo +@Mixin(EngineControllerBlockEntity.class) +public abstract class TFMGEngineControllerClientSteeringMixin { + + @Inject(method = "tick", at = @At("TAIL"), remap = false) + private void drivebywire$captureSteering(CallbackInfo ci) { + EngineControllerBlockEntity self = (EngineControllerBlockEntity)(Object)this; + Level level = self.getLevel(); + + if (level == null || !level.isClientSide) return; + + BlockPos active = TFMGClientControllerTracker.get(); + if (active == null || !active.equals(self.getBlockPos())) return; + + float angle = self.steeringWheelAngle.getValue(); + + final float CENTER_EPS = 4.0f; + + // force-center early + if (Math.abs(angle) < CENTER_EPS) { + angle = 0.0f; + } + + boolean left = angle < 0; + boolean right = angle > 0; + + WirePackets.getChannel().sendToServer( + new TFMGSteeringInputPacket(self.getBlockPos(), left, right) + ); + } +} diff --git a/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlockEntity.java b/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlockEntity.java index 771f1bf..fcea14b 100644 --- a/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlockEntity.java +++ b/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlockEntity.java @@ -3,6 +3,8 @@ import com.drmangotea.tfmg.content.engines.engine_controller.EngineControllerBlockEntity; import com.drmangotea.tfmg.content.engines.upgrades.TransmissionUpgrade.TransmissionState; import edn.stratodonut.drivebywire.compat.TFMGEngineControllerWireServerHandler; +import edn.stratodonut.drivebywire.compat.TFMGSteeringState; +import net.minecraft.core.BlockPos; import net.minecraft.world.level.Level; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Pseudo; @@ -20,50 +22,37 @@ public abstract class MixinTFMGEngineControllerBlockEntity { Level level = self.getLevel(); if (level == null || level.isClientSide) return; - var pos = self.getBlockPos(); + BlockPos pos = self.getBlockPos(); - boolean steerLeft = self.isPressed(2); - boolean steerRight = self.isPressed(3); - - boolean engineStarted = self.engineStarted; - - boolean shiftReverse = self.shift == TransmissionState.REVERSE; - boolean shiftNeutral = self.shift == TransmissionState.NEUTRAL; - boolean shift1 = self.shift == TransmissionState.SHIFT_1; - boolean shift2 = self.shift == TransmissionState.SHIFT_2; - boolean shift3 = self.shift == TransmissionState.SHIFT_3; - boolean shift4 = self.shift == TransmissionState.SHIFT_4; - boolean shift5 = self.shift == TransmissionState.SHIFT_5; - boolean shift6 = self.shift == TransmissionState.SHIFT_6; - - boolean pedalClutch = self.clutch; - boolean pedalBrake = self.brake; - boolean pedalGas = self.gas; + boolean steerLeft = TFMGSteeringState.isLeft(pos); + boolean steerRight = TFMGSteeringState.isRight(pos); TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.STEER_LEFT, steerLeft); TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.STEER_RIGHT, steerRight); - TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.ENGINE_STARTED, engineStarted); - - TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_REVERSE, shiftReverse); - TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_NEUTRAL, shiftNeutral); - TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_1, shift1); - TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_2, shift2); - TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_3, shift3); - TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_4, shift4); - TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_5, shift5); - TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_6, shift6); - - TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.PEDAL_CLUTCH, pedalClutch); - TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.PEDAL_BRAKE, pedalBrake); - TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.PEDAL_GAS, pedalGas); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.ENGINE_STARTED, self.engineStarted); + + TransmissionState s = self.shift; + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_REVERSE, s == TransmissionState.REVERSE); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_NEUTRAL, s == TransmissionState.NEUTRAL); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_1, s == TransmissionState.SHIFT_1); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_2, s == TransmissionState.SHIFT_2); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_3, s == TransmissionState.SHIFT_3); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_4, s == TransmissionState.SHIFT_4); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_5, s == TransmissionState.SHIFT_5); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.SHIFT_6, s == TransmissionState.SHIFT_6); + + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.PEDAL_CLUTCH, self.clutch); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.PEDAL_BRAKE, self.brake); + TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.PEDAL_GAS, self.gas); } @Inject(method = "remove", at = @At("HEAD"), remap = false) - private void drivebywire$reset(CallbackInfo ci) { + private void drivebywire$cleanup(CallbackInfo ci) { EngineControllerBlockEntity self = (EngineControllerBlockEntity)(Object)this; Level level = self.getLevel(); if (level != null && !level.isClientSide) { + TFMGSteeringState.clear(self.getBlockPos()); TFMGEngineControllerWireServerHandler.reset(level, self.getBlockPos()); } } diff --git a/src/main/java/edn/stratodonut/drivebywire/network/TFMGSteeringInputPacket.java b/src/main/java/edn/stratodonut/drivebywire/network/TFMGSteeringInputPacket.java new file mode 100644 index 0000000..1bd4a58 --- /dev/null +++ b/src/main/java/edn/stratodonut/drivebywire/network/TFMGSteeringInputPacket.java @@ -0,0 +1,45 @@ +package edn.stratodonut.drivebywire.network; + +import com.simibubi.create.foundation.networking.SimplePacketBase; +import edn.stratodonut.drivebywire.compat.TFMGSteeringState; +import net.minecraft.core.BlockPos; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.network.NetworkEvent; + +public class TFMGSteeringInputPacket extends SimplePacketBase { + + private final BlockPos pos; + private final boolean left; + private final boolean right; + + public TFMGSteeringInputPacket(BlockPos pos, boolean left, boolean right) { + this.pos = pos; + this.left = left; + this.right = right; + } + + public TFMGSteeringInputPacket(FriendlyByteBuf buf) { + this.pos = buf.readBlockPos(); + this.left = buf.readBoolean(); + this.right = buf.readBoolean(); + } + + @Override + public void write(FriendlyByteBuf buf) { + buf.writeBlockPos(pos); + buf.writeBoolean(left); + buf.writeBoolean(right); + } + + @Override + public boolean handle(NetworkEvent.Context ctx) { + ctx.enqueueWork(() -> { + ServerPlayer player = ctx.getSender(); + if (player == null) return; + + TFMGSteeringState.set(pos, left, right); + }); + return true; + } +} \ No newline at end of file diff --git a/src/main/java/edn/stratodonut/drivebywire/server/TFMGSteeringInputCache.java b/src/main/java/edn/stratodonut/drivebywire/server/TFMGSteeringInputCache.java new file mode 100644 index 0000000..512e622 --- /dev/null +++ b/src/main/java/edn/stratodonut/drivebywire/server/TFMGSteeringInputCache.java @@ -0,0 +1,26 @@ +package edn.stratodonut.drivebywire.server; + +import net.minecraft.core.BlockPos; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public final class TFMGSteeringInputCache { + + private static final Map CACHE = new ConcurrentHashMap<>(); + + public static void update(BlockPos pos, boolean left, boolean right) { + CACHE.put(pos, new State(left, right)); + } + + public static State get(BlockPos pos) { + return CACHE.getOrDefault(pos, State.NONE); + } + + public static void clear(BlockPos pos) { + CACHE.remove(pos); + } + + public record State(boolean left, boolean right) { + public static final State NONE = new State(false, false); + } +} diff --git a/src/main/resources/drivebywire.mixins.json b/src/main/resources/drivebywire.mixins.json index 1ed42f2..4482ad4 100644 --- a/src/main/resources/drivebywire.mixins.json +++ b/src/main/resources/drivebywire.mixins.json @@ -16,7 +16,9 @@ "compat.tfmg.MixinTFMGEngineControllerBlockEntity" ], "client": [ - "client.MixinSnqHandler" + "client.MixinSnqHandler", + "client.TFMGEngineControllerClientHandlerMixin", + "client.TFMGEngineControllerClientSteeringMixin" ], "injectors": { "defaultRequire": 1 From 757bf10aa6ae0eb7d1ec84beec11c03951ae9b8b Mon Sep 17 00:00:00 2001 From: Saravanan S Date: Sun, 1 Mar 2026 22:51:38 +0530 Subject: [PATCH 4/4] Added analog steering --- ...TFMGEngineControllerWireServerHandler.java | 14 ++++++++ .../drivebywire/compat/TFMGSteeringState.java | 19 ++++------- ...MGEngineControllerClientSteeringMixin.java | 32 +++++++++++++++---- .../MixinTFMGEngineControllerBlockEntity.java | 9 +++--- .../network/TFMGSteeringInputPacket.java | 16 ++++------ 5 files changed, 56 insertions(+), 34 deletions(-) diff --git a/src/main/java/edn/stratodonut/drivebywire/compat/TFMGEngineControllerWireServerHandler.java b/src/main/java/edn/stratodonut/drivebywire/compat/TFMGEngineControllerWireServerHandler.java index 2ff003d..173349c 100644 --- a/src/main/java/edn/stratodonut/drivebywire/compat/TFMGEngineControllerWireServerHandler.java +++ b/src/main/java/edn/stratodonut/drivebywire/compat/TFMGEngineControllerWireServerHandler.java @@ -33,6 +33,20 @@ public static void set(Level level, BlockPos pos, String channel, boolean active ); } + public static void setAnalog(Level level, BlockPos pos, String channel, float value) { + // clamp between 0 and 1 + float clamped = Math.max(0f, Math.min(1f, value)); + + int strength = (int)(clamped * 15f); + + ShipWireNetworkManager.trySetSignalAt( + level, + pos, + channel, + strength + ); + } + public static void reset(Level level, BlockPos pos) { set(level, pos, STEER_LEFT, false); set(level, pos, STEER_RIGHT, false); diff --git a/src/main/java/edn/stratodonut/drivebywire/compat/TFMGSteeringState.java b/src/main/java/edn/stratodonut/drivebywire/compat/TFMGSteeringState.java index 7c33ef3..b122715 100644 --- a/src/main/java/edn/stratodonut/drivebywire/compat/TFMGSteeringState.java +++ b/src/main/java/edn/stratodonut/drivebywire/compat/TFMGSteeringState.java @@ -7,24 +7,17 @@ public class TFMGSteeringState { - private static final Map LEFT = new ConcurrentHashMap<>(); - private static final Map RIGHT = new ConcurrentHashMap<>(); + private static final Map STEER = new ConcurrentHashMap<>(); - public static void set(BlockPos pos, boolean left, boolean right) { - LEFT.put(pos, left); - RIGHT.put(pos, right); + public static void set(BlockPos pos, float steer) { + STEER.put(pos, steer); } - public static boolean isLeft(BlockPos pos) { - return LEFT.getOrDefault(pos, false); - } - - public static boolean isRight(BlockPos pos) { - return RIGHT.getOrDefault(pos, false); + public static float getSteer(BlockPos pos) { + return STEER.getOrDefault(pos, 0f); } public static void clear(BlockPos pos) { - LEFT.remove(pos); - RIGHT.remove(pos); + STEER.remove(pos); } } diff --git a/src/main/java/edn/stratodonut/drivebywire/mixin/client/TFMGEngineControllerClientSteeringMixin.java b/src/main/java/edn/stratodonut/drivebywire/mixin/client/TFMGEngineControllerClientSteeringMixin.java index 17ee980..e05e128 100644 --- a/src/main/java/edn/stratodonut/drivebywire/mixin/client/TFMGEngineControllerClientSteeringMixin.java +++ b/src/main/java/edn/stratodonut/drivebywire/mixin/client/TFMGEngineControllerClientSteeringMixin.java @@ -20,6 +20,8 @@ @Mixin(EngineControllerBlockEntity.class) public abstract class TFMGEngineControllerClientSteeringMixin { + private float drivebywire$lastSent = 0f; + @Inject(method = "tick", at = @At("TAIL"), remap = false) private void drivebywire$captureSteering(CallbackInfo ci) { EngineControllerBlockEntity self = (EngineControllerBlockEntity)(Object)this; @@ -32,18 +34,34 @@ public abstract class TFMGEngineControllerClientSteeringMixin { float angle = self.steeringWheelAngle.getValue(); - final float CENTER_EPS = 4.0f; + final float DEADZONE = 4.0f; + if (Math.abs(angle) < DEADZONE) { + angle = 0f; + } + + final float MAX_STEER = 38f; + float normalized = angle / MAX_STEER; + normalized = Math.max(-1f, Math.min(1f, normalized)); + + float delta = Math.abs(normalized - drivebywire$lastSent); + + if (normalized == 0f && drivebywire$lastSent != 0f) { + drivebywire$lastSent = 0f; + + WirePackets.getChannel().sendToServer( + new TFMGSteeringInputPacket(self.getBlockPos(), 0f) + ); + return; + } - // force-center early - if (Math.abs(angle) < CENTER_EPS) { - angle = 0.0f; + if (delta < 0.02f) { + return; } - boolean left = angle < 0; - boolean right = angle > 0; + drivebywire$lastSent = normalized; WirePackets.getChannel().sendToServer( - new TFMGSteeringInputPacket(self.getBlockPos(), left, right) + new TFMGSteeringInputPacket(self.getBlockPos(), normalized) ); } } diff --git a/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlockEntity.java b/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlockEntity.java index fcea14b..3ccfd5d 100644 --- a/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlockEntity.java +++ b/src/main/java/edn/stratodonut/drivebywire/mixin/compat/tfmg/MixinTFMGEngineControllerBlockEntity.java @@ -24,11 +24,12 @@ public abstract class MixinTFMGEngineControllerBlockEntity { BlockPos pos = self.getBlockPos(); - boolean steerLeft = TFMGSteeringState.isLeft(pos); - boolean steerRight = TFMGSteeringState.isRight(pos); + float steer = TFMGSteeringState.getSteer(pos); + float leftSteerSignal = steer < 0 ? -steer : 0f; + float rightSteerSignal = steer > 0 ? steer : 0f; - TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.STEER_LEFT, steerLeft); - TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.STEER_RIGHT, steerRight); + TFMGEngineControllerWireServerHandler.setAnalog(level, pos, TFMGEngineControllerWireServerHandler.STEER_LEFT, leftSteerSignal); + TFMGEngineControllerWireServerHandler.setAnalog(level, pos, TFMGEngineControllerWireServerHandler.STEER_RIGHT, rightSteerSignal); TFMGEngineControllerWireServerHandler.set(level, pos, TFMGEngineControllerWireServerHandler.ENGINE_STARTED, self.engineStarted); diff --git a/src/main/java/edn/stratodonut/drivebywire/network/TFMGSteeringInputPacket.java b/src/main/java/edn/stratodonut/drivebywire/network/TFMGSteeringInputPacket.java index 1bd4a58..ea0822b 100644 --- a/src/main/java/edn/stratodonut/drivebywire/network/TFMGSteeringInputPacket.java +++ b/src/main/java/edn/stratodonut/drivebywire/network/TFMGSteeringInputPacket.java @@ -10,26 +10,22 @@ public class TFMGSteeringInputPacket extends SimplePacketBase { private final BlockPos pos; - private final boolean left; - private final boolean right; + float steering; - public TFMGSteeringInputPacket(BlockPos pos, boolean left, boolean right) { + public TFMGSteeringInputPacket(BlockPos pos, float steering) { this.pos = pos; - this.left = left; - this.right = right; + this.steering = steering; } public TFMGSteeringInputPacket(FriendlyByteBuf buf) { this.pos = buf.readBlockPos(); - this.left = buf.readBoolean(); - this.right = buf.readBoolean(); + this.steering = buf.readFloat(); } @Override public void write(FriendlyByteBuf buf) { buf.writeBlockPos(pos); - buf.writeBoolean(left); - buf.writeBoolean(right); + buf.writeFloat(steering); } @Override @@ -38,7 +34,7 @@ public boolean handle(NetworkEvent.Context ctx) { ServerPlayer player = ctx.getSender(); if (player == null) return; - TFMGSteeringState.set(pos, left, right); + TFMGSteeringState.set(pos, steering); }); return true; }