From bc946e0892d04a20e3d35df7cd2e053bab2abe54 Mon Sep 17 00:00:00 2001 From: fullwall Date: Fri, 2 Mar 2012 18:59:40 +0800 Subject: [PATCH] Don't keep chunk references around --- .../java/net/citizensnpcs/EventListen.java | 53 +++++++++---------- .../java/net/citizensnpcs/util/ByIdArray.java | 8 ++- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/main/java/net/citizensnpcs/EventListen.java b/src/main/java/net/citizensnpcs/EventListen.java index 496dd53b1..9b49b1357 100644 --- a/src/main/java/net/citizensnpcs/EventListen.java +++ b/src/main/java/net/citizensnpcs/EventListen.java @@ -1,10 +1,5 @@ package net.citizensnpcs; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.trait.trait.Owner; @@ -34,9 +29,13 @@ import org.bukkit.event.world.ChunkUnloadEvent; import org.bukkit.event.world.WorldLoadEvent; import org.bukkit.event.world.WorldUnloadEvent; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.ListMultimap; +import com.google.gson.internal.Pair; + public class EventListen implements Listener { private volatile CitizensNPCManager npcManager; - private final Map> toRespawn = new HashMap>(); + private final ListMultimap, Integer> toRespawn = ArrayListMultimap.create(); public EventListen(CitizensNPCManager npcManager) { this.npcManager = npcManager; @@ -47,13 +46,14 @@ public class EventListen implements Listener { */ @EventHandler public void onChunkLoad(ChunkLoadEvent event) { - if (!toRespawn.containsKey(event.getChunk())) + Pair coord = toIntPair(event.getChunk()); + if (!toRespawn.containsKey(coord)) return; - for (int id : toRespawn.get(event.getChunk())) { + for (int id : toRespawn.get(coord)) { NPC npc = npcManager.getNPC(id); npc.spawn(npc.getTrait(SpawnLocation.class).getLocation()); } - toRespawn.remove(event.getChunk()); + toRespawn.removeAll(coord); } @EventHandler @@ -61,7 +61,7 @@ public class EventListen implements Listener { if (event.isCancelled()) return; - List respawn = new ArrayList(); + Pair coord = toIntPair(event.getChunk()); for (NPC npc : npcManager) { if (!npc.isSpawned()) continue; @@ -70,11 +70,9 @@ public class EventListen implements Listener { && event.getChunk().getZ() == loc.getChunk().getZ()) { npc.getTrait(SpawnLocation.class).setLocation(loc); npc.despawn(); - respawn.add(npc.getId()); + toRespawn.put(coord, npc.getId()); } } - if (respawn.size() > 0) - toRespawn.put(event.getChunk(), respawn); } /* @@ -103,8 +101,7 @@ public class EventListen implements Listener { NPC npc = npcManager.getNPC(event.getEntity()); Player player = (Player) event.getTarget(); - if (!player.hasMetadata("selected") || player.getMetadata("selected").size() == 0 - || player.getMetadata("selected").get(0).asInt() != npc.getId()) { + if (player.getMetadata("selected").size() == 0 || player.getMetadata("selected").get(0).asInt() != npc.getId()) { if (player.getItemInHand().getTypeId() == Setting.SELECTION_ITEM.asInt() && (npc.getTrait(Owner.class).getOwner().equals(player.getName()) || player .hasPermission("citizens.admin"))) { @@ -146,14 +143,14 @@ public class EventListen implements Listener { @EventHandler public void onWorldLoad(WorldLoadEvent event) { - for (Chunk chunk : toRespawn.keySet()) { - if (event.getWorld().isChunkLoaded(chunk)) { - for (int id : toRespawn.get(chunk)) { - NPC npc = npcManager.getNPC(id); - npc.spawn(npc.getTrait(SpawnLocation.class).getLocation()); - } - toRespawn.remove(chunk); + for (Pair chunk : toRespawn.keySet()) { + if (!event.getWorld().isChunkLoaded(chunk.first, chunk.second)) + continue; + for (int id : toRespawn.get(chunk)) { + NPC npc = npcManager.getNPC(id); + npc.spawn(npc.getTrait(SpawnLocation.class).getLocation()); } + toRespawn.removeAll(chunk); } } @@ -168,13 +165,11 @@ public class EventListen implements Listener { Location loc = npc.getBukkitEntity().getLocation(); npc.getTrait(SpawnLocation.class).setLocation(loc); npc.despawn(); - if (toRespawn.containsKey(loc.getChunk())) - toRespawn.get(loc.getChunk()).add(npc.getId()); - else { - List respawn = new ArrayList(); - respawn.add(npc.getId()); - toRespawn.put(loc.getChunk(), respawn); - } + toRespawn.put(toIntPair(loc.getChunk()), npc.getId()); } } + + private Pair toIntPair(Chunk chunk) { + return new Pair(chunk.getX(), chunk.getZ()); + } } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/util/ByIdArray.java b/src/main/java/net/citizensnpcs/util/ByIdArray.java index 183b32cd8..a413b2ec0 100644 --- a/src/main/java/net/citizensnpcs/util/ByIdArray.java +++ b/src/main/java/net/citizensnpcs/util/ByIdArray.java @@ -14,7 +14,7 @@ public class ByIdArray implements Iterable { private int size; public ByIdArray() { - this(65535); + this(1000); } public ByIdArray(int capacity) { @@ -124,7 +124,7 @@ public class ByIdArray implements Iterable { if (index < lowest) lowest = index; - ensureCapacity(index + 1); + ensureCapacity(index + 2); elementData[index] = t; ++size; @@ -164,4 +164,8 @@ public class ByIdArray implements Iterable { if (elementData.length > highest) elementData = Arrays.copyOf(elementData, highest + 1); } + + public static ByIdArray create() { + return new ByIdArray(); + } } \ No newline at end of file