broken-arrow

Untitled

Jul 25th, 2021 (edited)
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. public final class InventoryListener implements Listener {
  2.  
  3.     private final WildChestsPlugin plugin;
  4.  
  5.     public static final Map<UUID, InventoryData> buyNewPage = Maps.newHashMap();
  6.  
  7.     public InventoryListener(WildChestsPlugin plugin){
  8.         this.plugin = plugin;
  9.         initGUIConfirm();
  10.     }
  11.  
  12.     /**
  13.      * The following two events are here for patching a dupe glitch caused
  14.      * by shift clicking and closing the inventory in the same time.
  15.      */
  16.  
  17.     private final Map<UUID, ItemStack> latestClickedItem = new HashMap<>();
  18.     private final String[] inventoryTitles = new String[] {"Expand Confirmation", };
  19.  
  20.     @EventHandler(priority = EventPriority.MONITOR)
  21.     public void onInventoryClickMonitor(InventoryClickEvent e){
  22.         if(e.getCurrentItem() != null && e.isCancelled() && Arrays.stream(inventoryTitles).anyMatch(title -> e.getView().getTitle().contains(title))) {
  23.             latestClickedItem.put(e.getWhoClicked().getUniqueId(), e.getCurrentItem());
  24.             Executor.sync(() -> latestClickedItem.remove(e.getWhoClicked().getUniqueId()), 20L);
  25.         }
  26.     }
  27.  
  28.     @EventHandler(priority = EventPriority.MONITOR)
  29.     public void onInventoryCloseMonitor(InventoryCloseEvent e){
  30.         if(latestClickedItem.containsKey(e.getPlayer().getUniqueId())){
  31.             ItemStack clickedItem = latestClickedItem.get(e.getPlayer().getUniqueId());
  32.             Executor.sync(() -> {
  33.                 e.getPlayer().getInventory().removeItem(clickedItem);
  34.                 ((Player) e.getPlayer()).updateInventory();
  35.             }, 1L);
  36.         }
  37.     }
  38.  
  39.     @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
  40.     public void onChestOpen(PlayerInteractEvent e){
  41.         if(e instanceof LinkedChestInteractEvent || e.getAction() != Action.RIGHT_CLICK_BLOCK ||
  42.                 e.getClickedBlock().getType() != Material.HOPPER)
  43.             return;
  44.  
  45.         if(buyNewPage.containsKey(e.getPlayer().getUniqueId())){
  46.             e.setCancelled(true);
  47.             return;
  48.         }
  49.  
  50.         boolean debug = e.getPlayer().isOp();
  51.  
  52.         Chest chest = plugin.getChestsManager().getChest(e.getClickedBlock().getLocation());
  53.  
  54.         if(debug){
  55.             WildChestsPlugin.debug("Opening a chest at " + LocationUtils.toString(e.getClickedBlock().getLocation()));
  56.             WildChestsPlugin.debug("Cached Chest: " + chest);
  57.         }
  58.  
  59.         if(chest == null)
  60.             return;
  61.  
  62.         plugin.getNMSInventory().updateTileEntity(chest);
  63.  
  64.         chest.onOpen(e);
  65.     }
  66.  
Add Comment
Please, Sign In to add comment