timbru31

Untitled

Jan 9th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.03 KB | None | 0 0
  1. package me.plugin.ptweaks;
  2.  
  3. import org.bukkit.Chunk;
  4. import org.bukkit.Location;
  5. import org.bukkit.World;
  6. import org.bukkit.event.EventHandler;
  7. import org.bukkit.event.Listener;
  8. import org.bukkit.event.player.PlayerMoveEvent;
  9. import org.bukkit.event.world.ChunkLoadEvent;
  10. import org.bukkit.event.world.ChunkUnloadEvent;
  11.  
  12. import java.util.Iterator;
  13. import java.util.LinkedHashMap;
  14. import java.util.Map;
  15. import java.util.Map.Entry;
  16.  
  17. public class ChunkPersistance implements Runnable, Listener {
  18.     private PerformanceTweaks mPlugin;
  19.     private Thread mThread = new Thread(this);
  20.     private int mLifeTime = 100000;
  21.     private int mPruneTime = 3000;
  22.     private int mSpawnChunkRadius = 128;
  23.     private int mViewDistanceChunks = 10;
  24.     // I believe a LinkedHashMap suits best, because it keeps the order like it was added, so better looping
  25.     // Since we want to remove our first entry generally... --> Found faster
  26.     // If you know what I mean ;) --> http://stackoverflow.com/a/2889800
  27.     private LinkedHashMap<String, Long> mChunks = new LinkedHashMap<String, Long>();
  28.  
  29.     public ChunkPersistance(PerformanceTweaks plugin) {
  30.         mPlugin = plugin;
  31.         // Register the events
  32.         plugin.getServer().getPluginManager().registerEvents(this, plugin);
  33.     }
  34.  
  35.     public void onEnable() {
  36.         // Get the config values
  37.         mLifeTime = mPlugin.chunkPersistance.getInt("lifetime", 100000);
  38.         mPruneTime = mPlugin.chunkPersistance.getInt("prune", 3000);
  39.         mSpawnChunkRadius = mPlugin.chunkPersistance.getInt("spawnChunkRadius", 128);
  40.         mViewDistanceChunks = mPlugin.chunkPersistance.getInt("viewDistanceChunks", 10);
  41.  
  42.         // Log the information
  43.         mPlugin.getLogger().info(String.format("Chunk Life Time: %dms", mLifeTime));
  44.         mPlugin.getLogger().info(String.format("Chunk Prune Time: %dms",mPruneTime));
  45.  
  46.         long time = System.currentTimeMillis();
  47.         // Go through all worlds
  48.         for (World world : mPlugin.getServer().getWorlds()) {
  49.             // Go through all chunks
  50.             for (Chunk chunk : world.getLoadedChunks()) {
  51.                 // Don't add if spawn chunk
  52.                 if (!isSpawnChunk(chunk)) {
  53.                     String s = chunk.getWorld().getName() + ";" + chunk.getX() + ";" + chunk.getZ();
  54.                     mChunks.put(s, time);
  55.                 }
  56.             }
  57.         }
  58.  
  59.         mThread.start();
  60.     }
  61.  
  62.     public void onDisable() {
  63.         // Disable the Thread again
  64.         if (mThread.isAlive()) mThread.interrupt();
  65.         // Iterate through HashMap
  66.         for (Map.Entry<String, Long> entry: mChunks.entrySet()) {
  67.             String[] key = entry.getKey().split(";");
  68.             String w = key[0];
  69.             int x = Integer.valueOf(key[1]);
  70.             int z = Integer.valueOf(key[2]);
  71.             World world = mPlugin.getServer().getWorld(w);
  72.             // If the chunk is loaded, unload!
  73.             if (world.isChunkLoaded(x, z))  world.unloadChunk(x, z);
  74.         }
  75.         clear();
  76.  
  77.     }
  78.  
  79.     @EventHandler
  80.     public void onChunkLoad(ChunkLoadEvent event) {
  81.         // If its NO spawn chunk
  82.         if (!isSpawnChunk(event.getChunk())) {
  83.             Chunk c = event.getChunk();
  84.             // Better use only the name of the world
  85.             String s = c.getWorld().getName() + ";" + c.getX() + ";" + c.getZ();
  86.             // Save the chunk with a time
  87.             mChunks.put(s, System.currentTimeMillis());
  88.         }
  89.     }
  90.  
  91.     @EventHandler
  92.     public void onChunkUnload(ChunkUnloadEvent event) {
  93.         // Never unload a spawn chunk!
  94.         if (isSpawnChunk(event.getChunk())) {
  95.             event.setCancelled(true);
  96.             return;
  97.         }
  98.  
  99.         Chunk c = event.getChunk();
  100.         String s = c.getWorld().getName() + ";" + c.getX() + ";" + c.getZ();
  101.  
  102.         // Check if the chunk is on our list
  103.         if (!mChunks.containsKey(s)) {
  104.             return;
  105.         }
  106.  
  107.         long age = System.currentTimeMillis() - mChunks.get(s);
  108.         // If the age is not "old" enough, keep the chunk loaded
  109.         if (age < mLifeTime) {
  110.             event.setCancelled(true);
  111.         }
  112.         // Else remove it from the list
  113.         else {
  114.             mChunks.remove(s);
  115.         }
  116.     }
  117.  
  118.     @EventHandler
  119.     public void onPlayerMove(PlayerMoveEvent event) {
  120.         long time = System.currentTimeMillis();
  121.         // I changed the code so that the chunks around the player are added/updated is customizable
  122.  
  123.         // Use minecraft method ;)
  124.         int x = event.getPlayer().getLocation().getChunk().getX();
  125.         int z = event.getPlayer().getLocation().getChunk().getZ();
  126.  
  127.         // Get all da chunks in a radius
  128.         World world = event.getPlayer().getLocation().getWorld();
  129.         for (int a = 0; a < mViewDistanceChunks; a++) {
  130.             for (int b = 0; b < mViewDistanceChunks; b++) {
  131.                 Chunk chunk = world.getChunkAt(x + a, z + b);
  132.                 if (!isSpawnChunk(chunk)) {
  133.                     String s = chunk.getWorld().getName() + ";" + chunk.getX() + ";" + chunk.getZ();
  134.                     mChunks.put(s, time);
  135.                 }
  136.             }
  137.         }
  138.     }
  139.  
  140.     public void clear() {
  141.         mChunks.clear();
  142.     }
  143.  
  144.     // Check is a chunk is a spawn chunk
  145.     private boolean isSpawnChunk(Chunk chunk) {
  146.         Location spawn = chunk.getWorld().getSpawnLocation();
  147.         // Get absolute value, so we can check just with bigger than 128
  148.         int x = Math.abs(chunk.getX() - spawn.getBlockX());
  149.         int z = Math.abs(chunk.getZ() - spawn.getBlockZ());
  150.         return (x > mSpawnChunkRadius) && (z > mSpawnChunkRadius);
  151.     }
  152.  
  153.     public void run() {
  154.         try {
  155.             while (!mThread.isInterrupted()) {
  156.                 try {
  157.                     Thread.sleep(mPruneTime);
  158.                 }
  159.                 catch (InterruptedException e) {
  160.                     Thread.currentThread().interrupt();
  161.                     return;
  162.                 }
  163.                 // Get current time
  164.                 long timeNow = System.currentTimeMillis();
  165.  
  166.                 // Iterate through HashMap
  167.                 Iterator<Entry<String, Long>> iterator = mChunks.entrySet().iterator();
  168.                 while (iterator.hasNext()) {
  169.                     Map.Entry<String, Long> entry = iterator.next();
  170.                     String[] key = entry.getKey().split(";");
  171.                     long time = entry.getValue();
  172.                     long age = timeNow - time;
  173.                     // If the liftTime of the chunk is reached
  174.                     if (age >= mLifeTime) {
  175.                         String w = key[0];
  176.                         int x = Integer.valueOf(key[1]);
  177.                         int z = Integer.valueOf(key[2]);
  178.                         World world = mPlugin.getServer().getWorld(w);
  179.                         // If the chunk is loaded, unload!
  180.                         if (world.isChunkLoaded(x, z)) {
  181.                             world.unloadChunk(x, z);
  182.                             iterator.remove();
  183.                         }
  184.                     }
  185.                 }
  186.             }
  187.         }
  188.         // Good to listen, since anything could happen, and I'm not sure what exactly (:
  189.         catch (Exception e) {
  190.             e.printStackTrace();
  191.         }
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment