package com.dunerealms.jarvis; import com.google.gson.*; import org.bukkit.*;import org.bukkit.entity.*;import org.bukkit.plugin.java.JavaPlugin;import java.util.*; public final class HomeNpcProbe extends JavaPlugin{ JsonArray row(UUID id,World w,Location at){JsonObject row=new JsonObject(),home=new JsonObject(),position=new JsonObject();row.addProperty("id",id.toString());row.addProperty("name","Isolated test clan");row.addProperty("lifecycle","active");home.addProperty("source","minecraft");home.addProperty("worldUuid",w.getUID().toString());position.addProperty("x",at.getBlockX());position.addProperty("y",at.getBlockY());position.addProperty("z",at.getBlockZ());home.add("position",position);row.add("homeBlock",home);JsonArray rows=new JsonArray();rows.add(row);return rows;} LivingEntity one(World w,String key){var matches=w.getEntitiesByClass(LivingEntity.class).stream().filter(e->key.equals(e.getPersistentDataContainer().get(OrganisationHomeNpcs.KEY,org.bukkit.persistence.PersistentDataType.STRING))).toList();if(matches.size()!=1)throw new AssertionError("Unexpected NPC count "+matches.size());return matches.get(0);} public void onEnable(){Bukkit.getScheduler().runTaskLater(this,()->{try{World w=Bukkit.getWorlds().getFirst();Location first=w.getSpawnLocation().clone().add(5,0,5);first.setY(w.getHighestBlockYAt(first)+1);Location second=first.clone().add(32,0,0);second.setY(w.getHighestBlockYAt(second)+1);UUID id=UUID.randomUUID();String key="clan:"+id;var controller=new OrganisationHomeNpcs(this);var owned=Set.of(id.toString());var blank=new JsonArray();if(!controller.reconcile(row(id,w,first),blank,owned,Set.of(),blank))throw new AssertionError("initial projection incomplete");var npc=one(w,key);UUID original=npc.getUniqueId();if(npc instanceof Mob m&&m.hasAI())throw new AssertionError("NPC AI active");if(!controller.reconcile(row(id,w,second),blank,owned,Set.of(),blank)||!one(w,key).getUniqueId().equals(original))throw new AssertionError("relocation lost identity");if(one(w,key).getLocation().distanceSquared(second)>16)throw new AssertionError("NPC did not move to new Home Block");controller=new OrganisationHomeNpcs(this);if(!controller.reconcile(row(id,w,second),blank,owned,Set.of(),blank)||!one(w,key).getUniqueId().equals(original))throw new AssertionError("restart lost identity");if(!controller.reconcile(blank,blank,Set.of(),Set.of(),blank)||!controller.absent("clan",id.toString())||Bukkit.getEntity(original)!=null)throw new AssertionError("retired NPC retained");getLogger().info("HOME_NPC_PROBE_PASS same identity after relocation and reload, retired NPC removed");}catch(Throwable error){getLogger().severe("HOME_NPC_PROBE_FAIL "+error);error.printStackTrace();}finally{Bukkit.shutdown();}},80L);} }