2012-06-28 23:20:03 +02:00
|
|
|
package com.gmail.nossr50.runnables;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
|
|
|
import org.bukkit.Chunk;
|
2012-07-07 20:42:35 +02:00
|
|
|
import org.bukkit.World;
|
2012-06-28 23:20:03 +02:00
|
|
|
|
|
|
|
import com.gmail.nossr50.mcMMO;
|
|
|
|
|
|
|
|
public class ChunkletUnloader implements Runnable {
|
|
|
|
private static Map<Chunk, Integer> unloadedChunks = new HashMap<Chunk, Integer>();
|
|
|
|
private static int minimumInactiveTime = 60; //Should be a multiple of RUN_INTERVAL for best performance
|
2012-07-06 06:12:17 +02:00
|
|
|
public static final int RUN_INTERVAL = 20;
|
2012-06-28 23:20:03 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unloadedChunks.put(chunk, 0);
|
|
|
|
}
|
|
|
|
|
2012-07-07 20:42:35 +02:00
|
|
|
public static void addToList(int cx, int cz, World world) {
|
|
|
|
addToList(world.getChunkAt(cx, cz));
|
|
|
|
}
|
|
|
|
|
2012-06-28 23:20:03 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2013-01-10 04:18:41 +01:00
|
|
|
for (Iterator<Entry<Chunk, Integer>> unloadedChunkIterator = unloadedChunks.entrySet().iterator() ; unloadedChunkIterator.hasNext() ; ) {
|
|
|
|
Entry<Chunk, Integer> entry = unloadedChunkIterator.next();
|
2012-06-28 23:20:03 +02:00
|
|
|
Chunk chunk = entry.getKey();
|
|
|
|
|
|
|
|
if (!chunk.isLoaded()) {
|
|
|
|
int inactiveTime = entry.getValue() + RUN_INTERVAL;
|
|
|
|
|
|
|
|
//Chunklets are unloaded only if their chunk has been unloaded for minimumInactiveTime
|
|
|
|
if (inactiveTime >= minimumInactiveTime) {
|
2013-01-10 05:03:17 +01:00
|
|
|
if (mcMMO.placeStore == null)
|
2012-11-13 23:27:58 +01:00
|
|
|
continue;
|
|
|
|
|
2012-12-24 22:17:19 +01:00
|
|
|
mcMMO.placeStore.unloadChunk(chunk.getX(), chunk.getZ(), chunk.getWorld());
|
2013-01-10 04:18:41 +01:00
|
|
|
unloadedChunkIterator.remove();
|
2012-06-28 23:20:03 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
unloadedChunks.put(entry.getKey(), inactiveTime);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//Just remove the entry if the chunk has been reloaded.
|
2013-01-10 04:18:41 +01:00
|
|
|
unloadedChunkIterator.remove();
|
2012-06-28 23:20:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|