Use and implement new registry API

This commit is contained in:
fullwall 2021-02-04 10:10:48 +08:00
parent 8039f6e9d3
commit 52a004fcaf
4 changed files with 43 additions and 22 deletions

View File

@ -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<NPCRegistry> anonymousRegistries = Lists.newArrayList();
private final List<NPCRegistry> 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<NPC> 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() {

View File

@ -99,6 +99,20 @@ public class CitizensNPCRegistry implements NPCRegistry {
}
}
@Override
public void despawnNPCs(DespawnReason reason) {
Iterator<NPC> 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<NPC> sorted() {
List<NPC> vals = new ArrayList<NPC>(npcs.valueCollection());

View File

@ -35,7 +35,7 @@ public class HologramTrait extends Trait {
@Persist
private final List<String> 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");

View File

@ -21,7 +21,7 @@ import net.citizensnpcs.npc.ai.NPCHolder;
*/
public class EntityMarkers<T> {
private final Map<T, Entity> markers = Maps.newHashMap();
private final NPCRegistry registry = CitizensAPI.createAnonymousNPCRegistry(new MemoryNPCDataStore());
private final NPCRegistry registry = CitizensAPI.createCitizensBackedNPCRegistry(new MemoryNPCDataStore());
private EntityType type;
public EntityMarkers() {