Solved Blocking Players from putting items in a Chest

Discussion in 'Plugin Development' started by Catoklysm, Apr 17, 2025.

  1. Offline

    Catoklysm

    I have created a chest with a custom title and I only want the owner to be able to change the items inside but other players are still able to view the chest.

    I am blocking InventoryClickEvent but this doesn't seem to prevent players from placing items in empty slots and I tried everything I could find and think of to prevent this from happening.

    Did anyone have a similiar problem or a possible solution? Thanks
     
  2. Offline

    Lolmewn

    I guess the event you're looking for is the InventoryInteractEvent
     
  3. Offline

    Catoklysm

    I've tried that, I tried any inventory event I could find
     
  4. Offline

    timtower Administrator Administrator Moderator

    Then post the code you tried
     
  5. Offline

    Catoklysm

    I have added debug messages to the Events like InventoryInteractEvent and InventoryDragEvent and InventoryClickEvent but they don't seem to fire at all when I place something in an empty slot and no destinct click seems to be registered by my InventoryClickEvent when I double click to COLLECT_TO_CURSOR .

    The rest of the code works as intended.

    This is what the code for the different Events looks like and they are also registered in the main class.

    Code:
    public class InventoryInteract implements Listener{
    
        @EventHandler
        public void onInventoryInteract(InventoryInteractEvent e) {
            Player player = (Player) e.getWhoClicked();
            player.sendMessage("InventoryInteractEvent called");
            System.out.println("InventoryInteractEvent called");
          
    //...
    
        }
    
    }
    This is what I have been trying in my InventoryClickEvent class

    Code:
                    if (e.isShiftClick() || e.getClick() == ClickType.DOUBLE_CLICK || e.getClick() == ClickType.NUMBER_KEY) {
                        e.setCancelled(true);
                        player.sendMessage("InventoryClickEvent canceled (bad click type)");
                        return;
                    }
                  
                if (e.getRawSlot() < e.getView().getTopInventory().getSize()) {
                    // Allow only if player is offering the exact correct item & amount
                    if (e.getCursor() != null
                            && e.getCursor().getType() == costItem
                            && e.getCursor().getAmount() == costAmount
                            && e.getCurrentItem() != null
                            && e.getCurrentItem().getType() != Material.AIR
                            && e.getCurrentItem().getType() != costItem) {
    
                        e.setCancelled(false);
                        player.sendMessage("InventoryClickEvent allowed (valid trade)");
                    } else {
                        e.setCancelled(true);
                        player.sendMessage("InventoryClickEvent canceled (invalid trade)");
                    }
                    return;
                }
    
                if (!(e.isShiftClick()
                        || e.getClick() == ClickType.DOUBLE_CLICK
                        || e.getClick() == ClickType.NUMBER_KEY
                        || e.getAction() == InventoryAction.COLLECT_TO_CURSOR
                        || (e.getAction() == InventoryAction.PLACE_ONE))) {
                e.setCancelled(false);
                player.sendMessage("InventoryClickEvent allowed (player inventory)");
                }
    I have encountered a line at the start of my code which I added to prevent null pointer expections which don't run the code if the slot is null or air which would also not block the event for empty slots

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 22, 2025

Share This Page