How to prohibit so that the player cannot put an item in any chests

69 Views Asked by At

I want to prohibit putting my custom item into any chests, I tried, but somehow the item in my regular inventory is shaking strangely and can still be placed in other inventories, I want to ask how to prohibit doing this as a crutch, as I am doing right now.

Here's my current code:

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        Inventory clickedInventory = event.getClickedInventory();
        if (clickedInventory != null && clickedInventory.getType() == InventoryType.PLAYER) {
            ItemStack itemStack = event.getHotbarButton() == -1 ? event.getCursor() : clickedInventory.getItem(event.getHotbarButton());
            if ((customItemService.isCustomItem(itemStack) || customItemService.isCustomItem(event.getCurrentItem())) && event.getSlotType() == InventoryType.SlotType.ARMOR) {
                event.setCancelled(true);
            }
            if (customItemService.isCustomItem(event.getCurrentItem()) && event.isShiftClick()) {
                event.setCancelled(true);
            }
            return;
        }

        Inventory inventory = event.getView().getTopInventory();
        if (inventory.getType() == InventoryType.WORKBENCH || inventory.getType() == InventoryType.CRAFTING) {
            return;
        }
        boolean found = inventory.getType() == InventoryType.CHEST;
        Location location = inventory.getLocation();
        if (location == null) {
            return;
        }
        boolean cancelled = false;
        Inventory playerInventory = event.getWhoClicked().getInventory();
        if (event.getHotbarButton() != -1 && customItemService.isCustomItem(playerInventory.getItem(event.getHotbarButton()))) {
            if (location.getBlockY() < 5 && found) {
                cancelled = true;
            }
            if (!found) {
                cancelled = true;
            }
        }
        if (customItemService.isCustomItem(event.getCurrentItem()) || customItemService.isCustomItem(event.getCursor())) {
            if (location.getBlockY() < 5) {
                cancelled = true;
            }
            if (!found) {
                cancelled = true;
            }
        }
        if (cancelled) {
            event.setCancelled(true);
            event.getWhoClicked().sendMessage(locale.get("not-allow"));
        }
    }


    @EventHandler
    public void onInventoryDrag(InventoryDragEvent event) {
        if (event.getInventory().getType() == InventoryType.PLAYER || event.getInventory().getType() == InventoryType.CRAFTING) {
            if (customItemService.isCustomItem(event.getCursor()) || customItemService.isCustomItem(event.getOldCursor())) {
                event.setCancelled(true);
            }
            return;
        }
        Inventory inventory = event.getView().getTopInventory();
        if (inventory.getType() == InventoryType.WORKBENCH) {
            return;
        }
        boolean found = inventory.getType() == InventoryType.CHEST;
        Location location = event.getView().getTopInventory().getLocation();
        if (location == null) {
            return;
        }
        boolean cancelled = false;
        if (customItemService.isCustomItem(event.getOldCursor()) || customItemService.isCustomItem(event.getCursor())) {
            if (location.getBlockY() < 5) {
                cancelled = true;
            }
            if (!found) {
                cancelled = true;
            }
        }
        if (cancelled) {
            event.setCancelled(true);
            event.getWhoClicked().sendMessage(locale.get("not-allow"));
        }
    }

I need it so that the player cannot place my custom item not through shift, not through buttons on the keyboard, not through the mouse

I tried to use inventory click events, it seemed to work, but you can still put the item in the trolley with the chest.

0

There are 0 best solutions below