From 001b27cecaa984b900923a866f93c92a18f9b789 Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 11 Mar 2023 22:42:28 +0100 Subject: [PATCH] feat: allow canceling background, fix key release --- .../ftbsc/bscv/modules/motion/GuiMove.java | 36 +++++++--- .../ftbsc/bscv/patches/BackgroundPatch.java | 68 +++++++++++++++++++ 2 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 src/main/java/ftbsc/bscv/patches/BackgroundPatch.java diff --git a/src/main/java/ftbsc/bscv/modules/motion/GuiMove.java b/src/main/java/ftbsc/bscv/modules/motion/GuiMove.java index d567375..193d77a 100644 --- a/src/main/java/ftbsc/bscv/modules/motion/GuiMove.java +++ b/src/main/java/ftbsc/bscv/modules/motion/GuiMove.java @@ -2,9 +2,10 @@ package ftbsc.bscv.modules.motion; import com.google.auto.service.AutoService; -import ftbsc.bscv.ICommons; import ftbsc.bscv.api.ILoadable; import ftbsc.bscv.modules.AbstractModule; +import ftbsc.bscv.patches.BackgroundPatch.RenderBackgroundEvent; +import ftbsc.bscv.tools.Setting; import net.minecraft.client.gui.advancements.AdvancementsScreen; import net.minecraft.client.gui.screen.CustomizeSkinScreen; import net.minecraft.client.gui.screen.IngameMenuScreen; @@ -15,13 +16,26 @@ import net.minecraft.client.gui.screen.inventory.ContainerScreen; import net.minecraft.client.settings.KeyBinding; import net.minecraft.client.util.InputMappings; import net.minecraftforge.client.event.InputUpdateEvent; +import net.minecraftforge.common.ForgeConfigSpec; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.client.gui.screen.ModListScreen; @AutoService(ILoadable.class) -public class GuiMove extends AbstractModule implements ICommons { +public class GuiMove extends AbstractModule { - private boolean once = false; + // TODO allow pausing GuiMove when a textfield is selected + + public final ForgeConfigSpec.ConfigValue background; + + public GuiMove() { + super(); + + this.background = Setting.Bool.builder() + .fallback(true) + .name("background") + .comment("show background on inventories when allowing to move") + .build(this); + } private boolean isKeyDown(KeyBinding key) { return InputMappings.isKeyDown(MC.getWindow().getWindow(), key.getKey().getValue()); @@ -56,6 +70,10 @@ public class GuiMove extends AbstractModule implements ICommons { 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); + + if (MC.player.input.shiftKeyDown && !MC.player.isSprinting()) { + MC.player.setSprinting(true); + } } private boolean allowMovementOnThisScreen(Screen screen) { @@ -67,13 +85,19 @@ public class GuiMove extends AbstractModule implements ICommons { return false; } + @SubscribeEvent + public void onRenderBackground(RenderBackgroundEvent event) { + if (!this.background.get() && event.screen instanceof ContainerScreen) { + event.setCanceled(true); + } + } + @SubscribeEvent public void onInputUpdate(InputUpdateEvent event) { if (MC.player == null) return; if (event.getEntityLiving() != MC.player) return; if (this.allowMovementOnThisScreen(MC.screen)) { - once = true; for (KeyBinding key : this.keys) { boolean state = InputMappings.isKeyDown(MC.getWindow().getWindow(), key.getKey().getValue()); if (state ^ key.isDown()) { @@ -82,10 +106,6 @@ public class GuiMove extends AbstractModule implements ICommons { } } this.forceMovementTick(); - } else if (once) { // release all keys once we leave the screen - once = false; - KeyBinding.releaseAll(); // TODO maybe only release movement keys? } } - } diff --git a/src/main/java/ftbsc/bscv/patches/BackgroundPatch.java b/src/main/java/ftbsc/bscv/patches/BackgroundPatch.java new file mode 100644 index 0000000..dae50ed --- /dev/null +++ b/src/main/java/ftbsc/bscv/patches/BackgroundPatch.java @@ -0,0 +1,68 @@ +package ftbsc.bscv.patches; + +import ftbsc.bscv.ICommons; +import ftbsc.lll.processor.annotations.Injector; +import ftbsc.lll.processor.annotations.Patch; +import ftbsc.lll.processor.annotations.Target; +import ftbsc.lll.tools.InsnSequence; +import ftbsc.lll.tools.PatternMatcher; +import ftbsc.lll.tools.debug.BytecodePrinter; +import net.minecraft.client.gui.screen.Screen; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.eventbus.api.Cancelable; +import net.minecraftforge.eventbus.api.Event; + +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.*; + +import com.mojang.blaze3d.matrix.MatrixStack; + +public class BackgroundPatch implements ICommons { + + public static boolean shouldDrawBackground(Screen screen) { + return MinecraftForge.EVENT_BUS.post(new RenderBackgroundEvent(screen)); + } + + @Cancelable + public static class RenderBackgroundEvent extends Event { + public final Screen screen; + + public RenderBackgroundEvent(Screen screen) { + this.screen = screen; + } + } + + @Patch(value = Screen.class, reason = "add hook to cancel background on some screens") + public abstract static class BackgroundOverride implements Opcodes { + @Target + abstract void renderBackground(MatrixStack stack, int x); + + @Injector + public void inject(ClassNode clazz, MethodNode main) { + AbstractInsnNode found = PatternMatcher.builder() + .opcodes(ALOAD, ALOAD, ICONST_0, ICONST_0) + .ignoreFrames() + .ignoreLabels() + .ignoreLineNumbers() + .build() + .find(main) + // .getFirst(); // TODO returns first InsnNode of method instead of match? + .getLast().getPrevious().getPrevious().getPrevious(); + + LabelNode skip = new LabelNode(); + InsnSequence is = new InsnSequence(); + is.add(new VarInsnNode(ALOAD, 0)); + is.add(new MethodInsnNode( + INVOKESTATIC, + "ftbsc/bscv/patches/BackgroundPatch$BackgroundHook", + "shouldDrawBackground", + "(Lnet/minecraft/client/gui/screen/Screen;)Z" + )); + is.add(new JumpInsnNode(IFEQ, skip)); + is.add(new InsnNode(RETURN)); + is.add(skip); + + main.instructions.insertBefore(found, is); + } + } +}