Integrate Wild Update quests and Citizens contacts

This commit is contained in:
unimatrixgit 2026-12-03 21:42:17 +01:00
commit f46b26d956

View file

@ -119,17 +119,61 @@ public final class CitizensBridge implements Listener {
} catch (ReflectiveOperationException | IllegalStateException unavailable) { /* Existing appearance remains. */ }
}
public static void rename(Entity entity, String name) {
try {
Object npc = citizen(entity);
if (npc != null && !name.equals(npcCall(npc, "getName", new Class<?>[0])))
npcCall(npc, "setName", new Class<?>[]{String.class}, name);
} catch (ReflectiveOperationException | IllegalStateException unavailable) { /* Keep current appearance. */ }
}
public record Contact(UUID id, Location location) {}
/** Read a single registered contact even if its chunk is unloaded. */
public static Contact contact(org.bukkit.World world, NamespacedKey marker, String role) {
try {
Contact result = null;
for (Object npc : (Iterable<?>) registry()) {
if (!(read(npc, "dr-markers") instanceof String text)
|| !(npcCall(npc, "getStoredLocation", new Class<?>[0]) instanceof Location location)
|| !world.equals(location.getWorld())) continue;
if (!matchesMarker(text, marker, role)) continue;
if (result != null || !(read(npc, "dr-original") instanceof String id)) return null;
result = new Contact(UUID.fromString(id), location);
}
return result;
} catch (ReflectiveOperationException | java.io.IOException | IllegalStateException | IllegalArgumentException unavailable) {
return null;
}
}
static boolean matchesMarker(String text, NamespacedKey marker, String role) throws java.io.IOException {
Properties markers = new Properties();
markers.load(new StringReader(text));
return (role.equals("1") ? "b:1" : "s:" + role).equals(markers.getProperty(marker.toString()));
}
public static LivingEntity spawnContact(JavaPlugin plugin, Location location, String name, NamespacedKey marker) {
return spawnContact(plugin, location, name, marker, null, false);
}
/**
* Create a Citizens player NPC. A non-null {@code value} stores the marker as a string and makes the NPC id stable per
* (plugin, world, marker, value). {@code genericSkin} stops Citizens looking up a real player whose name matches the NPC.
*/
public static LivingEntity spawnContact(JavaPlugin plugin, Location location, String name, NamespacedKey marker, String value, boolean genericSkin) {
Object npc = null;
try {
Object registry = registry();
npc = api("net.citizensnpcs.api.npc.NPCRegistry").getMethod("createNPC", EntityType.class, String.class).invoke(registry, EntityType.PLAYER, name);
UUID original = UUID.randomUUID();
write(npc, "dr-id", plugin.getName() + ":" + location.getWorld().getUID() + ":" + marker + ":" + original);
boolean flag = "1".equals(value); // "1" = one byte-marked contact per world, matching the id used when legacy entities are converted
write(npc, "dr-id", plugin.getName() + ":" + location.getWorld().getUID() + ":" + marker + ":" + (value == null ? original : value));
write(npc, "dr-owner", plugin.getName()); write(npc, "dr-original", original.toString());
Properties markers = new Properties(); markers.setProperty(marker.toString(), "b:1");
Properties markers = new Properties(); markers.setProperty(marker.toString(), value == null || flag ? "b:1" : "s:" + value);
StringWriter text = new StringWriter(); markers.store(text, null); write(npc, "dr-markers", text.toString());
npcCall(npc, "setProtected", new Class<?>[]{boolean.class}, true);
if (genericSkin) noDefaultSkin(npc);
if (!(boolean) npcCall(npc, "spawn", new Class<?>[]{Location.class}, location)) throw new IllegalStateException("Citizens spawn blocked");
new CitizensBridge(plugin, Set.of(marker)).restore(npc);
LivingEntity result = (LivingEntity) npcCall(npc, "getEntity", new Class<?>[0]);
@ -142,6 +186,57 @@ public final class CitizensBridge implements Listener {
}
}
private static void noDefaultSkin(Object npc) {
try {
Class<?> type = api("net.citizensnpcs.trait.SkinTrait");
Object trait = npcCall(npc, "getOrAddTrait", new Class<?>[]{Class.class}, type);
type.getMethod("setFetchDefaultSkin", boolean.class).invoke(trait, false);
} catch (ReflectiveOperationException | IllegalStateException unavailable) { /* Citizens default appearance. */ }
}
/** True when Citizens holds a (possibly unloaded) contact with this stable id; false when unknown or Citizens is not ready. */
public static boolean exists(JavaPlugin plugin, org.bukkit.World world, NamespacedKey marker, String value) {
try {
for (Object npc : (Iterable<?>) registry()) {
if (registeredAt(npc, world, marker, value)) return true;
}
} catch (ReflectiveOperationException | java.io.IOException | IllegalStateException unavailable) {
return true; // An indeterminate registry must not cause another spawn.
}
return false;
}
private static boolean registeredAt(Object npc, World world, NamespacedKey marker, String value)
throws ReflectiveOperationException, java.io.IOException {
return read(npc, "dr-markers") instanceof String text
&& matchesMarker(text, marker, value)
&& npcCall(npc, "getStoredLocation", new Class<?>[0]) instanceof Location location
&& world.equals(location.getWorld());
}
public static boolean removeRegistered(World world, NamespacedKey marker, String value) {
try {
Object registry = registry();
var matches = new ArrayList<Object>();
for (Object npc : (Iterable<?>) registry) {
if (registeredAt(npc, world, marker, value)) matches.add(npc);
}
for (Object npc : matches) npcCall(npc, "destroy", new Class<?>[0]);
api("net.citizensnpcs.api.npc.NPCRegistry").getMethod("saveToStore").invoke(registry);
for (Object npc : (Iterable<?>) registry) {
if (registeredAt(npc, world, marker, value)) return false;
}
return true;
} catch (ReflectiveOperationException | java.io.IOException | IllegalStateException unavailable) {
return false;
}
}
/** True when Citizens is enabled and its registry is ready. */
public static boolean ready() {
try { registry(); return true; } catch (ReflectiveOperationException | IllegalStateException unavailable) { return false; }
}
public static void walk(Entity entity, Location target) {
try {
Object npc = citizen(entity);
@ -199,6 +294,7 @@ public final class CitizensBridge implements Listener {
String name = legacy.getCustomName() == null ? "DuneRealms contact" : ChatColor.stripColor(legacy.getCustomName());
npc = api("net.citizensnpcs.api.npc.NPCRegistry").getMethod("createNPC", EntityType.class, String.class).invoke(registry, EntityType.PLAYER, name);
write(npc, "dr-id", stable); write(npc, "dr-owner", plugin.getName()); write(npc, "dr-original", identity(legacy).toString());
if (!marker.toString().equals("dunecore:smuggler")) noDefaultSkin(npc);
Properties markers = new Properties();
for (NamespacedKey key : legacy.getPersistentDataContainer().getKeys()) {
String text = legacy.getPersistentDataContainer().has(key, PersistentDataType.STRING) ? legacy.getPersistentDataContainer().get(key, PersistentDataType.STRING) : null;