import java.nio.file.Files; import java.nio.file.Path; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Opcodes; import org.objectweb.asm.tree.*; final class StationAndDoorOverlay implements Opcodes { private static final String JARVIS = "com/dunerealms/jarvis/"; private static final String ESS = "com/example/duneess/"; private static final String BUKKIT = "org/bukkit/Bukkit"; private static final String MANAGER = "org/bukkit/plugin/PluginManager"; private static final String REGISTER = "(Lorg/bukkit/event/Listener;Lorg/bukkit/plugin/Plugin;)V"; private static final String MATERIAL = "Lorg/bukkit/Material;"; private static final String PICKUP = "(" + MATERIAL.repeat(3) + ")Z"; private static final String FILL = "(Lorg/bukkit/event/player/PlayerBucketFillEvent;)V"; public static void main(String[] args) throws Exception { if (args.length != 5) throw new IllegalArgumentException("Expected role, entry, retained, donor and output"); String role = args[0], entry = args[1]; var reader = new ClassReader(Files.readAllBytes(Path.of(args[2]))); var retained = read(reader); var donor = read(new ClassReader(Files.readAllBytes(Path.of(args[3])))); if (!retained.name.equals(donor.name) || !retained.name.equals(entry)) { throw new IllegalArgumentException("Unexpected retained or donor class: " + entry); } if (role.equals("ess")) { if (!entry.equals(ESS + "DuneEss")) throw new IllegalArgumentException("Unexpected Essentials class"); registerAfterExisting(method(retained, "onEnable", "()V"), ESS + "IronDoorInteraction"); } else if (role.equals("station")) { switch (entry.substring(JARVIS.length())) { case "WorkstationOrigins" -> registerAfterExisting(method(retained, "", "(Lorg/bukkit/plugin/Plugin;Ljava/util/function/BiFunction;)V"), JARVIS + "ComposterOrigins"); case "ItemOrigin" -> replace(retained, donor, "label", "label", "(Ljava/lang/String;)Ljava/lang/String;"); case "ArrakisBrewing" -> replace(retained, donor, "brewed", "brewed", "(Lorg/bukkit/event/inventory/BrewEvent;)V"); case "ItemOriginRemainders" -> { replace(retained, donor, "fillLava", "fillBucket", FILL); replace(retained, donor, "burn", "burn", "(Lorg/bukkit/event/inventory/FurnaceBurnEvent;)V"); // Retain the old package-private predicate for callers compiled against the baseline. if (retained.methods.stream().anyMatch(m -> m.name.equals("bucketPickup"))) throw new IllegalArgumentException("Bucket pickup already installed"); retained.methods.add(method(donor, "bucketPickup", PICKUP)); } default -> throw new IllegalArgumentException("Unexpected station class: " + entry); } } else throw new IllegalArgumentException("Unknown overlay role: " + role); var writer = new ClassWriter(reader, 0); retained.accept(writer); Files.write(Path.of(args[4]), writer.toByteArray()); } private static MethodNode method(ClassNode node, String name, String descriptor) { return node.methods.stream().filter(m -> m.name.equals(name) && m.desc.equals(descriptor)).findFirst().orElseThrow(); } private static ClassNode read(ClassReader reader) { var node = new ClassNode(); reader.accept(node, 0); return node; } private static void replace(ClassNode retained, ClassNode donor, String oldName, String newName, String descriptor) { var original = method(retained, oldName, descriptor); var next = method(donor, newName, descriptor); retained.methods.set(retained.methods.indexOf(original), next); } private static void registerAfterExisting(MethodNode method, String listener) { MethodInsnNode registration = null; for (var instruction : method.instructions) { if (instruction instanceof MethodInsnNode call && call.owner.equals(MANAGER) && call.name.equals("registerEvents") && call.desc.equals(REGISTER)) { if (registration != null) throw new IllegalArgumentException("Ambiguous existing listener registration"); registration = call; } } if (registration == null) throw new IllegalArgumentException("Retained listener registration missing"); var code = new InsnList(); code.add(new MethodInsnNode(INVOKESTATIC, BUKKIT, "getPluginManager", "()L" + MANAGER + ";", false)); code.add(new TypeInsnNode(NEW, listener)); code.add(new InsnNode(DUP)); code.add(new MethodInsnNode(INVOKESPECIAL, listener, "", "()V", false)); code.add(new VarInsnNode(ALOAD, method.name.equals("") ? 1 : 0)); code.add(new MethodInsnNode(INVOKEINTERFACE, MANAGER, "registerEvents", REGISTER, true)); method.instructions.insert(registration, code); method.maxStack = Math.max(method.maxStack, 3); } }