diff options
Diffstat (limited to 'src/main/java/moe/nea/kowalski/init/EventClassTransformer.java')
-rw-r--r-- | src/main/java/moe/nea/kowalski/init/EventClassTransformer.java | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/main/java/moe/nea/kowalski/init/EventClassTransformer.java b/src/main/java/moe/nea/kowalski/init/EventClassTransformer.java new file mode 100644 index 0000000..a80a5b5 --- /dev/null +++ b/src/main/java/moe/nea/kowalski/init/EventClassTransformer.java @@ -0,0 +1,47 @@ +package moe.nea.kowalski.init; + +import net.minecraft.launchwrapper.IClassTransformer; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.*; + +import java.util.ListIterator; + +public class EventClassTransformer implements IClassTransformer { + @Override + public byte[] transform(String name, String transformedName, byte[] basicClass) { + if (!"net.minecraftforge.fml.common.eventhandler.Event".equals(name)) + return basicClass; + ClassNode classNode = new ClassNode(); + ClassReader classReader = new ClassReader(basicClass); + classReader.accept(classNode, 0); + for (MethodNode method : classNode.methods) { + if (method.name.equals("setCanceled")) { + transformCancelMethod(method); + } + } + ClassWriter writer = new ClassWriter(0); + classNode.accept(writer); + return writer.toByteArray(); + } + + private void transformCancelMethod(MethodNode method) { + ListIterator<AbstractInsnNode> iterator = + method.instructions.iterator(); + while (iterator.hasNext()) { + AbstractInsnNode insn = iterator.next(); + if (insn.getOpcode() != Opcodes.PUTFIELD) { + continue; + } + method.instructions.insert(insn, buildCallKowalski()); + } + } + + private InsnList buildCallKowalski() { + InsnList insnList = new InsnList(); + insnList.add(new VarInsnNode(Opcodes.ALOAD, 0)); + insnList.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "moe/nea/kowalski/Kowalski", "eventIsBeingCancelled", "(Lnet/minecraftforge/fml/common/eventhandler/Event;)V", false)); + return insnList; + } +} |