ChunkletUnloader.java 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package com.gmail.nossr50.runnables;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.Map;
  5. import java.util.Map.Entry;
  6. import org.bukkit.Chunk;
  7. import org.bukkit.World;
  8. import com.gmail.nossr50.mcMMO;
  9. public class ChunkletUnloader implements Runnable {
  10. private static Map<Chunk, Integer> unloadedChunks = new HashMap<Chunk, Integer>();
  11. private static int minimumInactiveTime = 60; //Should be a multiple of RUN_INTERVAL for best performance
  12. public static final int RUN_INTERVAL = 20;
  13. public static void addToList(Chunk chunk) {
  14. //Unfortunately we can't use Map.contains() because Chunks are always new objects
  15. //This method isn't efficient enough for me
  16. for (Chunk otherChunk : unloadedChunks.keySet()) {
  17. if (chunk.getX() == otherChunk.getX() && chunk.getZ() == otherChunk.getZ()) {
  18. return;
  19. }
  20. }
  21. unloadedChunks.put(chunk, 0);
  22. }
  23. public static void addToList(int cx, int cz, World world) {
  24. addToList(world.getChunkAt(cx, cz));
  25. }
  26. @Override
  27. public void run() {
  28. for (Iterator<Entry<Chunk, Integer>> unloadedChunkIterator = unloadedChunks.entrySet().iterator() ; unloadedChunkIterator.hasNext() ; ) {
  29. Entry<Chunk, Integer> entry = unloadedChunkIterator.next();
  30. Chunk chunk = entry.getKey();
  31. if (!chunk.isLoaded()) {
  32. int inactiveTime = entry.getValue() + RUN_INTERVAL;
  33. //Chunklets are unloaded only if their chunk has been unloaded for minimumInactiveTime
  34. if (inactiveTime >= minimumInactiveTime) {
  35. if (mcMMO.placeStore == null)
  36. continue;
  37. mcMMO.placeStore.unloadChunk(chunk.getX(), chunk.getZ(), chunk.getWorld());
  38. unloadedChunkIterator.remove();
  39. continue;
  40. }
  41. unloadedChunks.put(entry.getKey(), inactiveTime);
  42. }
  43. else {
  44. //Just remove the entry if the chunk has been reloaded.
  45. unloadedChunkIterator.remove();
  46. }
  47. }
  48. }
  49. }