Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public final class InventoryListener implements Listener {
- private final WildChestsPlugin plugin;
- public static final Map<UUID, InventoryData> buyNewPage = Maps.newHashMap();
- public InventoryListener(WildChestsPlugin plugin){
- this.plugin = plugin;
- initGUIConfirm();
- }
- /**
- * The following two events are here for patching a dupe glitch caused
- * by shift clicking and closing the inventory in the same time.
- */
- private final Map<UUID, ItemStack> latestClickedItem = new HashMap<>();
- private final String[] inventoryTitles = new String[] {"Expand Confirmation", };
- @EventHandler(priority = EventPriority.MONITOR)
- public void onInventoryClickMonitor(InventoryClickEvent e){
- if(e.getCurrentItem() != null && e.isCancelled() && Arrays.stream(inventoryTitles).anyMatch(title -> e.getView().getTitle().contains(title))) {
- latestClickedItem.put(e.getWhoClicked().getUniqueId(), e.getCurrentItem());
- Executor.sync(() -> latestClickedItem.remove(e.getWhoClicked().getUniqueId()), 20L);
- }
- }
- @EventHandler(priority = EventPriority.MONITOR)
- public void onInventoryCloseMonitor(InventoryCloseEvent e){
- if(latestClickedItem.containsKey(e.getPlayer().getUniqueId())){
- ItemStack clickedItem = latestClickedItem.get(e.getPlayer().getUniqueId());
- Executor.sync(() -> {
- e.getPlayer().getInventory().removeItem(clickedItem);
- ((Player) e.getPlayer()).updateInventory();
- }, 1L);
- }
- }
- @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
- public void onChestOpen(PlayerInteractEvent e){
- if(e instanceof LinkedChestInteractEvent || e.getAction() != Action.RIGHT_CLICK_BLOCK ||
- e.getClickedBlock().getType() != Material.HOPPER)
- return;
- if(buyNewPage.containsKey(e.getPlayer().getUniqueId())){
- e.setCancelled(true);
- return;
- }
- boolean debug = e.getPlayer().isOp();
- Chest chest = plugin.getChestsManager().getChest(e.getClickedBlock().getLocation());
- if(debug){
- WildChestsPlugin.debug("Opening a chest at " + LocationUtils.toString(e.getClickedBlock().getLocation()));
- WildChestsPlugin.debug("Cached Chest: " + chest);
- }
- if(chest == null)
- return;
- plugin.getNMSInventory().updateTileEntity(chest);
- chest.onOpen(e);
- }
Add Comment
Please, Sign In to add comment