Better despawning

This commit is contained in:
fullwall 2012-12-30 20:25:49 +08:00
parent b7bb0d8202
commit a0db109984

View File

@ -91,16 +91,15 @@ public class EventListen implements Listener {
if (!npc.isSpawned()) if (!npc.isSpawned())
continue; continue;
location = npc.getBukkitEntity().getLocation(location); location = npc.getBukkitEntity().getLocation(location);
boolean sameChunkCoordinates = coord.z == location.getBlockZ() >> 4 boolean sameChunkCoordinates = coord.z == location.getBlockZ() >> 4 && coord.x == location.getBlockX() >> 4;
&& coord.x == location.getBlockX() >> 4;
if (sameChunkCoordinates && event.getWorld().equals(location.getWorld())) { if (sameChunkCoordinates && event.getWorld().equals(location.getWorld())) {
if (!npc.despawn(DespawnReason.CHUNK_UNLOAD)) { npc.despawn(DespawnReason.CHUNK_UNLOAD);
event.setCancelled(true); if (event.getChunk().isLoaded()) {
continue; toRespawn.removeAll(coord);
return;
} }
toRespawn.put(coord, npc.getId()); toRespawn.put(coord, npc.getId());
Messaging.debug("Despawned", npc.getId(), "due to chunk unload at [" + coord.x + "," Messaging.debug("Despawned", npc.getId(), "due to chunk unload at [" + coord.x + "," + coord.z + "]");
+ coord.z + "]");
} }
} }
} }
@ -124,11 +123,9 @@ public class EventListen implements Listener {
return; return;
event.setCancelled(npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true)); event.setCancelled(npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true));
if (event instanceof EntityCombustByEntityEvent) { if (event instanceof EntityCombustByEntityEvent) {
Bukkit.getPluginManager().callEvent( Bukkit.getPluginManager().callEvent(new NPCCombustByEntityEvent((EntityCombustByEntityEvent) event, npc));
new NPCCombustByEntityEvent((EntityCombustByEntityEvent) event, npc));
} else if (event instanceof EntityCombustByBlockEvent) { } else if (event instanceof EntityCombustByBlockEvent) {
Bukkit.getPluginManager().callEvent( Bukkit.getPluginManager().callEvent(new NPCCombustByBlockEvent((EntityCombustByBlockEvent) event, npc));
new NPCCombustByBlockEvent((EntityCombustByBlockEvent) event, npc));
} else { } else {
Bukkit.getPluginManager().callEvent(new NPCCombustEvent(event, npc)); Bukkit.getPluginManager().callEvent(new NPCCombustEvent(event, npc));
} }
@ -141,8 +138,7 @@ public class EventListen implements Listener {
return; return;
event.setCancelled(npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true)); event.setCancelled(npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true));
if (event instanceof EntityDamageByEntityEvent) { if (event instanceof EntityDamageByEntityEvent) {
NPCDamageByEntityEvent damageEvent = new NPCDamageByEntityEvent(npc, NPCDamageByEntityEvent damageEvent = new NPCDamageByEntityEvent(npc, (EntityDamageByEntityEvent) event);
(EntityDamageByEntityEvent) event);
Bukkit.getPluginManager().callEvent(damageEvent); Bukkit.getPluginManager().callEvent(damageEvent);
if (!damageEvent.isCancelled() || !(damageEvent.getDamager() instanceof Player)) if (!damageEvent.isCancelled() || !(damageEvent.getDamager() instanceof Player))
@ -153,8 +149,7 @@ public class EventListen implements Listener {
NPCLeftClickEvent leftClickEvent = new NPCLeftClickEvent(npc, damager); NPCLeftClickEvent leftClickEvent = new NPCLeftClickEvent(npc, damager);
Bukkit.getPluginManager().callEvent(leftClickEvent); Bukkit.getPluginManager().callEvent(leftClickEvent);
} else if (event instanceof EntityDamageByBlockEvent) { } else if (event instanceof EntityDamageByBlockEvent) {
Bukkit.getPluginManager().callEvent( Bukkit.getPluginManager().callEvent(new NPCDamageByBlockEvent(npc, (EntityDamageByBlockEvent) event));
new NPCDamageByBlockEvent(npc, (EntityDamageByBlockEvent) event));
} else { } else {
Bukkit.getPluginManager().callEvent(new NPCDamageEvent(npc, event)); Bukkit.getPluginManager().callEvent(new NPCDamageEvent(npc, event));
} }
@ -254,8 +249,7 @@ public class EventListen implements Listener {
List<Integer> ids = toRespawn.get(chunk); List<Integer> ids = toRespawn.get(chunk);
for (int i = 0; i < ids.size(); i++) { for (int i = 0; i < ids.size(); i++) {
spawn(ids.get(i)); spawn(ids.get(i));
Messaging Messaging.debug("Spawned", ids.get(0), "due to world " + event.getWorld().getName() + " load");
.debug("Spawned", ids.get(0), "due to world " + event.getWorld().getName() + " load");
} }
toRespawn.removeAll(chunk); toRespawn.removeAll(chunk);
} }