Keep unnamed goods visible in Exchange listing cards
This commit is contained in:
parent
f7af3271aa
commit
26106815ce
1 changed files with 155 additions and 1 deletions
51
tools/ExchangeListingMerge.java
Normal file
51
tools/ExchangeListingMerge.java
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue

Staff