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
|
|
|
|
2013-01-14 05:02:11 +01:00
|
|
|
import com.gmail.nossr50.datatypes.InactiveChunk;
|
2012-06-28 23:20:03 +02:00
|
|
|
import com.gmail.nossr50.mcMMO;
|
|
|
|
|
|
|
|
public class ChunkletUnloader implements Runnable {
|
2013-01-14 05:02:11 +01:00
|
|
|
private static Map<String, InactiveChunk> unloadedChunks = new HashMap<String, InactiveChunk>();
|
2012-06-28 23:20:03 +02:00
|
|
|
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) {
|
2013-01-14 05:02:11 +01:00
|
|
|
if (chunk == null || chunk.getWorld() == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
String key = chunk.getWorld().getName() + "," + chunk.getX() + "," + chunk.getZ();
|
2012-06-28 23:20:03 +02:00
|
|
|
|
2013-01-14 05:02:11 +01:00
|
|
|
if (unloadedChunks.containsKey(key))
|
|
|
|
return;
|
|
|
|
|
|
|
|
unloadedChunks.put(key, new InactiveChunk(chunk));
|
2012-06-28 23:20:03 +02:00
|
|
|
}
|
|
|
|
|
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-14 05:02:11 +01:00
|
|
|
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;
|
2012-06-28 23:20:03 +02:00
|
|
|
|
|
|
|
if (!chunk.isLoaded()) {
|
2013-01-14 05:02:11 +01:00
|
|
|
int inactiveTime = entry.getValue().inactiveTime + RUN_INTERVAL;
|
2012-06-28 23:20:03 +02:00
|
|
|
|
|
|
|
//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)
|
2013-01-14 05:02:11 +01:00
|
|
|
return;
|
2012-11-13 23:27:58 +01:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-01-14 05:02:11 +01:00
|
|
|
entry.getValue().inactiveTime = inactiveTime;
|
2012-06-28 23:20:03 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|