mcMMO/src/main/java/com/gmail/nossr50/runnables/ChunkletUnloader.java

62 lines
2.0 KiB
Java
Raw Normal View History

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;
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
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);
}
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() {
for (Iterator<Entry<Chunk, Integer>> it = unloadedChunks.entrySet().iterator() ; it.hasNext() ; ) {
Entry<Chunk, Integer> entry = it.next();
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) {
2012-11-13 23:27:58 +01:00
if(mcMMO.p.placeStore == null)
continue;
mcMMO.p.placeStore.unloadChunk(chunk.getX(), chunk.getZ(), chunk.getWorld());
2012-06-28 23:20:03 +02:00
it.remove();
continue;
}
unloadedChunks.put(entry.getKey(), inactiveTime);
}
else {
//Just remove the entry if the chunk has been reloaded.
it.remove();
}
}
}
}