ChunkletUnloader.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 com.gmail.nossr50.mcMMO;
  8. public class ChunkletUnloader implements Runnable {
  9. private static Map<Chunk, Integer> unloadedChunks = new HashMap<Chunk, Integer>();
  10. private static int minimumInactiveTime = 60; //Should be a multiple of RUN_INTERVAL for best performance
  11. public static final int RUN_INTERVAL = 20;
  12. public static void addToList(Chunk chunk) {
  13. //Unfortunately we can't use Map.contains() because Chunks are always new objects
  14. //This method isn't efficient enough for me
  15. for (Chunk otherChunk : unloadedChunks.keySet()) {
  16. if (chunk.getX() == otherChunk.getX() && chunk.getZ() == otherChunk.getZ()) {
  17. return;
  18. }
  19. }
  20. unloadedChunks.put(chunk, 0);
  21. }
  22. @Override
  23. public void run() {
  24. for (Iterator<Entry<Chunk, Integer>> it = unloadedChunks.entrySet().iterator() ; it.hasNext() ; ) {
  25. Entry<Chunk, Integer> entry = it.next();
  26. Chunk chunk = entry.getKey();
  27. if (!chunk.isLoaded()) {
  28. int inactiveTime = entry.getValue() + RUN_INTERVAL;
  29. //Chunklets are unloaded only if their chunk has been unloaded for minimumInactiveTime
  30. if (inactiveTime >= minimumInactiveTime) {
  31. mcMMO.placeStore.chunkUnloaded(chunk.getX(), chunk.getZ(), chunk.getWorld());
  32. it.remove();
  33. continue;
  34. }
  35. unloadedChunks.put(entry.getKey(), inactiveTime);
  36. }
  37. else {
  38. //Just remove the entry if the chunk has been reloaded.
  39. it.remove();
  40. }
  41. }
  42. }
  43. }