Advertisement
JackOUT

Untitled

May 19th, 2022
591
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package games.coob.smp.listener;
  2.  
  3. import games.coob.smp.PlayerCache;
  4. import games.coob.smp.hologram.NMSHologramProvider;
  5. import games.coob.smp.model.DeathChestRegistry;
  6. import games.coob.smp.nms.NMSHologramI;
  7. import games.coob.smp.settings.Settings;
  8. import lombok.AccessLevel;
  9. import lombok.Getter;
  10. import lombok.NoArgsConstructor;
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.Location;
  13. import org.bukkit.Material;
  14. import org.bukkit.World;
  15. import org.bukkit.block.Block;
  16. import org.bukkit.entity.HumanEntity;
  17. import org.bukkit.entity.Player;
  18. import org.bukkit.event.EventHandler;
  19. import org.bukkit.event.Listener;
  20. import org.bukkit.event.block.Action;
  21. import org.bukkit.event.block.BlockBreakEvent;
  22. import org.bukkit.event.entity.PlayerDeathEvent;
  23. import org.bukkit.event.inventory.InventoryCloseEvent;
  24. import org.bukkit.event.player.PlayerInteractEvent;
  25. import org.bukkit.inventory.Inventory;
  26. import org.bukkit.inventory.ItemStack;
  27. import org.mineacademy.fo.remain.Remain;
  28.  
  29. import java.util.Arrays;
  30. import java.util.Objects;
  31. import java.util.stream.Stream;
  32.  
  33. @NoArgsConstructor(access = AccessLevel.PRIVATE)
  34. public final class DeathChestListener implements Listener {
  35.  
  36.     @Getter
  37.     private static final DeathChestListener instance = new DeathChestListener();
  38.  
  39.     @EventHandler
  40.     public void onPlayerInteract(final PlayerInteractEvent event) {
  41.         final Player player = event.getPlayer();
  42.  
  43.         if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
  44.             final Block block = event.getClickedBlock();
  45.             final DeathChestRegistry registry = DeathChestRegistry.getInstance();
  46.  
  47.             if (block != null && block.getType() == Settings.DeathStorageSection.STORAGE_MATERIAL.getMaterial() && registry.isRegistered(block)) {
  48.                 event.setCancelled(true);
  49.                 player.openInventory(registry.getInventory(block));
  50.  
  51.                 if (block.getType().toString().contains("CHEST"))
  52.                     Remain.sendChestOpen(block);
  53.             }
  54.         }
  55.     }
  56.  
  57.     @EventHandler
  58.     public void onPlayerDeath(final PlayerDeathEvent event) {
  59.         if (Settings.DeathStorageSection.ENABLE_DEATH_STORAGE) {
  60.             final Player player = event.getEntity();
  61.             final Location location = player.getLocation();
  62.             final Block block = location.getBlock();
  63.             final PlayerCache cache = PlayerCache.from(player);
  64.             final DeathChestRegistry registry = DeathChestRegistry.getInstance();
  65.             final NMSHologramI hologram = NMSHologramProvider.createHologram();
  66.             final ItemStack[] drops = Stream.of(player.getInventory().getContents()) // Create a stream of ItemStack
  67.                     .filter(Objects::nonNull) // Filter all non null values (removing empty slot)
  68.                     .toArray(ItemStack[]::new); // Convert the result to ItemStack array
  69.  
  70.             if (player.getInventory().isEmpty())
  71.                 return;
  72.  
  73.             if (player.getWorld().getEnvironment() == World.Environment.NORMAL)
  74.                 cache.setDeathLocation(location);
  75.             else {
  76.                 location.setY(1);
  77.                 cache.setDeathLocation(location);
  78.             }
  79.  
  80.             for (final ItemStack itemStack : Arrays.stream(drops).toList())
  81.                 event.getDrops().remove(itemStack);
  82.  
  83.             Inventory inventory = null;
  84.  
  85.             if (drops.length <= 9)
  86.                 inventory = Bukkit.createInventory(player, 9);
  87.             else if (drops.length <= 18)
  88.                 inventory = Bukkit.createInventory(player, 18);
  89.             else if (drops.length <= 27)
  90.                 inventory = Bukkit.createInventory(player, 27);
  91.             else if (drops.length <= 36)
  92.                 inventory = Bukkit.createInventory(player, 36);
  93.             else if (drops.length <= 45)
  94.                 inventory = Bukkit.createInventory(player, 45);
  95.  
  96.             assert inventory != null;
  97.             block.setType(Settings.DeathStorageSection.STORAGE_MATERIAL.getMaterial());
  98.  
  99.             for (final Player closePlayers : Remain.getOnlinePlayers()) // TODO
  100.                 if (closePlayers.getLocation().distance(block.getLocation().clone().add(0.5, -0.75, 0.5)) < Settings.DeathStorageSection.HOLOGRAM_VISIBLE_RANGE)
  101.                     hologram.show(block.getLocation().clone().add(0.5, -0.75, 0.5), player, chestOwnerMessage(Settings.DeathStorageSection.HOLOGRAM_TEXT, player));
  102.  
  103.             inventory.setContents(drops);
  104.             cache.setDeathChestInventory(inventory);
  105.  
  106.             if (!registry.isRegistered(block))
  107.                 registry.register(block, player, hologram);
  108.         }
  109.     }
  110.  
  111.     private String chestOwnerMessage(final String message, final Player player) {
  112.         if (message.contains("{player}"))
  113.             return message.replace("{player}", player.getName());
  114.  
  115.         return message;
  116.     }
  117.  
  118.     @EventHandler
  119.     public void onInventoryClose(final InventoryCloseEvent event) {
  120.         final HumanEntity human = event.getPlayer();
  121.  
  122.         if (human instanceof Player) {
  123.             final DeathChestRegistry registry = DeathChestRegistry.getInstance();
  124.  
  125.             for (final Location location : registry.getLocations()) {
  126.                 final Block block = registry.getBlock(location);
  127.  
  128.                 if (registry.getInventory(block).isEmpty()) {
  129.                     block.setType(Material.AIR);
  130.  
  131.                     for (final Player player : Remain.getOnlinePlayers()) // TODO
  132.                         registry.getHologram(block).remove(player);
  133.  
  134.                     registry.unregister(block);
  135.                 }
  136.  
  137.                 if (block.getType().toString().contains("CHEST"))
  138.                     Remain.sendChestClose(block);
  139.             }
  140.         }
  141.     }
  142.  
  143.     @EventHandler
  144.     public void onBlockBreak(final BlockBreakEvent event) {
  145.         if (Settings.DeathStorageSection.ENABLE_DEATH_STORAGE) {
  146.             final DeathChestRegistry registry = DeathChestRegistry.getInstance();
  147.             final Block block = event.getBlock();
  148.  
  149.             if (registry.isRegistered(block)) {
  150.                 final ItemStack[] items = Stream.of(registry.getInventory(block).getContents()) // Create a stream of ItemStack
  151.                         .filter(Objects::nonNull) // Filter all non null values (removing empty slot)
  152.                         .toArray(ItemStack[]::new); // Convert the result to ItemStack array
  153.  
  154.                 for (final ItemStack item : items) {
  155.                     block.getWorld().dropItem(block.getLocation(), item);
  156.  
  157.                     if (item.getType() == Settings.DeathStorageSection.STORAGE_MATERIAL.getMaterial())
  158.                         item.setType(Material.AIR);
  159.                 }
  160.  
  161.                 for (final Player player : Remain.getOnlinePlayers()) // TODO
  162.                     registry.getHologram(block).remove(player);
  163.  
  164.                 registry.unregister(block);
  165.             }
  166.         }
  167.     }
  168. }
  169.  
Advertisement
RAW Paste Data Copied
Advertisement