|
@@ -8,23 +8,24 @@ import java.util.Map.Entry;
|
|
|
import org.bukkit.Chunk;
|
|
|
import org.bukkit.World;
|
|
|
|
|
|
+import com.gmail.nossr50.datatypes.InactiveChunk;
|
|
|
import com.gmail.nossr50.mcMMO;
|
|
|
|
|
|
public class ChunkletUnloader implements Runnable {
|
|
|
- private static Map<Chunk, Integer> unloadedChunks = new HashMap<Chunk, Integer>();
|
|
|
+ private static Map<String, InactiveChunk> unloadedChunks = new HashMap<String, InactiveChunk>();
|
|
|
private static int minimumInactiveTime = 60; //Should be a multiple of RUN_INTERVAL for best performance
|
|
|
public static final int RUN_INTERVAL = 20;
|
|
|
|
|
|
public static void addToList(Chunk chunk) {
|
|
|
- //Unfortunately we can't use Map.contains() because Chunks are always new objects
|
|
|
- //This method isn't efficient enough for me
|
|
|
- for (Chunk otherChunk : unloadedChunks.keySet()) {
|
|
|
- if (chunk.getX() == otherChunk.getX() && chunk.getZ() == otherChunk.getZ()) {
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
+ if (chunk == null || chunk.getWorld() == null)
|
|
|
+ return;
|
|
|
+
|
|
|
+ String key = chunk.getWorld().getName() + "," + chunk.getX() + "," + chunk.getZ();
|
|
|
|
|
|
- unloadedChunks.put(chunk, 0);
|
|
|
+ if (unloadedChunks.containsKey(key))
|
|
|
+ return;
|
|
|
+
|
|
|
+ unloadedChunks.put(key, new InactiveChunk(chunk));
|
|
|
}
|
|
|
|
|
|
public static void addToList(int cx, int cz, World world) {
|
|
@@ -33,24 +34,35 @@ public class ChunkletUnloader implements Runnable {
|
|
|
|
|
|
@Override
|
|
|
public void run() {
|
|
|
- for (Iterator<Entry<Chunk, Integer>> unloadedChunkIterator = unloadedChunks.entrySet().iterator() ; unloadedChunkIterator.hasNext() ; ) {
|
|
|
- Entry<Chunk, Integer> entry = unloadedChunkIterator.next();
|
|
|
- Chunk chunk = entry.getKey();
|
|
|
+ for (Iterator<Entry<String, InactiveChunk>> unloadedChunkIterator = unloadedChunks.entrySet().iterator() ; unloadedChunkIterator.hasNext() ; ) {
|
|
|
+ Entry<String, InactiveChunk> entry = unloadedChunkIterator.next();
|
|
|
+
|
|
|
+ if (entry.getKey() == null || entry.getValue() == null) {
|
|
|
+ unloadedChunkIterator.remove();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (entry.getValue().chunk == null) {
|
|
|
+ unloadedChunkIterator.remove();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ Chunk chunk = entry.getValue().chunk;
|
|
|
|
|
|
if (!chunk.isLoaded()) {
|
|
|
- int inactiveTime = entry.getValue() + RUN_INTERVAL;
|
|
|
+ int inactiveTime = entry.getValue().inactiveTime + RUN_INTERVAL;
|
|
|
|
|
|
//Chunklets are unloaded only if their chunk has been unloaded for minimumInactiveTime
|
|
|
if (inactiveTime >= minimumInactiveTime) {
|
|
|
if (mcMMO.placeStore == null)
|
|
|
- continue;
|
|
|
+ return;
|
|
|
|
|
|
mcMMO.placeStore.unloadChunk(chunk.getX(), chunk.getZ(), chunk.getWorld());
|
|
|
unloadedChunkIterator.remove();
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- unloadedChunks.put(entry.getKey(), inactiveTime);
|
|
|
+ entry.getValue().inactiveTime = inactiveTime;
|
|
|
}
|
|
|
else {
|
|
|
//Just remove the entry if the chunk has been reloaded.
|