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

49 lines
6.3 KiB
Java

package com.example.duneterrain;
import java.lang.reflect.*;
import java.util.*;
import org.bukkit.*;
import org.bukkit.attribute.Attribute;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.*;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.*;
import org.bukkit.event.player.*;
import org.bukkit.inventory.*;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
/** Disposable native boot check. Never runs on a production world. */
public final class ArrakisFaunaSmoke extends JavaPlugin {
private JerboaMobs fauna;private ItemStack held;
@Override public void onEnable(){fauna=new JerboaMobs(this,this::item,()->1);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();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);});
}
private void run()throws Exception{
World world=Bukkit.getWorld("arrakis");check(world!=null,"arrakis test world absent");
world.getChunkAt(0,0).addPluginChunkTicket(this);
world.setChunkForceLoaded(0,0,true);
for(int x=0;x<16;x++)for(int z=0;z<16;z++)world.getBlockAt(x,64,z).setType(Material.STONE,false);
Player player=player(world);Plugin trails=Bukkit.getPluginManager().getPlugin("Trails");check(trails!=null&&trails.isEnabled(),"Native Trails not enabled");
var block=world.getBlockAt(8,65,8);block.setType(Material.SAND,false);
var constructor=TrailsBridge.class.getDeclaredConstructor(Plugin.class);constructor.setAccessible(true);TrailsBridge bridge=constructor.newInstance(this);
bridge.step(new PlayerMoveEvent(player,new Location(world,8.5,66,8.5),new Location(world,9.5,66,8.5)));
Object listener=TrailsBridge.nativeListener(trails);
Object link=listener.getClass().getMethod("getLink",org.bukkit.block.Block.class).invoke(listener,block);check(link!=null,"Sand trail absent");
for(int i=0;i<40&&block.getType()==Material.SAND;i++)listener.getClass().getMethod("makePath",Player.class,org.bukkit.block.Block.class,link.getClass(),boolean.class).invoke(listener,player,block,link,false);
check(block.getType()==Material.SANDSTONE,"Native wear did not create sandstone");
held=new ItemStack(Material.BRUSH);bridge.brush(new PlayerInteractEvent(player,Action.RIGHT_CLICK_BLOCK,held,block,BlockFace.UP,EquipmentSlot.HAND));check(block.getType()==Material.SAND,"Brush did not restore original sand");
var plain=world.getBlockAt(9,65,8);plain.setType(Material.SANDSTONE,false);bridge.brush(new PlayerInteractEvent(player,Action.RIGHT_CLICK_BLOCK,held,plain,BlockFace.UP,EquipmentSlot.HAND));check(plain.getType()==Material.SANDSTONE,"Brush altered ordinary construction");
Rabbit rabbit=world.spawn(new Location(world,2.5,65,2.5),Rabbit.class);fauna.replace(rabbit);check(!rabbit.isValid(),"Rabbit was not replaced");
Armadillo adult=world.getEntitiesByClass(Armadillo.class).stream().filter(e->JerboaMobs.isJerboa(e)&&e.isAdult()&&e.getLocation().distanceSquared(new Location(world,2.5,65,2.5))<.1).findFirst().orElseThrow();check(adult.getAttribute(Attribute.MAX_HEALTH).getValue()==3,"Jerboa health changed");check(Math.abs(adult.getAttribute(Attribute.MOVEMENT_SPEED).getValue()-.225)<1e-6,"Jerboa speed changed");
Cat legacy=world.spawn(new Location(world,4.5,65,4.5),Cat.class);legacy.getPersistentDataContainer().set(JerboaMobs.KIND,PersistentDataType.BYTE,(byte)1);legacy.setAge(-1234);fauna.migrate(legacy);check(!legacy.isValid(),"Legacy cat not migrated");check(world.getEntitiesByClass(Armadillo.class).stream().anyMatch(a->a.getAge()==-1234),"Migration lost baby age");
int before=world.getEntitiesByClass(Armadillo.class).size();held=item("spice_block");fauna.feed(new PlayerInteractEntityEvent(player,adult,EquipmentSlot.HAND));check(world.getEntitiesByClass(Armadillo.class).size()==before+4,"One-parent block did not produce four babies");
Location start=adult.getLocation().clone();Bukkit.getScheduler().runTaskLater(this,()->{getLogger().info("Native walk state ai="+adult.hasAI()+" ground="+adult.isOnGround()+" state="+adult.getState()+" at="+adult.getLocation()+" path="+adult.getPathfinder().moveTo(new Location(world,12.5,65,2.5),1));},10);
Bukkit.getScheduler().runTaskLater(this,()->{try{check(adult.getLocation().distanceSquared(start)>.04,"Native jerboa failed to walk");check(adult.isInvisible(),"Carrier remains visible");check(world.getEntitiesByClass(ItemDisplay.class).stream().anyMatch(d->d.getItemStack().getItemMeta().hasItemModel()),"Custom model display absent");adult.rollUp();check(adult.getState()!=Armadillo.State.IDLE,"Armadillo did not curl defensively");getLogger().info("FAUNA_SMOKE_SUCCESS sandWear=true brushRestore=true ordinarySandstonePreserved=true armadilloCarrier=true fourBabies=true realModelDisplay=true nativeWalking=true defensiveCurl=true");}catch(Throwable failure){getLogger().severe("FAUNA_SMOKE_FAIL "+failure);failure.printStackTrace();}finally{Bukkit.shutdown();}},40);
}
}