feat: allow canceling background, fix key release

This commit is contained in:
əlemi 2023-03-11 22:42:28 +01:00
parent 4238d733e8
commit 001b27ceca
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 96 additions and 8 deletions

View file

@ -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<Boolean> 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?
}
}
}

View file

@ -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);
}
}
}