From 52a004fcaff110cfd20a0d2bdf96fc87a742c726 Mon Sep 17 00:00:00 2001 From: fullwall Date: Thu, 4 Feb 2021 10:10:48 +0800 Subject: [PATCH] Use and implement new registry API --- .../main/java/net/citizensnpcs/Citizens.java | 41 ++++++++++--------- .../citizensnpcs/npc/CitizensNPCRegistry.java | 20 +++++++++ .../net/citizensnpcs/trait/HologramTrait.java | 2 +- .../trait/waypoint/EntityMarkers.java | 2 +- 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/Citizens.java b/main/src/main/java/net/citizensnpcs/Citizens.java index 596439373..39d7c2440 100644 --- a/main/src/main/java/net/citizensnpcs/Citizens.java +++ b/main/src/main/java/net/citizensnpcs/Citizens.java @@ -1,6 +1,7 @@ package net.citizensnpcs; import java.io.File; +import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Locale; @@ -73,6 +74,7 @@ import net.milkbowl.vault.economy.Economy; public class Citizens extends JavaPlugin implements CitizensPlugin { private final List anonymousRegistries = Lists.newArrayList(); + private final List citizensBackedRegistries = Lists.newArrayList(); private final CommandManager commands = new CommandManager(); private boolean compatible; private Settings config; @@ -105,6 +107,13 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { return anon; } + @Override + public NPCRegistry createCitizensBackedNPCRegistry(NPCDataStore store) { + CitizensNPCRegistry anon = new CitizensNPCRegistry(store, "anonymous-citizens-" + UUID.randomUUID().toString()); + citizensBackedRegistries.add(anon); + return anon; + } + @Override public NPCRegistry createNamedNPCRegistry(String name, NPCDataStore store) { NPCRegistry created = new CitizensNPCRegistry(store, name); @@ -127,17 +136,12 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { return SimpleNPCDataStore.create(saves); } - private void despawnNPCs() { - Iterator itr = npcRegistry.iterator(); - while (itr.hasNext()) { - NPC npc = itr.next(); - try { - npc.despawn(DespawnReason.RELOAD); - } catch (Throwable e) { - e.printStackTrace(); - // ensure that all entities are despawned + private void despawnNPCs(boolean save) { + for (NPCRegistry reg : Iterables.concat(Arrays.asList(npcRegistry), citizensBackedRegistries)) { + if (save) { + reg.saveToStore(); } - itr.remove(); + reg.despawnNPCs(DespawnReason.RELOAD); } } @@ -204,7 +208,9 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { @Override public NPCRegistry next() { if (stored == null) { - stored = Iterables.concat(storedRegistries.values(), anonymousRegistries).iterator(); + stored = Iterables + .concat(storedRegistries.values(), anonymousRegistries, citizensBackedRegistries) + .iterator(); return npcRegistry; } return stored.next(); @@ -275,9 +281,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { Editor.leaveAll(); if (compatible) { - saves.storeAll(npcRegistry); - saves.saveToDiskImmediate(); - despawnNPCs(); + despawnNPCs(true); npcRegistry = null; NMS.shutdown(); } @@ -374,7 +378,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { public void reload() throws NPCLoadException { Editor.leaveAll(); config.reload(); - despawnNPCs(); + despawnNPCs(false); ProfileFetcher.reset(); Skin.clearCache(); getServer().getPluginManager().callEvent(new CitizensPreReloadEvent()); @@ -399,13 +403,10 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { if (store == null) { throw new IllegalArgumentException("must be non-null"); } - if (saves != null) { - saves.storeAll(npcRegistry); - saves.saveToDiskImmediate(); - despawnNPCs(); - } + despawnNPCs(true); this.saves = store; this.npcRegistry = new CitizensNPCRegistry(saves, "citizens-global-" + UUID.randomUUID().toString()); + saves.loadInto(npcRegistry); } private void setupEconomy() { diff --git a/main/src/main/java/net/citizensnpcs/npc/CitizensNPCRegistry.java b/main/src/main/java/net/citizensnpcs/npc/CitizensNPCRegistry.java index 704c84f9d..3dcea86e0 100644 --- a/main/src/main/java/net/citizensnpcs/npc/CitizensNPCRegistry.java +++ b/main/src/main/java/net/citizensnpcs/npc/CitizensNPCRegistry.java @@ -99,6 +99,20 @@ public class CitizensNPCRegistry implements NPCRegistry { } } + @Override + public void despawnNPCs(DespawnReason reason) { + Iterator itr = iterator(); + while (itr.hasNext()) { + NPC npc = itr.next(); + try { + npc.despawn(reason); + } catch (Throwable e) { + e.printStackTrace(); // ensure that all entities are despawned + } + itr.remove(); + } + } + private int generateUniqueId() { return saves.createUniqueNPCId(this); } @@ -182,6 +196,12 @@ public class CitizensNPCRegistry implements NPCRegistry { }; } + @Override + public void saveToStore() { + saves.storeAll(this); + saves.saveToDiskImmediate(); + } + @Override public Iterable sorted() { List vals = new ArrayList(npcs.valueCollection()); diff --git a/main/src/main/java/net/citizensnpcs/trait/HologramTrait.java b/main/src/main/java/net/citizensnpcs/trait/HologramTrait.java index fcecbcb48..16838d3cf 100644 --- a/main/src/main/java/net/citizensnpcs/trait/HologramTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/HologramTrait.java @@ -35,7 +35,7 @@ public class HologramTrait extends Trait { @Persist private final List lines = Lists.newArrayList(); private NPC nameNPC; - private final NPCRegistry registry = CitizensAPI.createAnonymousNPCRegistry(new MemoryNPCDataStore()); + private final NPCRegistry registry = CitizensAPI.createCitizensBackedNPCRegistry(new MemoryNPCDataStore()); public HologramTrait() { super("hologramtrait"); diff --git a/main/src/main/java/net/citizensnpcs/trait/waypoint/EntityMarkers.java b/main/src/main/java/net/citizensnpcs/trait/waypoint/EntityMarkers.java index ba373d448..3d71b2214 100644 --- a/main/src/main/java/net/citizensnpcs/trait/waypoint/EntityMarkers.java +++ b/main/src/main/java/net/citizensnpcs/trait/waypoint/EntityMarkers.java @@ -21,7 +21,7 @@ import net.citizensnpcs.npc.ai.NPCHolder; */ public class EntityMarkers { private final Map markers = Maps.newHashMap(); - private final NPCRegistry registry = CitizensAPI.createAnonymousNPCRegistry(new MemoryNPCDataStore()); + private final NPCRegistry registry = CitizensAPI.createCitizensBackedNPCRegistry(new MemoryNPCDataStore()); private EntityType type; public EntityMarkers() {