Skip to content

Commit

Permalink
Add more info to debug messages to help with debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Mar 13, 2022
1 parent b5f1796 commit 4bf4dc0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
32 changes: 16 additions & 16 deletions main/src/main/java/net/citizensnpcs/EventListen.java
Expand Up @@ -151,15 +151,15 @@ Iterables.<NPC> concat(CitizensAPI.getNPCRegistry(), Iterables.concat(registries

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChunkLoad(ChunkLoadEvent event) {
ChunkCoord coord = new ChunkCoord(event.getChunk());
Runnable runnable = new Runnable() {
@Override
public void run() {
ChunkCoord coord = new ChunkCoord(event.getChunk());
respawnAllFromCoord(coord, event);
}
};
if (Messaging.isDebugging() && Setting.DEBUG_CHUNK_LOADS.asBoolean()) {
new Exception("CITIZENS CHUNK LOAD DEBUG " + new ChunkCoord(event.getChunk())).printStackTrace();
if (Messaging.isDebugging() && Setting.DEBUG_CHUNK_LOADS.asBoolean() && toRespawn.containsKey(coord)) {
new Exception("CITIZENS CHUNK LOAD DEBUG " + coord).printStackTrace();
}
if (event instanceof Cancellable) {
runnable.run();
Expand All @@ -185,7 +185,7 @@ public void onChunkUnload(final ChunkUnloadEvent event) {
if (!npc.despawn(DespawnReason.CHUNK_UNLOAD)) {
if (!(event instanceof Cancellable)) {
if (Messaging.isDebugging()) {
Messaging.debug("Reloading chunk because", npc.getId(), "couldn't despawn");
Messaging.debug("Reloading chunk because", npc, "couldn't despawn");
}
loadChunk = true;
toRespawn.put(coord, npc);
Expand All @@ -198,7 +198,7 @@ public void onChunkUnload(final ChunkUnloadEvent event) {
}
toRespawn.put(coord, npc);
if (Messaging.isDebugging()) {
Messaging.debug("Despawned id", npc.getId(), "due to chunk unload at", coord);
Messaging.debug("Despawned", npc, "due to chunk unload at", coord);
}
}
if (Messaging.isDebugging() && Setting.DEBUG_CHUNK_LOADS.asBoolean()) {
Expand Down Expand Up @@ -416,7 +416,7 @@ public void onNeedsRespawn(NPCNeedsRespawnEvent event) {
ChunkCoord coord = new ChunkCoord(event.getSpawnLocation());
if (toRespawn.containsEntry(coord, event.getNPC()))
return;
Messaging.debug("Stored", event.getNPC().getId(), "for respawn from NPCNeedsRespawnEvent");
Messaging.debug("Stored", event.getNPC(), "for respawn from NPCNeedsRespawnEvent");
toRespawn.put(coord, event.getNPC());
}

Expand All @@ -425,13 +425,13 @@ public void onNPCDespawn(NPCDespawnEvent event) {
if (event.getReason() == DespawnReason.PLUGIN || event.getReason() == DespawnReason.REMOVAL
|| event.getReason() == DespawnReason.RELOAD) {
if (Messaging.isDebugging()) {
Messaging.debug("Preventing further respawns of", event.getNPC().getId(),
Messaging.debug("Preventing further respawns of", event.getNPC(),
"due to DespawnReason." + event.getReason());
}
toRespawn.values().remove(event.getNPC());
} else if (Messaging.isDebugging()) {
Messaging.debug("Removing " + event.getNPC().getId() + " from skin tracker due to DespawnReason."
+ event.getReason().name());
Messaging.debug("Removing", event.getNPC(),
"from skin tracker due to DespawnReason." + event.getReason().name());
}
skinUpdateTracker.onNPCDespawn(event.getNPC());
}
Expand All @@ -445,7 +445,7 @@ public void onNPCRemove(NPCRemoveEvent event) {
public void onNPCSpawn(NPCSpawnEvent event) {
skinUpdateTracker.onNPCSpawn(event.getNPC());
if (Messaging.isDebugging()) {
Messaging.debug("Removing respawns of", event.getNPC().getId(), "due to SpawnReason." + event.getReason());
Messaging.debug("Removing respawns of", event.getNPC(), "due to SpawnReason." + event.getReason());
}
toRespawn.values().remove(event.getNPC());
}
Expand Down Expand Up @@ -681,7 +681,7 @@ public void onWorldUnload(WorldUnloadEvent event) {
}
if (npc.isSpawned()) {
toRespawn.put(new ChunkCoord(npc.getEntity().getLocation()), npc);
Messaging.debug("Despawned", npc.getId() + "due to world unload at", event.getWorld().getName());
Messaging.debug("Despawned", npc, "due to world unload at", event.getWorld().getName());
}
}
}
Expand All @@ -695,26 +695,26 @@ private void respawnAllFromCoord(ChunkCoord coord, Event event) {
NPC npc = ids.get(i);
if (npc.getOwningRegistry().getById(npc.getId()) != npc) {
if (Messaging.isDebugging()) {
Messaging.debug("Prevented deregistered NPC from respawning", npc.getId());
Messaging.debug("Prevented deregistered NPC from respawning", npc);
}
continue;
}
if (npc.isSpawned()) {
if (Messaging.isDebugging()) {
Messaging.debug("Can't respawn NPC", npc.getId(), ": already spawned");
Messaging.debug("Can't respawn NPC", npc, ": already spawned");
}
continue;
}
boolean success = spawn(npc);
if (!success) {
ids.remove(i--);
if (Messaging.isDebugging()) {
Messaging.debug("Couldn't respawn id", npc.getId(), "during", event, "at", coord);
Messaging.debug("Couldn't respawn", npc, "during", event, "at", coord);
}
continue;
}
if (Messaging.isDebugging()) {
Messaging.debug("Spawned id", npc.getId(), "during", event, "at", coord);
Messaging.debug("Spawned", npc, "during", event, "at", coord);
}
}
for (NPC npc : ids) {
Expand All @@ -726,7 +726,7 @@ private boolean spawn(NPC npc) {
Location spawn = npc.getOrAddTrait(CurrentLocation.class).getLocation();
if (spawn == null) {
if (Messaging.isDebugging()) {
Messaging.debug("Couldn't find a spawn location for despawned NPC id", npc.getId());
Messaging.debug("Couldn't find a spawn location for despawned NPC", npc);
}
return false;
}
Expand Down
25 changes: 15 additions & 10 deletions main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java
Expand Up @@ -69,7 +69,7 @@ public CitizensNPC(UUID uuid, int id, String name, EntityController entityContro
@Override
public boolean despawn(DespawnReason reason) {
if (!isSpawned() && reason != DespawnReason.DEATH) {
Messaging.debug("Tried to despawn", getId(), "while already despawned, DespawnReason." + reason);
Messaging.debug("Tried to despawn", toString(), "while already despawned, DespawnReason." + reason);
if (reason == DespawnReason.RELOAD) {
unloadEvents();
}
Expand All @@ -81,7 +81,7 @@ public boolean despawn(DespawnReason reason) {
}
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled() && reason != DespawnReason.DEATH) {
Messaging.debug("Couldn't despawn", getId(), "due to despawn event cancellation. Will load chunk.",
Messaging.debug("Couldn't despawn", toString(), "due to despawn event cancellation. Will load chunk.",
getEntity().isValid(), ", DespawnReason." + reason);
return false;
}
Expand All @@ -99,7 +99,7 @@ public boolean despawn(DespawnReason reason) {
for (Trait trait : new ArrayList<Trait>(traits.values())) {
trait.onDespawn();
}
Messaging.debug("Despawned", getId(), "DespawnReason." + reason);
Messaging.debug("Despawned", toString(), "DespawnReason." + reason);
if (reason == DespawnReason.DEATH) {
entityController.setEntity(null);
} else {
Expand Down Expand Up @@ -161,7 +161,7 @@ public void load(final DataKey root) {
if (spawnLocation.getLocation() != null) {
spawn(spawnLocation.getLocation(), SpawnReason.RESPAWN);
} else {
Messaging.debug("Tried to spawn", getId(), "on load but world was null");
Messaging.debug("Tried to spawn", toString(), "on load but world was null");
}
}

Expand Down Expand Up @@ -240,11 +240,11 @@ public boolean spawn(Location at, SpawnReason reason) {
Preconditions.checkNotNull(at, "location cannot be null");
Preconditions.checkNotNull(reason, "reason cannot be null");
if (getEntity() != null) {
Messaging.debug("Tried to spawn", getId(), "while already spawned. SpawnReason." + reason);
Messaging.debug("Tried to spawn", toString(), "while already spawned. SpawnReason." + reason);
return false;
}
if (at.getWorld() == null) {
Messaging.debug("Tried to spawn", getId(), "but the world was null. SpawnReason." + reason);
Messaging.debug("Tried to spawn", toString(), "but the world was null. SpawnReason." + reason);
return false;
}
at = at.clone();
Expand Down Expand Up @@ -272,8 +272,8 @@ public boolean spawn(Location at, SpawnReason reason) {

if (!couldSpawn) {
if (Messaging.isDebugging()) {
Messaging.debug("Retrying spawn of", getId(), "later, SpawnReason." + reason + ". Was loaded", loaded,
"is loaded", Util.isLoaded(at));
Messaging.debug("Retrying spawn of", toString(), "later, SpawnReason." + reason + ". Was loaded",
loaded, "is loaded", Util.isLoaded(at));
}
// we need to wait before trying to spawn
entityController.remove();
Expand All @@ -300,7 +300,7 @@ public boolean spawn(Location at, SpawnReason reason) {

if (spawnEvent.isCancelled()) {
entityController.remove();
Messaging.debug("Couldn't spawn", getId(), "SpawnReason." + reason + " due to event cancellation.");
Messaging.debug("Couldn't spawn", toString(), "SpawnReason." + reason + " due to event cancellation.");
return false;
}

Expand Down Expand Up @@ -337,7 +337,7 @@ public boolean spawn(Location at, SpawnReason reason) {
updateCustomNameVisibility();
updateCustomName();

Messaging.debug("Spawned", getId(), "SpawnReason." + reason);
Messaging.debug("Spawned", toString(), "SpawnReason." + reason);
return true;
}

Expand All @@ -352,6 +352,11 @@ public void teleport(Location location, TeleportCause reason) {
}
}

@Override
public String toString() {
return getId() + "{" + getName() + ", " + getOrAddTrait(MobType.class).getType() + "}";
}

@Override
public void update() {
try {
Expand Down

0 comments on commit 4bf4dc0

Please sign in to comment.