Add naturally spawning cold pigs on Sigma
This commit is contained in:
parent
1a1f6c8d09
commit
693037a982
1 changed files with 187 additions and 2 deletions
69
tools/SigmaColdPigMerge.java
Normal file
69
tools/SigmaColdPigMerge.java
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
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.ClassNode;
|
||||
import org.objectweb.asm.tree.FrameNode;
|
||||
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;
|
||||
|
||||
class SigmaColdPigMerge {
|
||||
private static final String OWNER = "com/example/duneterrain/PlanetMobs";
|
||||
private static final String PIG = "(Ljava/lang/String;Ljava/lang/String;)Z";
|
||||
private static final String SPAWN = "(Lorg/bukkit/event/entity/CreatureSpawnEvent;)";
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (args.length != 3) throw new IllegalArgumentException("Expected retained, donor and output classes");
|
||||
var reader = new ClassReader(Files.readAllBytes(Path.of(args[0])));
|
||||
var retained = parse(reader);
|
||||
var donor = parse(new ClassReader(Files.readAllBytes(Path.of(args[1]))));
|
||||
if (!OWNER.equals(retained.name) || !OWNER.equals(donor.name) ||
|
||||
retained.methods.stream().anyMatch(m -> m.name.equals("sigmaColdPig") || m.name.equals("allowedColdPig"))) {
|
||||
throw new IllegalArgumentException("Unexpected retained PlanetMobs class");
|
||||
}
|
||||
retained.methods.add(method(donor, "sigmaColdPig", SPAWN + "Z"));
|
||||
retained.methods.add(method(donor, "allowedColdPig", PIG));
|
||||
var allowed = method(retained, "allowed", "(Ljava/lang/String;Ljava/lang/String;Z)Z");
|
||||
var check = new InsnList();
|
||||
check.add(new VarInsnNode(Opcodes.ALOAD, 0));
|
||||
check.add(new VarInsnNode(Opcodes.ALOAD, 1));
|
||||
check.add(new MethodInsnNode(Opcodes.INVOKESTATIC, OWNER, "allowedColdPig", PIG, false));
|
||||
prefix(allowed, check, true);
|
||||
var spawning = method(retained, "spawning", SPAWN + "V");
|
||||
var convert = new InsnList();
|
||||
convert.add(new VarInsnNode(Opcodes.ALOAD, 1));
|
||||
convert.add(new MethodInsnNode(Opcodes.INVOKESTATIC, OWNER, "sigmaColdPig", SPAWN + "Z", false));
|
||||
prefix(spawning, convert, false);
|
||||
var writer = new ClassWriter(reader, 0);
|
||||
retained.accept(writer);
|
||||
Files.write(Path.of(args[2]), writer.toByteArray());
|
||||
}
|
||||
|
||||
private static void prefix(MethodNode method, InsnList instructions, boolean returnsValue) {
|
||||
var resume = new LabelNode();
|
||||
instructions.add(new JumpInsnNode(Opcodes.IFEQ, resume));
|
||||
if (returnsValue) instructions.add(new InsnNode(Opcodes.ICONST_1));
|
||||
instructions.add(new InsnNode(returnsValue ? Opcodes.IRETURN : Opcodes.RETURN));
|
||||
instructions.add(resume);
|
||||
instructions.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));
|
||||
method.instructions.insert(instructions);
|
||||
method.maxStack = Math.max(method.maxStack, 2);
|
||||
}
|
||||
|
||||
private static ClassNode parse(ClassReader reader) {
|
||||
var node = new ClassNode();
|
||||
reader.accept(node, 0);
|
||||
return node;
|
||||
}
|
||||
|
||||
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(() -> new IllegalArgumentException("Missing expected method " + name));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue

Staff