From 2fd25d470948451fbef7302c6a6ed8eebfea6911 Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 11 Mar 2023 18:08:42 +0100 Subject: fix: GuiMove now works, I dug way more... --- .../java/ftbsc/bscv/modules/motion/GuiMove.java | 84 ++++++++++++++++------ 1 file changed, 63 insertions(+), 21 deletions(-) diff --git a/src/main/java/ftbsc/bscv/modules/motion/GuiMove.java b/src/main/java/ftbsc/bscv/modules/motion/GuiMove.java index 9d3bf1c..d567375 100644 --- a/src/main/java/ftbsc/bscv/modules/motion/GuiMove.java +++ b/src/main/java/ftbsc/bscv/modules/motion/GuiMove.java @@ -5,44 +5,86 @@ import com.google.auto.service.AutoService; import ftbsc.bscv.ICommons; import ftbsc.bscv.api.ILoadable; import ftbsc.bscv.modules.AbstractModule; +import net.minecraft.client.gui.advancements.AdvancementsScreen; +import net.minecraft.client.gui.screen.CustomizeSkinScreen; import net.minecraft.client.gui.screen.IngameMenuScreen; import net.minecraft.client.gui.screen.OptionsScreen; import net.minecraft.client.gui.screen.OptionsSoundsScreen; -import net.minecraft.client.gui.screen.VideoSettingsScreen; +import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.inventory.ContainerScreen; import net.minecraft.client.settings.KeyBinding; import net.minecraft.client.util.InputMappings; -import net.minecraftforge.event.entity.living.LivingEvent; +import net.minecraftforge.client.event.InputUpdateEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.client.gui.screen.ModListScreen; @AutoService(ILoadable.class) public class GuiMove extends AbstractModule implements ICommons { + private boolean once = false; + + private boolean isKeyDown(KeyBinding key) { + return InputMappings.isKeyDown(MC.getWindow().getWindow(), key.getKey().getValue()); + } + + private KeyBinding[] keys = { + MC.options.keyUp, + MC.options.keyDown, + MC.options.keyLeft, + MC.options.keyRight, + MC.options.keyJump, + }; + + private Class[] screens = { + ContainerScreen.class, + OptionsScreen.class, + OptionsSoundsScreen.class, + IngameMenuScreen.class, + AdvancementsScreen.class, + ModListScreen.class, + CustomizeSkinScreen.class, + }; + + private void forceMovementTick() { + // TODO can we patch to make this always happen instead of duplicating code? + MC.player.input.up = this.isKeyDown(MC.options.keyUp); + MC.player.input.down = this.isKeyDown(MC.options.keyDown); + MC.player.input.left = this.isKeyDown(MC.options.keyLeft); + MC.player.input.right = this.isKeyDown(MC.options.keyRight); + MC.player.input.jumping = this.isKeyDown(MC.options.keyJump); + MC.player.input.shiftKeyDown = this.isKeyDown(MC.options.keyShift); + + MC.player.input.forwardImpulse = MC.player.input.up == MC.player.input.down ? 0.0F : (MC.player.input.up ? 1.0F : -1.0F); + MC.player.input.leftImpulse = MC.player.input.left == MC.player.input.right ? 0.0F : (MC.player.input.left ? 1.0F : -1.0F); + } + + private boolean allowMovementOnThisScreen(Screen screen) { + for (Class clazz : this.screens) { + if (clazz.isInstance(screen)) { + return true; + } + } + return false; + } + @SubscribeEvent - public void onPlayerUpdate(LivingEvent.LivingUpdateEvent event) { + public void onInputUpdate(InputUpdateEvent event) { if (MC.player == null) return; if (event.getEntityLiving() != MC.player) return; - KeyBinding[] keys = { - MC.options.keyUp, - MC.options.keyDown, - MC.options.keyLeft, - MC.options.keyRight, - MC.options.keyJump, - }; - - if ( - MC.screen instanceof ContainerScreen - || MC.screen instanceof OptionsScreen - || MC.screen instanceof VideoSettingsScreen - || MC.screen instanceof OptionsSoundsScreen - || MC.screen instanceof IngameMenuScreen - ) { - for (KeyBinding key : keys) { + if (this.allowMovementOnThisScreen(MC.screen)) { + once = true; + for (KeyBinding key : this.keys) { boolean state = InputMappings.isKeyDown(MC.getWindow().getWindow(), key.getKey().getValue()); - KeyBinding.set(key.getKey(), state); - key.setDown(state); + if (state ^ key.isDown()) { + KeyBinding.set(key.getKey(), state); + key.setDown(state); + } } + this.forceMovementTick(); + } else if (once) { // release all keys once we leave the screen + once = false; + KeyBinding.releaseAll(); // TODO maybe only release movement keys? } } -- cgit v1.2.3-56-ga3b1