diff --git a/alicefixes/src/com/skcraft/alicefixes/ObfNames.java b/alicefixes/src/com/skcraft/alicefixes/ObfNames.java deleted file mode 100644 index a3fef39..0000000 --- a/alicefixes/src/com/skcraft/alicefixes/ObfNames.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.skcraft.alicefixes; - -public class ObfNames { - - public static final String ENTITY_LIVING = "ng"; - public static final String ENTITY = "mp"; - public static final String TILE_ENTITY = "aqp"; - -} diff --git a/alicefixes/src/com/skcraft/alicefixes/TransformBlockBreaker.java b/alicefixes/src/com/skcraft/alicefixes/TransformBlockBreaker.java deleted file mode 100644 index 54a461d..0000000 --- a/alicefixes/src/com/skcraft/alicefixes/TransformBlockBreaker.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.skcraft.alicefixes; - -import static org.objectweb.asm.Opcodes.*; - -import java.util.Iterator; -import java.util.logging.Level; - -import net.minecraft.tileentity.TileEntity; - -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.FieldInsnNode; -import org.objectweb.asm.tree.InsnList; -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 cpw.mods.fml.common.FMLLog; -import cpw.mods.fml.relauncher.IClassTransformer; - -// THIS MAY NOT WORK WHEN THE NEW RP COMES OUT (if it ever does...) -public class TransformBlockBreaker implements IClassTransformer { - - @Override - public byte[] transform(String name, String transformedName, byte[] bytes) { - if(name.equals("com.eloraam.redpower.machine.TileBreaker")) { - return handleBreakerTransform(bytes); - } - return bytes; - } - - private byte[] handleBreakerTransform(byte[] bytes) { - - ClassNode classNode = new ClassNode(); - ClassReader classReader = new ClassReader(bytes); - classReader.accept(classNode, 0); - - Iterator methods = classNode.methods.iterator(); - while(methods.hasNext()) { - MethodNode method = methods.next(); - if(method.name.equals("onBlockNeighborChange")) { - LabelNode l0 = new LabelNode(); - LabelNode l1 = new LabelNode(); - LabelNode l2 = new LabelNode(); - InsnList toInject = new InsnList(); - toInject.add(l0); - toInject.add(new VarInsnNode(ALOAD, 0)); //this - toInject.add(new VarInsnNode(ALOAD, 0)); //this - toInject.add(new FieldInsnNode(GETFIELD, "com/eloraam/redpower/machine/TileBreaker", "Rotation", "I")); - toInject.add(new MethodInsnNode(INVOKESTATIC, - "com/skcraft/alicefixes/TransformBlockBreaker", - "canMine", - "(L" + ObfNames.TILE_ENTITY + ";I)Z")); //Invoke canMine() in this class - toInject.add(new JumpInsnNode(IFNE, l1)); //if statement - toInject.add(l2); - toInject.add(new InsnNode(RETURN)); - toInject.add(l1); - - method.instructions.insertBefore(method.instructions.getFirst(), toInject); //insert before first instruction - FMLLog.log(Level.INFO, "Block Breaker successfully patched!"); - break; - } - } - ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); - classNode.accept(writer); - return writer.toByteArray(); - } - - public static boolean canMine(TileEntity tile, int rotation) { - - CoordHelper facingCoords = new CoordHelper(tile.xCoord, tile.yCoord, tile.zCoord); - facingCoords.addFacingAsOffset(rotation); - - int id = tile.worldObj.getBlockId(facingCoords.x, facingCoords.y, facingCoords.z); - - for(int i = 0; i < BreakerBlacklist.forbiddenIds.length; i++) { - if(id == BreakerBlacklist.forbiddenIds[i]) { - return false; - } - } - return true; - } -} diff --git a/alicefixes/src/com/skcraft/alicefixes/TransformMiningLaser.java b/alicefixes/src/com/skcraft/alicefixes/TransformMiningLaser.java deleted file mode 100644 index 6e96cda..0000000 --- a/alicefixes/src/com/skcraft/alicefixes/TransformMiningLaser.java +++ /dev/null @@ -1,119 +0,0 @@ -package com.skcraft.alicefixes; - -import static org.objectweb.asm.Opcodes.*; - -import java.lang.reflect.Method; -import java.util.Iterator; -import java.util.logging.Level; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.event.block.BlockBreakEvent; -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.FieldInsnNode; -import org.objectweb.asm.tree.InsnList; -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 net.minecraft.entity.Entity; -import net.minecraft.entity.EntityLiving; -import net.minecraft.util.MovingObjectPosition; -import net.minecraft.util.Vec3; -import cpw.mods.fml.common.FMLLog; -import cpw.mods.fml.relauncher.IClassTransformer; - -public class TransformMiningLaser implements IClassTransformer{ - - private final String LASER_CLASS_NAME = "ic2.core.item.tool.EntityMiningLaser"; - private final String MINE_METHOD_NAME = "canMine"; - - @Override - public byte[] transform(String name, String transformedName, byte[] bytes) { - if(name.equals(LASER_CLASS_NAME)) { - return handleLaserTransform(bytes); - } - return bytes; - } - - private byte[] handleLaserTransform(byte[] bytes) { - - ClassNode classNode = new ClassNode(); - ClassReader classReader = new ClassReader(bytes); - classReader.accept(classNode, 0); - - Iterator methods = classNode.methods.iterator(); - while(methods.hasNext()) { - MethodNode method = methods.next(); - if(method.name.equals(MINE_METHOD_NAME)) { - LabelNode l0 = new LabelNode(); - LabelNode l1 = new LabelNode(); - LabelNode l2 = new LabelNode(); - InsnList toInject = new InsnList(); - toInject.add(l0); - toInject.add(new VarInsnNode(ALOAD, 0)); //"this" - toInject.add(new VarInsnNode(ALOAD, 0)); //"this" - toInject.add(new FieldInsnNode(GETFIELD, "ic2/core/item/tool/EntityMiningLaser", "owner", - "L" + ObfNames.ENTITY_LIVING + ";")); //owner of laser - //invokes canMine() in this class - toInject.add(new MethodInsnNode(INVOKESTATIC, - "com/skcraft/alicefixes/TransformMiningLaser", - "canMine", - "(L" + ObfNames.ENTITY + ";L" + ObfNames.ENTITY_LIVING + ";)Z")); - toInject.add(new JumpInsnNode(IFNE, l1)); //if statement - toInject.add(l2); - toInject.add(new InsnNode(ICONST_0)); //false - toInject.add(new InsnNode(IRETURN)); //return - toInject.add(l1); - //Insert these instructions at the start of the method's - method.instructions.insertBefore(method.instructions.getFirst(), toInject); - - FMLLog.log(Level.INFO, "Mining laser successfully patched!"); - break; - } - } - - ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); - classNode.accept(writer); - return writer.toByteArray(); - } - - public static boolean canMine(Entity laser, EntityLiving owner) { - Vec3 currentPos = Vec3.createVectorHelper(laser.posX, laser.posY, laser.posZ); - Vec3 heading = Vec3.createVectorHelper(laser.posX + laser.motionX, laser.posY + laser.motionY, laser.posZ + laser.motionZ); - MovingObjectPosition pos = laser.worldObj.rayTraceBlocks_do_do(currentPos, heading, false, true); - - if(pos != null) { - int xPos = pos.blockX; - int yPos = pos.blockY; - int zPos = pos.blockZ; - - try { - Method m = owner.getClass().getDeclaredMethod("getBukkitEntity", new Class[] {}); - org.bukkit.entity.Entity ent = (org.bukkit.entity.Entity)m.invoke(owner); - if ((ent instanceof Player)) { - Player player = (Player)ent; - org.bukkit.World bukkitWorld = player.getWorld(); - BlockBreakEvent breakEv = new BlockBreakEvent(bukkitWorld.getBlockAt(xPos, yPos, zPos), player); - Bukkit.getPluginManager().callEvent(breakEv); - if (breakEv.isCancelled()) { - laser.setDead(); - return false; - } - - breakEv.setCancelled(true); - } - } - catch(Exception e) { - e.printStackTrace(); - } - } - - return true; - } -} diff --git a/alicefixes/src/com/skcraft/alicefixes/transformers/TransformBlockBreaker.java b/alicefixes/src/com/skcraft/alicefixes/transformers/TransformBlockBreaker.java new file mode 100644 index 0000000..2c2a8b5 --- /dev/null +++ b/alicefixes/src/com/skcraft/alicefixes/transformers/TransformBlockBreaker.java @@ -0,0 +1,103 @@ +package com.skcraft.alicefixes.transformers; + +import static org.objectweb.asm.Opcodes.*; + +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.BreakerBlacklist; +import com.skcraft.alicefixes.util.CoordHelper; +import com.skcraft.alicefixes.util.ObfNames; + +import net.minecraft.tileentity.TileEntity; +import cpw.mods.fml.relauncher.IClassTransformer; + +// THIS MAY NOT WORK ANYMORE! +public class TransformBlockBreaker implements IClassTransformer { + + @Override + public byte[] transform(String name, String transformedName, byte[] bytes) { + if(name.equals("com.eloraam.redpower.machine.TileBreaker")) { + return handleBreakerTransform(bytes); + } + return bytes; + } + + private byte[] handleBreakerTransform(byte[] bytes) { + ClassReader cr = new ClassReader(bytes); + ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS); + + cr.accept(new TileBreakerVisitor(cw), 0); + return cw.toByteArray(); + } + + public static boolean canMine(TileEntity tile, int rotation) { + + CoordHelper facingCoords = new CoordHelper(tile.xCoord, tile.yCoord, + tile.zCoord); + facingCoords.addFacingAsOffset(rotation); + + int id = tile.worldObj.getBlockId(facingCoords.x, facingCoords.y, + facingCoords.z); + + for(int i = 0; i < BreakerBlacklist.forbiddenIds.length; i++) { + if(id == BreakerBlacklist.forbiddenIds[i]) { + return false; + } + } + return true; + } + + class TileBreakerVisitor extends ClassVisitor { + + public TileBreakerVisitor(ClassVisitor cv) { + super(ASM4, cv); + } + + @Override + public MethodVisitor visitMethod(int access, String name, String desc, + String signature, String[] exceptions) { + + if(name.equals("onBlockNeighborChange")) { + return new OnNeighborChangeVisitor(super.visitMethod(access, + name, desc, signature, exceptions)); + } + return super.visitMethod(access, name, desc, signature, + exceptions); + } + } + + class OnNeighborChangeVisitor extends MethodVisitor { + + public OnNeighborChangeVisitor(MethodVisitor mv) { + super(ASM4, mv); + } + + @Override + public void visitCode() { + Label l0 = new Label(); + mv.visitLabel(l0); + mv.visitVarInsn(ALOAD, 0); + mv.visitVarInsn(ALOAD, 0); + mv.visitFieldInsn(GETFIELD, + "com/eloraam/redpower/machine/TileBreaker", + "Rotation", + "I"); + mv.visitMethodInsn(INVOKESTATIC, + "com/skcraft/alicefixes/transformers/TransformBlockBreaker", + "canMine", + "(L" + ObfNames.TILE_ENTITY + ";I)Z"); + Label l1 = new Label(); + mv.visitJumpInsn(IFEQ, 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/TransformMiningLaser.java b/alicefixes/src/com/skcraft/alicefixes/transformers/TransformMiningLaser.java new file mode 100644 index 0000000..351e6e7 --- /dev/null +++ b/alicefixes/src/com/skcraft/alicefixes/transformers/TransformMiningLaser.java @@ -0,0 +1,130 @@ +package com.skcraft.alicefixes.transformers; + +import static org.objectweb.asm.Opcodes.*; + +import java.lang.reflect.Method; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLiving; +import net.minecraft.util.MovingObjectPosition; +import net.minecraft.util.Vec3; + +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 TransformMiningLaser implements IClassTransformer { + + @Override + public byte[] transform(String name, String transformedName, byte[] bytes) { + if(name.equals("ic2.core.item.tool.EntityMiningLaser")) { + return handleLaserTransform(bytes); + } + return bytes; + } + + private byte[] handleLaserTransform(byte[] bytes) { + ClassReader cr = new ClassReader(bytes); + ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS); + + cr.accept(new EntityLaserVisitor(cw), 0); + return cw.toByteArray(); + } + + public static boolean canMine(Entity laser, EntityLiving owner) { + Vec3 currentPos = Vec3.createVectorHelper(laser.posX, laser.posY, + laser.posZ); + Vec3 heading = Vec3.createVectorHelper(laser.posX + laser.motionX, + laser.posY + laser.motionY, laser.posZ + laser.motionZ); + MovingObjectPosition pos = laser.worldObj.rayTraceBlocks_do_do( + currentPos, heading, false, true); + + if(pos != null) { + int xPos = pos.blockX; + int yPos = pos.blockY; + int zPos = pos.blockZ; + + try { + Method m = owner.getClass() + .getDeclaredMethod("getBukkitEntity", new Class[] {}); + org.bukkit.entity.Entity ent = + (org.bukkit.entity.Entity)m.invoke(owner); + if ((ent instanceof Player)) { + Player player = (Player)ent; + org.bukkit.World bukkitWorld = player.getWorld(); + BlockBreakEvent breakEv = new BlockBreakEvent( + bukkitWorld.getBlockAt(xPos, yPos, zPos), player); + Bukkit.getPluginManager().callEvent(breakEv); + if (breakEv.isCancelled()) { + return false; + } + + breakEv.setCancelled(true); + } + } + catch(Exception e) { + e.printStackTrace(); + } + } + + return true; + } + + class EntityLaserVisitor extends ClassVisitor { + + public EntityLaserVisitor(ClassVisitor cv) { + super(ASM4, cv); + } + + @Override + public MethodVisitor visitMethod(int access, String name, String desc, + String signature, String[] exceptions) { + + if(name.equals("canMine")) { + return new CanMineVisitor(super.visitMethod(access, name, desc, + signature, exceptions)); + } + return super.visitMethod(access, name, desc, signature, + exceptions); + } + } + + class CanMineVisitor extends MethodVisitor { + + public CanMineVisitor(MethodVisitor mv) { + super(ASM4, mv); + } + + @Override + public void visitCode() { + Label l0 = new Label(); + mv.visitLabel(l0); + mv.visitVarInsn(ALOAD, 0); + mv.visitVarInsn(ALOAD, 0); + mv.visitFieldInsn(GETFIELD, "ic2/core/item/tool/EntityMiningLaser", + "owner", "L" + ObfNames.ENTITY_LIVING + ";"); + mv.visitMethodInsn(INVOKESTATIC, + "com/skcraft/alicefixes/transformers/TransformMiningLaser", + "canMine", + "(L" + ObfNames.ENTITY + ";L" + + ObfNames.ENTITY_LIVING + ";)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/CoordHelper.java b/alicefixes/src/com/skcraft/alicefixes/util/CoordHelper.java similarity index 90% rename from alicefixes/src/com/skcraft/alicefixes/CoordHelper.java rename to alicefixes/src/com/skcraft/alicefixes/util/CoordHelper.java index ace9e52..e713be3 100644 --- a/alicefixes/src/com/skcraft/alicefixes/CoordHelper.java +++ b/alicefixes/src/com/skcraft/alicefixes/util/CoordHelper.java @@ -1,10 +1,8 @@ -package com.skcraft.alicefixes; +package com.skcraft.alicefixes.util; public class CoordHelper { - int x; - int y; - int z; + public int x, y, z; public CoordHelper(int x, int y, int z) { this.x = x; diff --git a/alicefixes/src/com/skcraft/alicefixes/util/ObfNames.java b/alicefixes/src/com/skcraft/alicefixes/util/ObfNames.java new file mode 100644 index 0000000..f99d5d7 --- /dev/null +++ b/alicefixes/src/com/skcraft/alicefixes/util/ObfNames.java @@ -0,0 +1,15 @@ +package com.skcraft.alicefixes.util; + +public class ObfNames { + + public static final String ENTITY_LIVING = "ng"; + public static final String ENTITY = "mp"; + public static final String TILE_ENTITY = "aqp"; + public static final String ENTITY_PLAYER = "sq"; + public static final String WORLD = "aab"; + public static final String MOV_OBJ_POS = "ara"; + public static final String ITEM_STACK = "wm"; + + // Srgname for worldobj + public static final String WORLD_OBJ = "field_70170_p"; +}