Advertisement
DuneSciFye

Untitled

Dec 21st, 2023
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. package com.bgsoftware.wildchests.listeners;
  2.  
  3. import com.bgsoftware.wildchests.WildChestsPlugin;
  4. import com.bgsoftware.wildchests.objects.chests.WChest;
  5. import org.bukkit.Chunk;
  6. import org.bukkit.Location;
  7. import org.bukkit.Material;
  8. import org.bukkit.scheduler.BukkitRunnable;
  9. import org.bukkit.event.EventHandler;
  10. import org.bukkit.event.EventPriority;
  11. import org.bukkit.event.Listener;
  12. import org.bukkit.event.world.ChunkLoadEvent;
  13. import org.bukkit.event.world.ChunkUnloadEvent;
  14.  
  15. public final class ChunksListener implements Listener {
  16.  
  17. private final WildChestsPlugin plugin;
  18.  
  19. public ChunksListener(WildChestsPlugin plugin){
  20. this.plugin = plugin;
  21. }
  22.  
  23. @EventHandler(priority = EventPriority.LOWEST)
  24. public void onChunkLoad(ChunkLoadEvent e){
  25. handleChunkLoad(plugin, e.getChunk());
  26. }
  27.  
  28. @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
  29. public void onChunkUnload(ChunkUnloadEvent e){
  30. plugin.getDataHandler().saveDatabase(e.getChunk(), true);
  31. }
  32.  
  33. public static void handleChunkLoad(WildChestsPlugin plugin, Chunk chunk){
  34. new BukkitRunnable() {
  35. @Override
  36. public void run() {
  37. plugin.getChestsManager().loadChestsForChunk(chunk);
  38.  
  39. plugin.getChestsManager().getChests(chunk).forEach(chest -> {
  40. Location location = chest.getLocation();
  41. Material blockType = location.getBlock().getType();
  42. if(blockType != Material.CHEST){
  43. WildChestsPlugin.log("Loading chunk " + chunk.getX() + ", " + chunk.getX() + " but found a chest not " +
  44. "associated with a chest block but " + blockType + " at " + location.getWorld().getName() + ", " +
  45. location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ());
  46. chest.remove();
  47. }
  48. else{
  49. ((WChest) chest).onChunkLoad();
  50. }
  51. });
  52. }
  53. }.runTaskLater(plugin, 5l);
  54. }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement