Skip to content

Instantly share code, notes, and snippets.

@leonardosnt
Last active September 20, 2016 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonardosnt/eb3c8e1e6227a603adba57e70beedd05 to your computer and use it in GitHub Desktop.
Save leonardosnt/eb3c8e1e6227a603adba57e70beedd05 to your computer and use it in GitHub Desktop.
File origi = new File("C:\\Users\\Leonardo\\Documents\\Servers\\Eraldia\\plugins\\dtlTraders.jar");
File jarFile = new File("C:\\Users\\Leonardo\\Documents\\Servers\\Eraldia\\plugins\\dtlTraders-patched.jar");
java.nio.file.Files.copy(origi.toPath(), jarFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
File patchedDir = new File("C:\\Users\\Leonardo\\Documents\\Servers\\Eraldia\\plugins\\patched");
patchedDir.mkdir();
JarFile jar = new JarFile(jarFile);
jar.stream().forEach(entry -> {
if (entry.getName().endsWith("/Trader.class")) {
try {
InputStream input = jar.getInputStream(entry);
ClassReader cr = new ClassReader(input);
ClassWriter cw = new ClassWriter(0);
cr.accept(new ClassVisitor(ASM5, cw) {
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
MethodVisitor def = super.visitMethod(access, name, desc, signature, exceptions);
if (name.equals("transactionEvent")) {
return new MethodVisitor(ASM5, def) {
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
if (opcode == GETSTATIC && name.equals("INVENTORY_FULL")) {
super.visitVarInsn(ALOAD, 1);
return;
}
super.visitFieldInsn(opcode, owner, name, desc);
}
};
}
return def;
}
}, 0);
File classFile = new File(patchedDir, entry.getName());
classFile.getParentFile().mkdirs();
byte[] bytecode = cw.toByteArray();
Files.write(bytecode, classFile);
} catch (IOException e) {
e.printStackTrace();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment