change to chunk loading/unloading

This commit is contained in:
aPunch 2012-02-19 11:45:43 -06:00
parent 2690a5d0aa
commit 1e8375fb04

View File

@ -1,8 +1,9 @@
package net.citizensnpcs; package net.citizensnpcs;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
@ -12,6 +13,7 @@ import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.Messaging;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -25,7 +27,7 @@ import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent; import org.bukkit.event.world.ChunkUnloadEvent;
public class EventListen implements Listener { public class EventListen implements Listener {
private final List<Integer> toRespawn = new ArrayList<Integer>(); private final Map<Chunk, List<Integer>> toRespawn = new HashMap<Chunk, List<Integer>>();
private volatile CitizensNPCManager npcManager; private volatile CitizensNPCManager npcManager;
public EventListen(CitizensNPCManager npcManager) { public EventListen(CitizensNPCManager npcManager) {
@ -37,12 +39,16 @@ public class EventListen implements Listener {
*/ */
@EventHandler @EventHandler
public void onChunkLoad(ChunkLoadEvent event) { public void onChunkLoad(ChunkLoadEvent event) {
Iterator<Integer> itr = toRespawn.iterator(); if (!toRespawn.containsKey(event.getChunk()))
while (itr.hasNext()) { return;
NPC npc = npcManager.getNPC(itr.next()); for (int id : toRespawn.get(event.getChunk())) {
npc.spawn(npc.getTrait(SpawnLocation.class).getLocation()); NPC npc = npcManager.getNPC(id);
itr.remove(); Location loc = npc.getTrait(SpawnLocation.class).getLocation();
npc.spawn(loc);
Messaging.log("Spawned " + npc.getId() + " at " + loc.getX() + ", " + loc.getY() + ", " + loc.getZ()
+ " in world " + loc.getWorld().getName());
} }
toRespawn.remove(event.getChunk());
} }
@EventHandler @EventHandler
@ -50,17 +56,21 @@ public class EventListen implements Listener {
if (event.isCancelled()) if (event.isCancelled())
return; return;
List<Integer> respawn = new ArrayList<Integer>();
for (NPC npc : npcManager) { for (NPC npc : npcManager) {
if (!npc.isSpawned()) if (!npc.isSpawned())
return; return;
Location loc = npc.getBukkitEntity().getLocation(); Location loc = npc.getBukkitEntity().getLocation();
if (event.getWorld().equals(loc.getWorld()) && event.getChunk().getX() == loc.getChunk().getX() if (event.getWorld().equals(loc.getWorld()) && event.getChunk().getX() == loc.getChunk().getX()
&& event.getChunk().getZ() == loc.getChunk().getZ()) { && event.getChunk().getZ() == loc.getChunk().getZ()) {
toRespawn.add(npc.getId()); Messaging.log("Despawned " + npc.getId() + " at " + loc.getX() + ", " + loc.getY() + ", " + loc.getZ()
+ " in world " + loc.getWorld().getName());
npc.getTrait(SpawnLocation.class).setLocation(loc); npc.getTrait(SpawnLocation.class).setLocation(loc);
npc.despawn(); npc.despawn();
respawn.add(npc.getId());
} }
} }
toRespawn.put(event.getChunk(), respawn);
} }
/* /*