plugins/tools/ExchangeListingMerge.java
unimatrixgit 26106815ce
Some checks failed
Exchange Listing Cards Build / exchange-listing-cards (push) Successful in 1m7s
DuneRealms Plugin CI / plugins (push) Successful in 10m11s
DuneRealms Resourcepack CI / resourcepack (push) Failing after 19s
Keep unnamed goods visible in Exchange listing cards
2026-12-06 11:14:19 +01:00

51 lines
2.6 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.Handle;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InvokeDynamicInsnNode;
import org.objectweb.asm.tree.MethodNode;
class ExchangeListingMerge {
private static final String CLASS = "com/dunerealms/gui/ExchangeMenus";
private static final String LIST_BODY = "lambda$list$13";
private static final String LIST_DESC = "(Lorg/bukkit/entity/Player;ZILcom/google/gson/JsonObject;)V";
private static final String NAME_DESC = "(Lnet/kyori/adventure/text/Component;Ljava/lang/String;Z)Lnet/kyori/adventure/text/Component;";
public static void main(String[] args) throws Exception {
if (args.length != 3) throw new IllegalArgumentException("Expected retained, donor and output classes");
var retainedReader = new ClassReader(Files.readAllBytes(Path.of(args[0])));
ClassNode retained = parse(retainedReader);
ClassNode donor = parse(new ClassReader(Files.readAllBytes(Path.of(args[1]))));
if (!CLASS.equals(retained.name) || !CLASS.equals(donor.name)) throw new IllegalArgumentException("Wrong class");
MethodNode old = method(retained,LIST_BODY,LIST_DESC);
MethodNode next = method(donor,LIST_BODY,LIST_DESC);
for (var instruction : next.instructions) {
if (instruction instanceof InvokeDynamicInsnNode dynamic) {
for (Object argument : dynamic.bsmArgs) {
if (argument instanceof Handle target && CLASS.equals(target.getOwner())) {
method(retained,target.getName(),target.getDesc());
}
}
}
}
if (retained.methods.stream().anyMatch(m -> m.name.equals("listingName"))) throw new IllegalArgumentException("Preview helper already exists");
retained.methods.set(retained.methods.indexOf(old),next);
retained.methods.add(method(donor,"listingName",NAME_DESC));
var writer = new ClassWriter(retainedReader,0);
retained.accept(writer);
Files.write(Path.of(args[2]),writer.toByteArray());
}
private static MethodNode method(ClassNode cls,String name,String desc) {
return cls.methods.stream().filter(m -> m.name.equals(name) && m.desc.equals(desc)).findFirst()
.orElseThrow(() -> new IllegalArgumentException("Missing exact method " + name + desc));
}
private static ClassNode parse(ClassReader reader) {
var node = new ClassNode();
reader.accept(node,0);
return node;
}
}