Solved InventoryClickEvent on chest doublechest but not Enderchest

Discussion in 'Spigot Plugin Development' started by Poinball, Jun 13, 2017.

  1. Hey guys,

    In this code , I Cancel the movement of a specific item in a player inventory when he have a chest or double chest open. I tried the same with Enderchest but it doesnt work ...

    Code (Text):
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onInventoryClick(InventoryClickEvent event) {

            Player p = (Player) event.getWhoClicked();
            Inventory clicked = event.getClickedInventory();
            if ( event.getInventory().getHolder() instanceof Chest || event.getInventory().getHolder() instanceof DoubleChest || event.getInventory().getHolder() instanceof EnderChest) {
                if (clicked == event.getWhoClicked().getInventory()) {
                    ItemStack clickedOn = event.getCurrentItem();
                    if (clickedOn != null && event.getCurrentItem().hasItemMeta() && event.getCurrentItem().getItemMeta().getDisplayName().equals( "Red King's Crown" )) {
                        event.setCancelled( true );
                    }
                }
            }
        }
    Someone have an idea ?
     
  2. You would be better checking if Event#getClickedInventory#getType#equals(InventoryType.ENDER_CHEST)
     
    • Agree Agree x 1
  3. Both doesnt work :/

    edit : I fount it

    It was :
    event.getInventory().equals( p.getEnderChest())
     
  4. Code (Text):
    @EventHandler
        public void onClick (InventoryClickEvent e) {
            if (e.getInventory().getType() == InventoryType.ENDER_CHEST) {
                //if you want to stop all ender chest movement of items, do it here with an 'e.setCancelled(true);'
                if (e.getCurrentItem().getType() == Material.GOLD_INGOT) {
                    e.setCancelled(true);
                }
            }
        }
    That code disables gold ingots from going in/out of ender chests. The code can be easily adapted to other items.
    1- Check if the inventorytype is an enderchest.
    2- Check the item

    EDIT: Sorry, didn't see that you've found a solution already!