daily pastebin goal
77%
SHARE
TWEET

Untitled

a guest May 22nd, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package de.blablubbabc.test2;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5.  
  6. import org.apache.commons.lang.Validate;
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.Chunk;
  9. import org.bukkit.Location;
  10. import org.bukkit.World;
  11. import org.bukkit.block.Block;
  12. import org.bukkit.event.EventHandler;
  13. import org.bukkit.event.EventPriority;
  14. import org.bukkit.event.Listener;
  15. import org.bukkit.event.world.ChunkLoadEvent;
  16. import org.bukkit.event.world.ChunkUnloadEvent;
  17. import org.bukkit.plugin.java.JavaPlugin;
  18.  
  19. public class Test extends JavaPlugin implements Listener {
  20.  
  21.     private final Map<ChunkCoords, Long> recentlyUnloaded = new LinkedHashMap<>();
  22.  
  23.     @Override
  24.     public void onEnable() {
  25.         Bukkit.getPluginManager().registerEvents(this, this);
  26.     }
  27.  
  28.     @Override
  29.     public void onDisable() {
  30.     }
  31.  
  32.     @EventHandler(priority = EventPriority.MONITOR)
  33.     void onChunkLoad(ChunkLoadEvent event) {
  34.         Chunk chunk = event.getChunk();
  35.         Long unloadedTime = recentlyUnloaded.get(new ChunkCoords(chunk));
  36.         if (unloadedTime != null) {
  37.             long reloadDelay = System.currentTimeMillis() - unloadedTime;
  38.             getLogger().warning("  Detected recently (" + reloadDelay + "ms) unloaded chunk getting reloaded: " + chunk.getX() + "," + chunk.getZ());
  39.             Thread.dumpStack();
  40.         }
  41.     }
  42.  
  43.     @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
  44.     void onChunkUnload(ChunkUnloadEvent event) {
  45.         Chunk chunk = event.getChunk();
  46.         ChunkCoords coord = new ChunkCoords(chunk);
  47.         Long time = System.currentTimeMillis();
  48.         recentlyUnloaded.put(coord, time);
  49.         Bukkit.getScheduler().runTaskLater(this, () -> {
  50.             recentlyUnloaded.remove(coord, time);
  51.         }, 100L); // 5 seconds
  52.     }
  53.  
  54.     /**
  55.      * Stores positional information about a chunk, like its world and coordinates.
  56.      */
  57.     public static final class ChunkCoords {
  58.  
  59.         private final String worldName;
  60.         private final int chunkX;
  61.         private final int chunkZ;
  62.  
  63.         public ChunkCoords(String worldName, int chunkX, int chunkZ) {
  64.             Validate.notNull(worldName, "World name is null!");
  65.             this.worldName = worldName;
  66.             this.chunkX = chunkX;
  67.             this.chunkZ = chunkZ;
  68.         }
  69.  
  70.         public ChunkCoords(Chunk chunk) {
  71.             Validate.notNull(chunk, "Chunk is null!");
  72.             this.worldName = chunk.getWorld().getName();
  73.             this.chunkX = chunk.getX();
  74.             this.chunkZ = chunk.getZ();
  75.         }
  76.  
  77.         public ChunkCoords(Location location) {
  78.             Validate.notNull(location, "Location is null!");
  79.             World world = location.getWorld();
  80.             Validate.notNull(world);
  81.             this.worldName = world.getName();
  82.             this.chunkX = convertBlockCoord(location.getBlockX());
  83.             this.chunkZ = convertBlockCoord(location.getBlockZ());
  84.         }
  85.  
  86.         public ChunkCoords(Block block) {
  87.             Validate.notNull(block, "Block is null!");
  88.             World world = block.getWorld();
  89.             Validate.notNull(world);
  90.             this.worldName = world.getName();
  91.             this.chunkX = convertBlockCoord(block.getX());
  92.             this.chunkZ = convertBlockCoord(block.getZ());
  93.         }
  94.  
  95.         /**
  96.          * Gets the chunk's world name.
  97.          *
  98.          * @return the chunk's world name
  99.          */
  100.         public String getWorldName() {
  101.             return worldName;
  102.         }
  103.  
  104.         /**
  105.          * Gets the chunk's x-coordinate
  106.          *
  107.          * @return the chunk's x-coordinate
  108.          */
  109.         public int getChunkX() {
  110.             return chunkX;
  111.         }
  112.  
  113.         /**
  114.          * Gets the chunk's z-coordinate
  115.          *
  116.          * @return the chunk's z-coordinate
  117.          */
  118.         public int getChunkZ() {
  119.             return chunkZ;
  120.         }
  121.  
  122.         public boolean isChunkLoaded() {
  123.             World world = Bukkit.getServer().getWorld(worldName);
  124.             if (world != null) {
  125.                 return world.isChunkLoaded(chunkX, chunkZ);
  126.             }
  127.             return false;
  128.         }
  129.  
  130.         public boolean isSameChunk(Chunk chunk) {
  131.             if (chunk == null) return false;
  132.             return chunk.getX() == chunkX && chunk.getZ() == chunkZ && chunk.getWorld().getName().equals(worldName);
  133.         }
  134.  
  135.         @Override
  136.         public String toString() {
  137.             return getClass().getName()
  138.                     + "[worldName=" + worldName
  139.                     + ",chunkX=" + chunkX
  140.                     + ",chunkZ=" + chunkZ + "]";
  141.         }
  142.  
  143.         @Override
  144.         public int hashCode() {
  145.             final int prime = 31;
  146.             int result = 1;
  147.             result = prime * result + chunkX;
  148.             result = prime * result + chunkZ;
  149.             result = prime * result + worldName.hashCode();
  150.             return result;
  151.         }
  152.  
  153.         @Override
  154.         public boolean equals(Object obj) {
  155.             if (this == obj) return true;
  156.             if (obj == null) return false;
  157.             if (this.getClass() != obj.getClass()) return false;
  158.             ChunkCoords other = (ChunkCoords) obj;
  159.             if (chunkX != other.chunkX) return false;
  160.             if (chunkZ != other.chunkZ) return false;
  161.             if (!worldName.equals(other.worldName)) return false;
  162.             return true;
  163.         }
  164.  
  165.         public static int convertBlockCoord(int blockCoord) {
  166.             return blockCoord >> 4;
  167.         }
  168.  
  169.         public static ChunkCoords fromBlockPos(String worldName, int blockX, int blockZ) {
  170.             return new ChunkCoords(worldName, convertBlockCoord(blockX), convertBlockCoord(blockZ));
  171.         }
  172.     }
  173. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top