From ac4a18a428d32fe4af9606672f66d713c0965de1 Mon Sep 17 00:00:00 2001 From: alemi Date: Sun, 19 Feb 2023 20:20:47 +0100 Subject: [PATCH] chore: some boilerplate for boat patch --- .../java/ftbsc/bscv/events/BoatEvent.java | 12 ++++ .../java/ftbsc/bscv/patches/BoatPatch.java | 59 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 src/main/java/ftbsc/bscv/events/BoatEvent.java create mode 100644 src/main/java/ftbsc/bscv/patches/BoatPatch.java diff --git a/src/main/java/ftbsc/bscv/events/BoatEvent.java b/src/main/java/ftbsc/bscv/events/BoatEvent.java new file mode 100644 index 0000000..32eeb37 --- /dev/null +++ b/src/main/java/ftbsc/bscv/events/BoatEvent.java @@ -0,0 +1,12 @@ +package ftbsc.bscv.events; + +import net.minecraftforge.eventbus.api.Cancelable; +import net.minecraftforge.eventbus.api.Event; + +public class BoatEvent { + + @Cancelable + public class Control extends Event { + public Control() {} + } +} diff --git a/src/main/java/ftbsc/bscv/patches/BoatPatch.java b/src/main/java/ftbsc/bscv/patches/BoatPatch.java new file mode 100644 index 0000000..3d7f13d --- /dev/null +++ b/src/main/java/ftbsc/bscv/patches/BoatPatch.java @@ -0,0 +1,59 @@ +package ftbsc.bscv.patches; + +import net.minecraft.network.IPacket; +import net.minecraftforge.common.MinecraftForge; + +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.AbstractInsnNode; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.InsnNode; +import org.objectweb.asm.tree.JumpInsnNode; +import org.objectweb.asm.tree.LabelNode; +import org.objectweb.asm.tree.MethodInsnNode; +import org.objectweb.asm.tree.MethodNode; +import org.objectweb.asm.tree.VarInsnNode; + +import ftbsc.bscv.events.BoatEvent; +import ftbsc.bscv.events.PacketEvent; +import ftbsc.lll.IInjector; +import ftbsc.lll.tools.InsnSequence; +import ftbsc.lll.tools.PatternMatcher; + +public class BoatPatch { + + public static class BoatHook { + public static boolean boatControl(IPacket pkt) { + return MinecraftForge.EVENT_BUS.post(new BoatEvent.Control()); + } + + public static boolean pktOut(IPacket pkt) { + return MinecraftForge.EVENT_BUS.post(new PacketEvent(pkt, true)); + } + } + + public static class BoatControlOverride implements IInjector, Opcodes { + public String name() { return "BoatControlOverride"; } + public String reason() { return "add hook to cancel vanilla boat controls"; } + public String targetClass() { return "net.minecraft.entity.item.BoatEntity"; } + public String methodName() { return "func_184443_x"; } // void controlBoat() + public String methodDesc() { return "()V"; } + + public void inject(ClassNode clazz, MethodNode main) { + // Hook at method start + LabelNode skip = new LabelNode(); + InsnSequence is = new InsnSequence(); + is.add(new VarInsnNode(ALOAD, 2)); + is.add(new MethodInsnNode( + INVOKESTATIC, + "ftbsc/bscv/patches/BoatPatch$BoatHook", + "boatControl", + "(Lnet/minecraft/network/IPacket;)Z" + )); + is.add(new JumpInsnNode(IFEQ, skip)); + is.add(new InsnNode(RETURN)); + is.add(skip); + + main.instructions.insert(is); + } + } +}