More spawn/despawn debug

This commit is contained in:
fullwall 2013-01-04 15:18:12 +08:00
parent dc4fb0a33e
commit f1917117e6
2 changed files with 17 additions and 9 deletions

View File

@ -92,8 +92,8 @@ public class EventListen implements Listener {
return; return;
} }
toRespawn.put(coord, npc.getId()); toRespawn.put(coord, npc.getId());
Messaging.debug("Despawned id ", npc.getId(), "due to chunk unload at [" + coord.x + "," + coord.z Messaging
+ "]"); .debug("Despawned id", npc.getId(), "due to chunk unload at [" + coord.x + "," + coord.z + "]");
} }
} }
} }
@ -268,22 +268,23 @@ public class EventListen implements Listener {
int id = ids.get(i); int id = ids.get(i);
boolean success = spawn(id); boolean success = spawn(id);
if (!success) { if (!success) {
Messaging.debug("Couldn't respawn id " + id + " during chunk event at [" + coord.x + "," + coord.z Messaging.debug("Couldn't respawn id", id, "during chunk event at [" + coord.x + "," + coord.z + "]");
+ "]");
continue; continue;
} }
ids.remove(i); ids.remove(i);
Messaging.debug("Spawned id ", id, "due to chunk event at [" + coord.x + "," + coord.z + "]"); Messaging.debug("Spawned id", id, "due to chunk event at [" + coord.x + "," + coord.z + "]");
} }
} }
private boolean spawn(int id) { private boolean spawn(int id) {
NPC npc = npcRegistry.getById(id); NPC npc = npcRegistry.getById(id);
if (npc == null) if (npc == null) {
Messaging.debug("Couldn't despawn unknown NPC id", id);
return false; return false;
}
Location spawn = npc.getTrait(CurrentLocation.class).getLocation(); Location spawn = npc.getTrait(CurrentLocation.class).getLocation();
if (spawn == null) { if (spawn == null) {
Messaging.debug("Couldn't find a spawn location for despawned NPC ID: " + id); Messaging.debug("Couldn't find a spawn location for despawned NPC id", id);
return false; return false;
} }
return npc.spawn(spawn); return npc.spawn(spawn);

View File

@ -50,8 +50,10 @@ public class CitizensNPC extends AbstractNPC {
@Override @Override
public boolean despawn(DespawnReason reason) { public boolean despawn(DespawnReason reason) {
if (!isSpawned()) if (!isSpawned()) {
Messaging.debug("Tried to despawn", getId(), "while already despawned.");
return false; return false;
}
NPCDespawnEvent event = new NPCDespawnEvent(this, reason); NPCDespawnEvent event = new NPCDespawnEvent(this, reason);
if (reason == DespawnReason.CHUNK_UNLOAD) if (reason == DespawnReason.CHUNK_UNLOAD)
@ -59,6 +61,7 @@ public class CitizensNPC extends AbstractNPC {
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) { if (event.isCancelled()) {
getBukkitEntity().getLocation().getChunk().load(); getBukkitEntity().getLocation().getChunk().load();
Messaging.debug("Couldn't despawn", getId(), "due to despawn event cancellation. Force loaded chunk.");
return false; return false;
} }
boolean keepSelected = getTrait(Spawned.class).shouldSpawn(); boolean keepSelected = getTrait(Spawned.class).shouldSpawn();
@ -190,13 +193,16 @@ public class CitizensNPC extends AbstractNPC {
@Override @Override
public boolean spawn(Location at) { public boolean spawn(Location at) {
Preconditions.checkNotNull(at, "location cannot be null"); Preconditions.checkNotNull(at, "location cannot be null");
if (isSpawned()) if (isSpawned()) {
Messaging.debug("Tried to spawn", getId(), "while already spawned.");
return false; return false;
}
entityController.spawn(at, this); entityController.spawn(at, this);
EntityLiving mcEntity = getHandle(); EntityLiving mcEntity = getHandle();
boolean couldSpawn = !Util.isLoaded(at) ? false : mcEntity.world.addEntity(mcEntity, SpawnReason.CUSTOM); boolean couldSpawn = !Util.isLoaded(at) ? false : mcEntity.world.addEntity(mcEntity, SpawnReason.CUSTOM);
if (!couldSpawn) { if (!couldSpawn) {
Messaging.debug("Retrying spawn of", getId(), "later due to chunk being unloaded.");
// we need to wait for a chunk load before trying to spawn // we need to wait for a chunk load before trying to spawn
mcEntity = null; mcEntity = null;
EventListen.addForRespawn(at, getId()); EventListen.addForRespawn(at, getId());
@ -207,6 +213,7 @@ public class CitizensNPC extends AbstractNPC {
Bukkit.getPluginManager().callEvent(spawnEvent); Bukkit.getPluginManager().callEvent(spawnEvent);
if (spawnEvent.isCancelled()) { if (spawnEvent.isCancelled()) {
mcEntity = null; mcEntity = null;
Messaging.debug("Couldn't spawn", getId(), "due to event cancellation.");
return false; return false;
} }