plugins/tools/PatchSpiceShovels.java
2026-11-27 23:43:05 +00:00

27 lines
3.5 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.*;
/** Additive patch of the exact live Arrakis Core, preserving jobs and heat fixes. */
public final class PatchSpiceShovels {
static byte[] methodBytes(MethodNode m){var w=new ClassWriter(0);w.visit(65,1,"Check",null,"java/lang/Object",null);m.accept(w.visitMethod(m.access,m.name,m.desc,m.signature,m.exceptions.toArray(String[]::new)));w.visitEnd();return w.toByteArray();}
public static void main(String[] args)throws Exception{
var source=Path.of(args[0]);var target=Path.of(args[1]);if(Files.exists(target))throw new IllegalStateException("Output exists");
if(!HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(source))).equals("d0dcc4375ca000154949bed3fe27d336391dc23037545ed77743779bfb2f5e6f"))throw new IllegalStateException("Live Core changed");
var files=new LinkedHashMap<String,byte[]>();try(var zip=new ZipInputStream(Files.newInputStream(source))){for(ZipEntry e;(e=zip.getNextEntry())!=null;)files.put(e.getName(),zip.readAllBytes());}
String main="com/example/dunerealms/DuneCore.class";var node=new ClassNode();new ClassReader(files.get(main)).accept(node,0);var before=new HashMap<String,byte[]>();int changes=0;
for(var method:node.methods){before.put(method.name+method.desc,methodBytes(method));if(!method.name.equals("onSpice")||!method.desc.equals("(Lorg/bukkit/event/block/BlockBreakEvent;)V"))continue;
for(var instruction:method.instructions.toArray())if(instruction instanceof FieldInsnNode field&&field.getOpcode()==Opcodes.GETSTATIC&&field.owner.equals("org/bukkit/Material")&&field.name.equals("NETHERITE_SHOVEL")){
if(!(field.getNext() instanceof JumpInsnNode branch)||branch.getOpcode()!=Opcodes.IF_ACMPNE)throw new IllegalStateException("Harvest predicate changed");
var replace=new InsnList();replace.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL,"org/bukkit/Material","name","()Ljava/lang/String;",false));replace.add(new LdcInsnNode("_SHOVEL"));replace.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL,"java/lang/String","endsWith","(Ljava/lang/String;)Z",false));method.instructions.insertBefore(field,replace);method.instructions.remove(field);branch.setOpcode(Opcodes.IFEQ);changes++;
}
}
if(changes!=1)throw new IllegalStateException("Expected exactly one material gate");var writer=new ClassWriter(0);node.accept(writer);byte[] changed=writer.toByteArray();var check=new ClassNode();new ClassReader(changed).accept(check,0);
for(var m:check.methods)if(!m.name.equals("onSpice")&&!Arrays.equals(before.get(m.name+m.desc),methodBytes(m)))throw new IllegalStateException("Unrelated method changed");
files.put(main,changed);String yml=new String(files.get("plugin.yml"),java.nio.charset.StandardCharsets.UTF_8);if(!yml.contains("1.0.27-layout1-jobs1-sun374"))throw new IllegalStateException("Unexpected Core version");files.put("plugin.yml",yml.replace("1.0.27-layout1-jobs1-sun374","1.0.27-layout1-jobs1-sun374-shovels1").getBytes(java.nio.charset.StandardCharsets.UTF_8));
try(var out=new ZipOutputStream(Files.newOutputStream(target,StandardOpenOption.CREATE_NEW))){for(var e:files.entrySet()){var entry=new ZipEntry(e.getKey());entry.setTime(0);out.putNextEntry(entry);out.write(e.getValue());out.closeEntry();}}
System.out.println("Changed only spice shovel material gate and version; legacy identity check, jobs, drops and heat logic preserved. SHA256 "+HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(target))));
}
}