MC/tools/PatchTrailsNature.java
2026-11-27 23:43:05 +00:00

29 lines
2 KiB
Java

import java.nio.file.*;
import java.security.*;
import java.util.*;
import java.util.zip.*;
import org.objectweb.asm.*;
import org.objectweb.asm.tree.*;
/** Exact upstream Trails1.9 adaptation: natural wear ignores external claims. */
public final class PatchTrailsNature {
public static void main(String[] args)throws Exception{
byte[] original=Files.readAllBytes(Path.of(args[0]));String hash=HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(original));
if(!hash.equals("e88c456fad9504c338181ee580d1e6b20bece1ff141f73c6f378e32f5bdcdb27"))throw new IllegalArgumentException("Unexpected upstream Trails artifact");
int patched=0;
try(var in=new ZipInputStream(new java.io.ByteArrayInputStream(original));var out=new ZipOutputStream(Files.newOutputStream(Path.of(args[1])))){
ZipEntry entry;while((entry=in.getNextEntry())!=null){byte[] bytes=in.readAllBytes();
if(entry.getName().equals("me/ccrama/Trails/listeners/MoveEventListener.class")){
var type=new ClassNode();new ClassReader(bytes).accept(type,0);
for(MethodNode method:type.methods)if(method.name.equals("checkConditions")&&method.desc.equals("(Lorg/bukkit/entity/Player;Lorg/bukkit/Location;)Z")){
method.instructions.clear();method.tryCatchBlocks.clear();if(method.localVariables!=null)method.localVariables.clear();method.instructions.add(new InsnNode(Opcodes.ICONST_1));method.instructions.add(new InsnNode(Opcodes.IRETURN));method.maxStack=1;patched++;
}
var writer=new ClassWriter(0);type.accept(writer);bytes=writer.toByteArray();
}
if(entry.getName().equals("plugin.yml"))bytes=new String(bytes,java.nio.charset.StandardCharsets.UTF_8).replace("version: 1.9","version: 1.9-dunerealms1").getBytes(java.nio.charset.StandardCharsets.UTF_8);
var next=new ZipEntry(entry.getName());next.setTime(0);out.putNextEntry(next);out.write(bytes);out.closeEntry();
}
}
if(patched!=1){Files.delete(Path.of(args[1]));throw new IllegalStateException("Expected exactly one protection adapter");}
}
}