From edfbbf0b58aada776fdde28e6f08c01e7a6454e7 Mon Sep 17 00:00:00 2001 From: Joe12o Date: Mon, 22 Jul 2013 21:04:01 -0400 Subject: [PATCH] Added transformers for the items Shadow told me about. --- .../skcraft/alicefixes/AliceTransformer.java | 24 +-- .../transformers/TransformDislocWand.java | 106 +++++++++++++ .../transformers/TransformElemAxe.java | 104 +++++++++++++ .../transformers/TransformElemShovel.java | 140 ++++++++++++++++++ .../transformers/TransformExcWand.java | 117 +++++++++++++++ .../transformers/TransformTradeWand.java | 135 +++++++++++++++++ .../transformers/TransformVajra.java | 107 +++++++++++++ 7 files changed, 724 insertions(+), 9 deletions(-) create mode 100644 alicefixes/src/com/skcraft/alicefixes/transformers/TransformDislocWand.java create mode 100644 alicefixes/src/com/skcraft/alicefixes/transformers/TransformElemAxe.java create mode 100644 alicefixes/src/com/skcraft/alicefixes/transformers/TransformElemShovel.java create mode 100644 alicefixes/src/com/skcraft/alicefixes/transformers/TransformExcWand.java create mode 100644 alicefixes/src/com/skcraft/alicefixes/transformers/TransformTradeWand.java create mode 100644 alicefixes/src/com/skcraft/alicefixes/transformers/TransformVajra.java diff --git a/alicefixes/src/com/skcraft/alicefixes/AliceTransformer.java b/alicefixes/src/com/skcraft/alicefixes/AliceTransformer.java index 3c35b7d..c61770d 100644 --- a/alicefixes/src/com/skcraft/alicefixes/AliceTransformer.java +++ b/alicefixes/src/com/skcraft/alicefixes/AliceTransformer.java @@ -2,24 +2,30 @@ import java.util.logging.Level; +import com.skcraft.alicefixes.transformers.TransformDislocWand; +import com.skcraft.alicefixes.transformers.TransformElemAxe; +import com.skcraft.alicefixes.transformers.TransformElemShovel; +import com.skcraft.alicefixes.transformers.TransformMiningLaser; +import com.skcraft.alicefixes.transformers.TransformExcWand; +import com.skcraft.alicefixes.transformers.TransformTradeWand; +import com.skcraft.alicefixes.transformers.TransformVajra; + import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.relauncher.IClassTransformer; public class AliceTransformer implements IClassTransformer { private final IClassTransformer[] transformers = { - new TransformMiningLaser()}; - //new TransformBlockBreaker() - //new TransformIC2Explosions(), - //new TransformTCExcWand(), - //new TransformTCEquWand(), - //new TransformTCFrostWand(), - //new TransformTCAxe(), - //new TransformTCShovel()}; + new TransformMiningLaser(), + new TransformExcWand(), + new TransformTradeWand(), + new TransformElemAxe(), + new TransformElemShovel(), + new TransformDislocWand(), + new TransformVajra()}; @Override public byte[] transform(String name, String transformedName, byte[] bytes) { - if(bytes == null) { return bytes; } diff --git a/alicefixes/src/com/skcraft/alicefixes/transformers/TransformDislocWand.java b/alicefixes/src/com/skcraft/alicefixes/transformers/TransformDislocWand.java new file mode 100644 index 0000000..ef782ac --- /dev/null +++ b/alicefixes/src/com/skcraft/alicefixes/transformers/TransformDislocWand.java @@ -0,0 +1,106 @@ +package com.skcraft.alicefixes.transformers; + +import static org.objectweb.asm.Opcodes.*; + +import java.lang.reflect.Method; + +import net.minecraft.entity.player.EntityPlayer; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.block.BlockBreakEvent; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; + +import com.skcraft.alicefixes.util.ObfNames; + +import cpw.mods.fml.relauncher.IClassTransformer; + +public class TransformDislocWand implements IClassTransformer { + + @Override + public byte[] transform(String name, String transformedName, byte[] bytes) { + + if(name.equals("vazkii.tinkerer.item.ItemWandDislocation")) { + ClassReader cr = new ClassReader(bytes); + ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS); + cr.accept(new WandVisitor(cw), 0); + return cw.toByteArray(); + } + return bytes; + } + + public static boolean canMine(EntityPlayer player, int x, int y, int z) { + try { + Method m = player.getClass() + .getDeclaredMethod("getBukkitEntity", new Class[] {}); + org.bukkit.entity.Entity ent = + (org.bukkit.entity.Entity)m.invoke(player); + if ((ent instanceof Player)) { + Player bukkitPlayer = (Player)ent; + org.bukkit.World bukkitWorld = bukkitPlayer.getWorld(); + BlockBreakEvent breakEv = new BlockBreakEvent( + bukkitWorld.getBlockAt(x, y, z), bukkitPlayer); + Bukkit.getPluginManager().callEvent(breakEv); + if (breakEv.isCancelled()) { + return false; + } + breakEv.setCancelled(true); + } + } + catch(Exception e) { + e.printStackTrace(); + } + return true; + } + + class WandVisitor extends ClassVisitor { + + public WandVisitor(ClassVisitor cv) { + super(ASM4, cv); + } + + @Override + public MethodVisitor visitMethod(int access, String name, String desc, + String signature, String[] exceptions) { + + if(name.equals("onItemUseFirst")) { + return new OnItemUseFirstVisitor(super.visitMethod(access, name, + desc, signature, exceptions)); + } + return super.visitMethod(access, name, desc, signature, + exceptions); + } + } + + class OnItemUseFirstVisitor extends MethodVisitor { + + public OnItemUseFirstVisitor(MethodVisitor mv) { + super(ASM4, mv); + } + + @Override + public void visitCode() { + Label l0 = new Label(); + mv.visitLabel(l0); + mv.visitVarInsn(ALOAD, 2); + mv.visitVarInsn(ILOAD, 4); + mv.visitVarInsn(ILOAD, 5); + mv.visitVarInsn(ILOAD, 6); + mv.visitMethodInsn(INVOKESTATIC, "com/skcraft/alicefixes/transformers/TransformDislocWand", + "canMine", "(L" + ObfNames.ENTITY_PLAYER +";III)Z"); + Label l1 = new Label(); + mv.visitJumpInsn(IFNE, l1); + Label l2 = new Label(); + mv.visitLabel(l2); + mv.visitInsn(ICONST_1); + mv.visitInsn(IRETURN); + mv.visitLabel(l1); + mv.visitCode(); + } + } + +} diff --git a/alicefixes/src/com/skcraft/alicefixes/transformers/TransformElemAxe.java b/alicefixes/src/com/skcraft/alicefixes/transformers/TransformElemAxe.java new file mode 100644 index 0000000..d70f2bd --- /dev/null +++ b/alicefixes/src/com/skcraft/alicefixes/transformers/TransformElemAxe.java @@ -0,0 +1,104 @@ +package com.skcraft.alicefixes.transformers; + +import static org.objectweb.asm.Opcodes.*; + +import java.lang.reflect.Method; + +import net.minecraft.entity.player.EntityPlayer; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.block.BlockBreakEvent; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; + +import com.skcraft.alicefixes.util.ObfNames; + +import cpw.mods.fml.relauncher.IClassTransformer; + +public class TransformElemAxe implements IClassTransformer { + + @Override + public byte[] transform(String name, String transformedName, byte[] bytes) { + + if(name.equals("thaumcraft.common.items.equipment.ItemElementalAxe")) { + ClassReader cr = new ClassReader(bytes); + ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS); + cr.accept(new AxeVisitor(cw), 0); + return cw.toByteArray(); + } + return bytes; + } + + public static boolean canMine(EntityPlayer player, int x, int y, int z) { + try { + Method m = player.getClass() + .getDeclaredMethod("getBukkitEntity", new Class[] {}); + org.bukkit.entity.Entity ent = + (org.bukkit.entity.Entity)m.invoke(player); + if ((ent instanceof Player)) { + Player bukkitPlayer = (Player)ent; + org.bukkit.World bukkitWorld = bukkitPlayer.getWorld(); + BlockBreakEvent breakEv = new BlockBreakEvent( + bukkitWorld.getBlockAt(x, y, z), bukkitPlayer); + Bukkit.getPluginManager().callEvent(breakEv); + if (breakEv.isCancelled()) { + return false; + } + breakEv.setCancelled(true); + } + } + catch(Exception e) { + e.printStackTrace(); + } + return true; + } + + class AxeVisitor extends ClassVisitor { + + public AxeVisitor(ClassVisitor cv) { + super(ASM4, cv); + } + + @Override + public MethodVisitor visitMethod(int access, String name, String desc, + String signature, String[] exceptions) { + + if(name.equals("breakFurthestBlock")) { + return new BreakFurthestBlockVisitor(super.visitMethod(access, name, + desc, signature, exceptions)); + } + return super.visitMethod(access, name, desc, signature, + exceptions); + } + } + + class BreakFurthestBlockVisitor extends MethodVisitor { + + public BreakFurthestBlockVisitor(MethodVisitor mv) { + super(ASM4, mv); + } + + @Override + public void visitCode() { + Label l0 = new Label(); + mv.visitLabel(l0); + mv.visitVarInsn(ALOAD, 7); + mv.visitVarInsn(ILOAD, 3); + mv.visitVarInsn(ILOAD, 4); + mv.visitVarInsn(ILOAD, 5); + mv.visitMethodInsn(INVOKESTATIC, "com/skcraft/alicefixes/transformers/TransformElemAxe", + "canMine", "(L" + ObfNames.ENTITY_PLAYER +";III)Z"); + Label l1 = new Label(); + mv.visitJumpInsn(IFNE, l1); + Label l2 = new Label(); + mv.visitLabel(l2); + mv.visitInsn(RETURN); + mv.visitLabel(l1); + mv.visitCode(); + } + } +} diff --git a/alicefixes/src/com/skcraft/alicefixes/transformers/TransformElemShovel.java b/alicefixes/src/com/skcraft/alicefixes/transformers/TransformElemShovel.java new file mode 100644 index 0000000..52274ae --- /dev/null +++ b/alicefixes/src/com/skcraft/alicefixes/transformers/TransformElemShovel.java @@ -0,0 +1,140 @@ +package com.skcraft.alicefixes.transformers; + +import static org.objectweb.asm.Opcodes.*; + +import java.lang.reflect.Method; + +import net.minecraft.entity.EntityLiving; +import net.minecraft.entity.player.EntityPlayer; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.block.BlockBreakEvent; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; + +import com.skcraft.alicefixes.util.ObfNames; + +import cpw.mods.fml.relauncher.IClassTransformer; + +public class TransformElemShovel implements IClassTransformer { + + @Override + public byte[] transform(String name, String transformedName, byte[] bytes) { + + if(name.equals("thaumcraft.common.items.equipment.ItemElementalShovel")) { + ClassReader cr = new ClassReader(bytes); + ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS); + cr.accept(new ShovelVisitor(cw), 0); + return cw.toByteArray(); + } + return bytes; + } + + public static boolean canMine(EntityLiving entity, int x, int y, int z) { + if(entity instanceof EntityPlayer) { + EntityPlayer player = (EntityPlayer)entity; + try { + Method m = player.getClass() + .getDeclaredMethod("getBukkitEntity", new Class[] {}); + org.bukkit.entity.Entity ent = + (org.bukkit.entity.Entity)m.invoke(player); + if ((ent instanceof Player)) { + Player bukkitPlayer = (Player)ent; + org.bukkit.World bukkitWorld = bukkitPlayer.getWorld(); + BlockBreakEvent breakEv = new BlockBreakEvent( + bukkitWorld.getBlockAt(x, y, z), bukkitPlayer); + Bukkit.getPluginManager().callEvent(breakEv); + if (breakEv.isCancelled()) { + return false; + } + breakEv.setCancelled(true); + } + } + catch(Exception e) { + e.printStackTrace(); + } + } + return true; + } + + class ShovelVisitor extends ClassVisitor { + + public ShovelVisitor(ClassVisitor cv) { + super(ASM4, cv); + } + + @Override + public MethodVisitor visitMethod(int access, String name, String desc, + String signature, String[] exceptions) { + + if(name.equals("func_77648_a")) { + return new OnItemUseVisitor(super.visitMethod(access, name, + desc, signature, exceptions)); + } else if(name.equals("func_77660_a")) { + return new OnBlockDestroyedVisitor(super.visitMethod(access, name, + desc, signature, exceptions)); + } + return super.visitMethod(access, name, desc, signature, + exceptions); + } + } + + class OnItemUseVisitor extends MethodVisitor { + + public OnItemUseVisitor(MethodVisitor mv) { + super(ASM4, mv); + } + + @Override + public void visitCode() { + Label l0 = new Label(); + mv.visitLabel(l0); + mv.visitVarInsn(ALOAD, 2); + mv.visitVarInsn(ILOAD, 4); + mv.visitVarInsn(ILOAD, 5); + mv.visitVarInsn(ILOAD, 6); + mv.visitMethodInsn(INVOKESTATIC, "com/skcraft/alicefixes/transformers/TransformElemShovel", + "canMine", "(L" + ObfNames.ENTITY_LIVING +";III)Z"); + Label l1 = new Label(); + mv.visitJumpInsn(IFNE, l1); + Label l2 = new Label(); + mv.visitLabel(l2); + mv.visitInsn(ICONST_0); + mv.visitInsn(IRETURN); + mv.visitLabel(l1); + mv.visitCode(); + } + } + + class OnBlockDestroyedVisitor extends MethodVisitor { + + public OnBlockDestroyedVisitor(MethodVisitor mv) { + super(ASM4, mv); + } + + @Override + public void visitCode() { + Label l0 = new Label(); + mv.visitLabel(l0); + mv.visitVarInsn(ALOAD, 7); + mv.visitVarInsn(ILOAD, 4); + mv.visitVarInsn(ILOAD, 5); + mv.visitVarInsn(ILOAD, 6); + mv.visitMethodInsn(INVOKESTATIC, "com/skcraft/alicefixes/transformers/TransformElemShovel", + "canMine", "(L" + ObfNames.ENTITY_LIVING + ";III)Z"); + Label l1 = new Label(); + mv.visitJumpInsn(IFNE, l1); + Label l2 = new Label(); + mv.visitLabel(l2); + mv.visitInsn(ICONST_0); + mv.visitInsn(IRETURN); + mv.visitLabel(l1); + mv.visitCode(); + } + } + +} diff --git a/alicefixes/src/com/skcraft/alicefixes/transformers/TransformExcWand.java b/alicefixes/src/com/skcraft/alicefixes/transformers/TransformExcWand.java new file mode 100644 index 0000000..8297f42 --- /dev/null +++ b/alicefixes/src/com/skcraft/alicefixes/transformers/TransformExcWand.java @@ -0,0 +1,117 @@ +package com.skcraft.alicefixes.transformers; + +import static org.objectweb.asm.Opcodes.*; + +import java.lang.reflect.Method; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.util.MovingObjectPosition; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.block.BlockBreakEvent; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; + +import com.skcraft.alicefixes.util.ObfNames; + +import cpw.mods.fml.relauncher.IClassTransformer; + +public class TransformExcWand implements IClassTransformer { + + @Override + public byte[] transform(String name, String transformedName, byte[] bytes) { + if(name.equals("thaumcraft.common.items.wands.ItemWandExcavation")) { + ClassReader cr = new ClassReader(bytes); + ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS); + cr.accept(new WandVisitor(cw), 0); + return cw.toByteArray(); + } + return bytes; + } + + public static boolean canMine(EntityPlayer player, MovingObjectPosition pos) { + if(pos != null) { + int xPos = pos.blockX; + int yPos = pos.blockY; + int zPos = pos.blockZ; + + try { + Method m = player.getClass() + .getDeclaredMethod("getBukkitEntity", new Class[] {}); + org.bukkit.entity.Entity ent = + (org.bukkit.entity.Entity)m.invoke(player); + if ((ent instanceof Player)) { + Player bukkitPlayer = (Player)ent; + org.bukkit.World bukkitWorld = bukkitPlayer.getWorld(); + BlockBreakEvent breakEv = new BlockBreakEvent( + bukkitWorld.getBlockAt(xPos, yPos, zPos), bukkitPlayer); + Bukkit.getPluginManager().callEvent(breakEv); + if (breakEv.isCancelled()) { + return false; + } + + breakEv.setCancelled(true); + } + } + catch(Exception e) { + e.printStackTrace(); + } + } + + return true; + } + + class WandVisitor extends ClassVisitor { + + public WandVisitor(ClassVisitor cv) { + super(ASM4, cv); + } + + @Override + public MethodVisitor visitMethod(int access, String name, String desc, + String signature, String[] exceptions) { + + if(name.equals("onUsingItemTick")) { + return new OnUsingItemTickVisitor(super.visitMethod(access, name, desc, + signature, exceptions)); + } + return super.visitMethod(access, name, desc, signature, + exceptions); + } + } + + class OnUsingItemTickVisitor extends MethodVisitor { + + public OnUsingItemTickVisitor(MethodVisitor mv) { + super(ASM4, mv); + } + + @Override + public void visitCode() { + Label l0 = new Label(); + mv.visitLabel(l0); + mv.visitVarInsn(ALOAD, 2); + mv.visitVarInsn(ALOAD, 2); + mv.visitFieldInsn(GETFIELD, ObfNames.ENTITY_PLAYER, ObfNames.WORLD_OBJ, "L" + + ObfNames.WORLD + ";"); + mv.visitVarInsn(ALOAD, 2); + mv.visitInsn(ICONST_0); + mv.visitMethodInsn(INVOKESTATIC, "thaumcraft/common/lib/Utils", + "getTargetBlock", "(L" + ObfNames.WORLD + ";L" + ObfNames.ENTITY_PLAYER + + ";Z)L" + ObfNames.MOV_OBJ_POS + ";"); + mv.visitMethodInsn(INVOKESTATIC, "com/skcraft/alicefixes/transformers/TransformExcWand", + "canMine", "(L" + ObfNames.ENTITY_PLAYER + ";L" + ObfNames.MOV_OBJ_POS + ";)Z"); + Label l1 = new Label(); + mv.visitJumpInsn(IFNE, l1); + Label l2 = new Label(); + mv.visitLabel(l2); + mv.visitInsn(RETURN); + mv.visitLabel(l1); + mv.visitCode(); + } + } +} diff --git a/alicefixes/src/com/skcraft/alicefixes/transformers/TransformTradeWand.java b/alicefixes/src/com/skcraft/alicefixes/transformers/TransformTradeWand.java new file mode 100644 index 0000000..74dfff4 --- /dev/null +++ b/alicefixes/src/com/skcraft/alicefixes/transformers/TransformTradeWand.java @@ -0,0 +1,135 @@ +package com.skcraft.alicefixes.transformers; + +import static org.objectweb.asm.Opcodes.*; + +import java.lang.reflect.Method; + +import net.minecraft.entity.player.EntityPlayer; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.block.BlockBreakEvent; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; + +import com.skcraft.alicefixes.util.ObfNames; + +import cpw.mods.fml.relauncher.IClassTransformer; + +public class TransformTradeWand implements IClassTransformer { + + @Override + public byte[] transform(String name, String transformedName, byte[] bytes) { + + if(name.equals("thaumcraft.common.items.wands.ItemWandTrade")) { + ClassReader cr = new ClassReader(bytes); + ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS); + cr.accept(new WandVisitor(cw), 0); + return cw.toByteArray(); + } + return bytes; + } + + public static boolean canMine(EntityPlayer player, int x, int y, int z) { + try { + Method m = player.getClass() + .getDeclaredMethod("getBukkitEntity", new Class[] {}); + org.bukkit.entity.Entity ent = + (org.bukkit.entity.Entity)m.invoke(player); + if ((ent instanceof Player)) { + Player bukkitPlayer = (Player)ent; + org.bukkit.World bukkitWorld = bukkitPlayer.getWorld(); + BlockBreakEvent breakEv = new BlockBreakEvent( + bukkitWorld.getBlockAt(x, y, z), bukkitPlayer); + Bukkit.getPluginManager().callEvent(breakEv); + if (breakEv.isCancelled()) { + return false; + } + breakEv.setCancelled(true); + } + } + catch(Exception e) { + e.printStackTrace(); + } + return true; + } + + class WandVisitor extends ClassVisitor { + + public WandVisitor(ClassVisitor cv) { + super(ASM4, cv); + } + + @Override + public MethodVisitor visitMethod(int access, String name, String desc, + String signature, String[] exceptions) { + + if(name.equals("onItemUseFirst")) { + return new OnItemUseFirstVisitor(super.visitMethod(access, name, + desc, signature, exceptions)); + } else if(name.equals("onBlockStartBreak")) { + return new OnBlockStartBreakVisitor(super.visitMethod(access, name, + desc, signature, exceptions)); + } + return super.visitMethod(access, name, desc, signature, + exceptions); + } + } + + class OnItemUseFirstVisitor extends MethodVisitor { + + public OnItemUseFirstVisitor(MethodVisitor mv) { + super(ASM4, mv); + } + + @Override + public void visitCode() { + Label l0 = new Label(); + mv.visitLabel(l0); + mv.visitVarInsn(ALOAD, 2); + mv.visitVarInsn(ILOAD, 4); + mv.visitVarInsn(ILOAD, 5); + mv.visitVarInsn(ILOAD, 6); + mv.visitMethodInsn(INVOKESTATIC, "com/skcraft/alicefixes/transformers/TransformTradeWand", + "canMine", "(L" + ObfNames.ENTITY_PLAYER + ";III)Z"); + Label l1 = new Label(); + mv.visitJumpInsn(IFNE, l1); + Label l2 = new Label(); + mv.visitLabel(l2); + mv.visitInsn(ICONST_0); + mv.visitInsn(IRETURN); + mv.visitLabel(l1); + mv.visitCode(); + } + } + + class OnBlockStartBreakVisitor extends MethodVisitor { + + public OnBlockStartBreakVisitor(MethodVisitor mv) { + super(ASM4, mv); + } + + @Override + public void visitCode() { + Label l0 = new Label(); + mv.visitLabel(l0); + mv.visitVarInsn(ALOAD, 5); + mv.visitVarInsn(ILOAD, 2); + mv.visitVarInsn(ILOAD, 3); + mv.visitVarInsn(ILOAD, 4); + mv.visitMethodInsn(INVOKESTATIC, "com/skcraft/alicefixes/transformers/TransformTradeWand", + "canMine", "(L" + ObfNames.ENTITY_PLAYER + ";III)Z"); + Label l1 = new Label(); + mv.visitJumpInsn(IFNE, l1); + Label l2 = new Label(); + mv.visitLabel(l2); + mv.visitInsn(ICONST_0); + mv.visitInsn(IRETURN); + mv.visitLabel(l1); + mv.visitCode(); + } + } +} diff --git a/alicefixes/src/com/skcraft/alicefixes/transformers/TransformVajra.java b/alicefixes/src/com/skcraft/alicefixes/transformers/TransformVajra.java new file mode 100644 index 0000000..4656591 --- /dev/null +++ b/alicefixes/src/com/skcraft/alicefixes/transformers/TransformVajra.java @@ -0,0 +1,107 @@ +package com.skcraft.alicefixes.transformers; + +import static org.objectweb.asm.Opcodes.*; + +import java.lang.reflect.Method; + +import net.minecraft.entity.player.EntityPlayer; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.block.BlockBreakEvent; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; + +import com.skcraft.alicefixes.util.ObfNames; + +import cpw.mods.fml.relauncher.IClassTransformer; + +public class TransformVajra implements IClassTransformer { + + @Override + public byte[] transform(String name, String transformedName, byte[] bytes) { + + if(name.equals("gravisuite.ItemVajra")) { + ClassReader cr = new ClassReader(bytes); + ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS); + cr.accept(new VajraVisitor(cw), 0); + return cw.toByteArray(); + } + return bytes; + } + + public static boolean canMine(EntityPlayer player, int x, int y, int z) { + try { + Method m = player.getClass() + .getDeclaredMethod("getBukkitEntity", new Class[] {}); + org.bukkit.entity.Entity ent = + (org.bukkit.entity.Entity)m.invoke(player); + if ((ent instanceof Player)) { + Player bukkitPlayer = (Player)ent; + org.bukkit.World bukkitWorld = bukkitPlayer.getWorld(); + BlockBreakEvent breakEv = new BlockBreakEvent( + bukkitWorld.getBlockAt(x, y, z), bukkitPlayer); + Bukkit.getPluginManager().callEvent(breakEv); + if (breakEv.isCancelled()) { + return false; + } + breakEv.setCancelled(true); + } + } + catch(Exception e) { + e.printStackTrace(); + } + return true; + } + + class VajraVisitor extends ClassVisitor { + + public VajraVisitor(ClassVisitor cv) { + super(ASM4, cv); + } + + @Override + public MethodVisitor visitMethod(int access, String name, String desc, + String signature, String[] exceptions) { + + if(name.equals("a") && desc.equals("(L" + ObfNames.ITEM_STACK + ";L" + + ObfNames.ENTITY_PLAYER + ";L" + ObfNames.WORLD + ";IIIIFFF)Z")) { + return new OnItemUseFirstVisitor(super.visitMethod(access, name, + desc, signature, exceptions)); + } + return super.visitMethod(access, name, desc, signature, + exceptions); + } + } + + class OnItemUseFirstVisitor extends MethodVisitor { + + public OnItemUseFirstVisitor(MethodVisitor mv) { + super(ASM4, mv); + } + + @Override + public void visitCode() { + Label l0 = new Label(); + mv.visitLabel(l0); + mv.visitVarInsn(ALOAD, 2); + mv.visitVarInsn(ILOAD, 4); + mv.visitVarInsn(ILOAD, 5); + mv.visitVarInsn(ILOAD, 6); + mv.visitMethodInsn(INVOKESTATIC, "com/skcraft/alicefixes/transformers/TransformVajra", + "canMine", "(L" + ObfNames.ENTITY_PLAYER +";III)Z"); + Label l1 = new Label(); + mv.visitJumpInsn(IFNE, l1); + Label l2 = new Label(); + mv.visitLabel(l2); + mv.visitInsn(ICONST_0); + mv.visitInsn(IRETURN); + mv.visitLabel(l1); + mv.visitCode(); + } + } + +}