Make /citizens reload behave more sanely with regards to trait listener unloading

This commit is contained in:
fullwall 2016-01-20 18:11:35 +08:00
parent 847652046e
commit 21d3f86b7f
3 changed files with 9 additions and 6 deletions

View File

@ -36,7 +36,6 @@ import net.citizensnpcs.api.npc.SimpleNPCDataStore;
import net.citizensnpcs.api.scripting.EventRegistrar;
import net.citizensnpcs.api.scripting.ObjectProvider;
import net.citizensnpcs.api.scripting.ScriptCompiler;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitFactory;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.NBTStorage;
@ -105,10 +104,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
while (itr.hasNext()) {
NPC npc = itr.next();
try {
npc.despawn(DespawnReason.REMOVAL);
for (Trait trait : npc.getTraits()) {
trait.onDespawn();
}
npc.despawn(DespawnReason.RELOAD);
} catch (Throwable e) {
e.printStackTrace();
// ensure that all entities are despawned

View File

@ -340,7 +340,8 @@ public class EventListen implements Listener {
@EventHandler
public void onNPCDespawn(NPCDespawnEvent event) {
if (event.getReason() == DespawnReason.PLUGIN || event.getReason() == DespawnReason.REMOVAL) {
if (event.getReason() == DespawnReason.PLUGIN || event.getReason() == DespawnReason.REMOVAL
|| event.getReason() == DespawnReason.RELOAD) {
if (event.getNPC().getStoredLocation() != null) {
toRespawn.remove(toCoord(event.getNPC().getStoredLocation()), event.getNPC());
}

View File

@ -64,6 +64,9 @@ public class CitizensNPC extends AbstractNPC {
if (reason == DespawnReason.REMOVAL) {
Bukkit.getPluginManager().callEvent(new NPCDespawnEvent(this, reason));
}
if (reason == DespawnReason.RELOAD) {
unloadEvents();
}
return false;
}
NPCDespawnEvent event = new NPCDespawnEvent(this, reason);
@ -84,6 +87,9 @@ public class CitizensNPC extends AbstractNPC {
trait.onDespawn();
}
navigator.onDespawn();
if (reason == DespawnReason.RELOAD) {
unloadEvents();
}
entityController.remove();
return true;