44 lines
2.2 KiB
Java
44 lines
2.2 KiB
Java
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.MethodInsnNode;
|
|
|
|
final class HomeDimensionOverlay implements Opcodes {
|
|
private static final String TRAVEL = "com/example/duneess/PersonalHomeTravel";
|
|
private static final String ROUTES = "com/example/duneess/HomeDimensionRoutes";
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
if (args.length != 2) throw new IllegalArgumentException("Expected retained and output class");
|
|
var old = new ClassReader(Files.readAllBytes(Path.of(args[0])));
|
|
var retained = new ClassNode();
|
|
old.accept(retained, 0);
|
|
if (!retained.name.equals(TRAVEL)) throw new IllegalArgumentException("Unexpected retained travel class");
|
|
|
|
replace(retained, "begin", "(Lorg/bukkit/entity/Player;Lorg/bukkit/Location;)V");
|
|
replace(retained, "lambda$attempt$3",
|
|
"(Lcom/google/gson/JsonObject;Ljava/lang/String;Lorg/bukkit/entity/Player;Ljava/lang/String;)V");
|
|
|
|
var writer = new ClassWriter(old, 0);
|
|
retained.accept(writer);
|
|
Files.write(Path.of(args[1]), writer.toByteArray());
|
|
}
|
|
|
|
private static void replace(ClassNode retained, String name, String descriptor) {
|
|
var method = retained.methods.stream().filter(m -> m.name.equals(name) && m.desc.equals(descriptor))
|
|
.findFirst().orElseThrow();
|
|
int patched = 0;
|
|
for (var instruction : method.instructions.toArray()) {
|
|
if (instruction instanceof MethodInsnNode call && call.getOpcode() == INVOKEINTERFACE
|
|
&& call.owner.equals("org/bukkit/World") && call.name.equals("equals")
|
|
&& call.desc.equals("(Ljava/lang/Object;)Z")) {
|
|
method.instructions.set(call, new MethodInsnNode(INVOKESTATIC, ROUTES, "canReturn",
|
|
"(Lorg/bukkit/World;Ljava/lang/Object;)Z", false));
|
|
patched++;
|
|
}
|
|
}
|
|
if (patched != 1) throw new IllegalArgumentException("Expected one retained world predicate: " + name);
|
|
}
|
|
}
|

Staff