Preserve exact live terrain during fauna rollout and fix probe defaults

This commit is contained in:
Dev 2026-11-23 22:45:40 +01:00
commit 065d02cfdc
2 changed files with 29 additions and 1 deletions

View file

@ -19,7 +19,7 @@ public final class ArrakisFaunaSmoke extends JavaPlugin {
@Override public void onEnable(){fauna=new JerboaMobs(this,this::item,()->0);fauna.start();Bukkit.getScheduler().runTaskLater(this,()->{try{run();}catch(Throwable failure){getLogger().severe("FAUNA_SMOKE_FAIL "+failure);failure.printStackTrace();Bukkit.shutdown();}},50);}
private ItemStack item(String id){var stack=new ItemStack(id.contains("hide")?Material.RABBIT_HIDE:id.contains("vial")?Material.POTION:id.contains("cooked")?Material.COOKED_RABBIT:Material.RABBIT);var meta=stack.getItemMeta();meta.getPersistentDataContainer().set(new NamespacedKey("executableitems","ei-id"),PersistentDataType.STRING,id);stack.setItemMeta(meta);return stack;}
private static void check(boolean condition,String message){if(!condition)throw new IllegalStateException(message);}
private Object defaults(Method m){Class<?> r=m.getReturnType();return r==boolean.class?false:r==int.class?0:r==float.class?0f:r==double.class?0d:r==long.class?0L:null;}
private Object defaults(Method m){Class<?> r=m.getReturnType();if(r==boolean.class)return false;if(r==int.class)return 0;if(r==float.class)return 0f;if(r==double.class)return 0d;if(r==long.class)return 0L;return null;}
private Player player(World world){
UUID uuid=UUID.randomUUID();PlayerInventory inventory=(PlayerInventory)Proxy.newProxyInstance(getClassLoader(),new Class<?>[]{PlayerInventory.class},(p,m,a)->switch(m.getName()){case "getItem"->held;case "setItem"->{held=(ItemStack)a[1];yield null;}default->defaults(m);});
return (Player)Proxy.newProxyInstance(getClassLoader(),new Class<?>[]{Player.class},(p,m,a)->switch(m.getName()){case "getName"->"FaunaSmoke";case "getUniqueId"->uuid;case "getWorld"->world;case "getInventory"->inventory;case "getGameMode"->GameMode.SURVIVAL;case "isOnline"->true;case "getLocation"->new Location(world,8.5,65,8.5);default->defaults(m);});

View file

@ -0,0 +1,28 @@
import java.nio.file.*;
import java.security.*;
import java.util.*;
import java.util.zip.*;
import org.objectweb.asm.*;
import org.objectweb.asm.tree.*;
/** Add fauna/Trails to an exact current terrain JAR without replacing generation. */
public final class PatchFaunaTerrain {
public static void main(String[] args)throws Exception{
Path input=Path.of(args[0]),classes=Path.of(args[1]),output=Path.of(args[2]);String expected=args[3],prefix="com/example/duneterrain/";
if(Files.exists(output)||!HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(input))).equals(expected))throw new IllegalStateException("Unexpected live terrain or output");
var entries=new LinkedHashMap<String,byte[]>();try(var zip=new ZipInputStream(Files.newInputStream(input))){for(ZipEntry e;(e=zip.getNextEntry())!=null;)if(entries.put(e.getName(),zip.readAllBytes())!=null)throw new IllegalStateException("Duplicate archive entry");}
var original=new LinkedHashMap<>(entries);String main=prefix+"DuneTerrain.class";var type=new ClassNode();new ClassReader(entries.get(main)).accept(type,0);var methods=new HashMap<String,byte[]>();for(var method:type.methods)methods.put(method.name+method.desc,PatchArrakisEcology.methodBytes(method));
int edits=0;for(MethodNode method:type.methods)if(method.name.equals("onEnable")&&method.desc.equals("()V")){
for(var instruction:method.instructions)if(instruction instanceof MethodInsnNode call&&call.owner.equals(prefix+"TrailsBridge"))throw new IllegalStateException("Trails bridge already present");
var hook=new InsnList();hook.add(new VarInsnNode(Opcodes.ALOAD,0));hook.add(new MethodInsnNode(Opcodes.INVOKESTATIC,prefix+"TrailsBridge","start","(Lorg/bukkit/plugin/Plugin;)V",false));method.instructions.insert(hook);method.maxStack=Math.max(method.maxStack,1);edits++;
}
if(edits!=1)throw new IllegalStateException("Native activation hook changed");var writer=new ClassWriter(0);type.accept(writer);entries.put(main,writer.toByteArray());var verify=new ClassNode();new ClassReader(entries.get(main)).accept(verify,0);for(var method:verify.methods)if(!method.name.equals("onEnable")&&!Arrays.equals(methods.get(method.name+method.desc),PatchArrakisEcology.methodBytes(method)))throw new IllegalStateException("Unrelated terrain method changed");
var allowed=new HashSet<String>(Set.of(main,"plugin.yml"));var inserted=new HashSet<String>();
try(var paths=Files.list(classes.resolve(prefix))){for(Path file:paths.filter(p->p.getFileName().toString().matches("(?:ArrakisEcology|JerboaMobs|FaunaModels|TrailsBridge)(?:\\$[A-Za-z0-9]+)?\\.class")).toList()){String name=prefix+file.getFileName();entries.put(name,Files.readAllBytes(file));allowed.add(name);inserted.add(name);}}
for(String name:List.of("ArrakisEcology","JerboaMobs","FaunaModels","TrailsBridge"))if(!inserted.contains(prefix+name+".class"))throw new IllegalStateException("Missing compiled feature");
String descriptor=new String(entries.get("plugin.yml"),java.nio.charset.StandardCharsets.UTF_8);entries.put("plugin.yml",descriptor.replaceAll("(?m)^version:.*$","version: 1.0.12-arrakeen1-fauna2-trails1").getBytes(java.nio.charset.StandardCharsets.UTF_8));
for(var e:original.entrySet())if(!allowed.contains(e.getKey())&&!Arrays.equals(e.getValue(),entries.get(e.getKey())))throw new IllegalStateException("Unrelated archive entry changed");
try(var zip=new ZipOutputStream(Files.newOutputStream(output,StandardOpenOption.CREATE_NEW))){for(var e:entries.entrySet()){var next=new ZipEntry(e.getKey());next.setTime(0);zip.putNextEntry(next);zip.write(e.getValue());zip.closeEntry();}}
System.out.println("Preserved original generation and unrelated terrain methods. SHA256 "+HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(output))));
}
}