Skip to content

Instantly share code, notes, and snippets.

@thomas15v
Created August 8, 2014 21:20
Show Gist options
  • Save thomas15v/d0017879daacaaf3a1e1 to your computer and use it in GitHub Desktop.
Save thomas15v/d0017879daacaaf3a1e1 to your computer and use it in GitHub Desktop.
import mcpc.patchengine.api.IPatch;
import mcpc.patchengine.asm.util.ClassUtil;
import mcpc.patchengine.asm.util.MethodUtil;
import mcpc.patchengine.asm.util.NavigationUtil;
import mcpc.patchengine.common.Configuration;
import mcpc.patchengine.common.Constants;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.*;
import org.objectweb.asm.util.Printer;
import org.objectweb.asm.util.Textifier;
import org.objectweb.asm.util.TraceMethodVisitor;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Logger;
/**
* Created by thomas on 8/8/2014.
*/
public class RedPowerBlockBreakerFix implements IPatch {
private String Classname = "com.eloraam.redpower.machine.TileBreaker";
@Override
public String[] getClassNames() {
return new String[]{Classname};
}
@Override
public void loadConfigurations(Configuration configuration) {
}
@Override
public void transform(String s, ClassNode classNode) {
if (s.equalsIgnoreCase(Classname)){
MethodNode method = ClassUtil.getMethod(classNode, "onBlockNeighborChange", String.format("(I)V", new Object[]{"I"}));
if (method == null)
Logger.getLogger("Minecraft").info("Dangit :'(");
else {
method.maxStack = 10;
// printinstruction(method.instructions);
//INVOKEVIRTUAL amq.getBlockDropped (Lyc;IIIII)Ljava/util/ArrayList;
LabelNode LabelMethode1 = MethodUtil.getLabelWithInsn(method, new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "amq", "getBlockDropped", "(Lyc;IIIII)Ljava/util/ArrayList;"));
LabelNode LabelMethode2 = MethodUtil.getLabelWithInsn(method, new MethodInsnNode(Opcodes.INVOKEVIRTUAL, Constants.WorldClass, "e", "(IIII)Z"));
/* ALOAD 0
GETFIELD com/eloraam/redpower/machine/TileBreaker.k : Lyc;
ALOAD 3
GETFIELD com/eloraam/redpower/core/WorldCoord.x : I
ALOAD 3
GETFIELD com/eloraam/redpower/core/WorldCoord.y : I
ALOAD 3
GETFIELD com/eloraam/redpower/core/WorldCoord.z : I
ICONST_0
*/
InsnList blockbreakcode = new InsnList();
blockbreakcode.add( new VarInsnNode(Opcodes.ALOAD, 0 ) );
blockbreakcode.add( new FieldInsnNode( Opcodes.GETFIELD, "com/eloraam/redpower/machine/TileBreaker", "k", "Lyc;" ) );
blockbreakcode.add( new VarInsnNode(Opcodes.ALOAD, 3 ) );
blockbreakcode.add( new FieldInsnNode( Opcodes.GETFIELD, "com/eloraam/redpower/core/WorldCoord", "x", "I" ) );
blockbreakcode.add( new VarInsnNode(Opcodes.ALOAD, 3 ) );
blockbreakcode.add( new FieldInsnNode( Opcodes.GETFIELD, "com/eloraam/redpower/core/WorldCoord", "y", "I" ) );
blockbreakcode.add( new VarInsnNode(Opcodes.ALOAD, 3 ) );
blockbreakcode.add( new FieldInsnNode( Opcodes.GETFIELD, "com/eloraam/redpower/core/WorldCoord", "z", "I" ) );
blockbreakcode.add( new InsnNode(Opcodes.ICONST_0) );
blockbreakcode.add( new InsnNode(Opcodes.ICONST_0) );
blockbreakcode.add( new InsnNode(Opcodes.ICONST_1) );
blockbreakcode.add (new LdcInsnNode("[Redpower2]" ));
blockbreakcode.add( new InsnNode(Opcodes.ICONST_0) );
blockbreakcode.add( new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "yc", "trySetBlockAndMetadata", "(IIIIIZLjava/lang/String;Z)Z" ) );
LabelNode label1 = new LabelNode();
blockbreakcode.add ( new JumpInsnNode(Opcodes.IFEQ, label1) );
blockbreakcode.add( label1 );
method.instructions.insert(LabelMethode1, blockbreakcode);
//method.instructions.insert(permLabel, new JumpInsnNode(Opcodes.GOTO , label2));
LabelNode label2 = new LabelNode();
method.instructions.insert(LabelMethode2, new JumpInsnNode(Opcodes.GOTO, label2));
method.instructions.insert(NavigationUtil.getNextLabel(LabelMethode2), label2);
return;
// clearlabel(permLabel, method);
// printlabel(LabelMethode1);
}
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static Printer printer = new Textifier();
private static TraceMethodVisitor mp = new TraceMethodVisitor(printer);
public static void printinstruction(InsnList list){
for (AbstractInsnNode node : list.toArray())
Logger.getLogger("Minecraft").info(insnToString(node));
}
public static void printlabel(LabelNode node){
AbstractInsnNode next = node.getNext();
while ((next != null) && (!(next instanceof LabelNode)))
{
Logger.getLogger("Minecraft").info(insnToString(next));
next = next.getNext();
}
}
public static void clearlabel(LabelNode node, MethodNode method){
AbstractInsnNode next = node.getNext();
while ((next != null) && (!(next instanceof LabelNode)))
{
method.instructions.remove(next);
next = next.getNext();
}
}
public static String insnToString(AbstractInsnNode insn){
insn.accept(mp);
StringWriter sw = new StringWriter();
printer.print(new PrintWriter(sw));
printer.getText().clear();
return sw.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment