Solved Stop player from moving item to certain inventories

Discussion in 'Spigot Plugin Development' started by Vhbob, Jan 1, 2020.

  1. Hello, I'm trying to only let players move items in and out of certain inventories. Here is what I have tried so far, but the getClickedInventory() method does not return the correct inventory for this instance. For example, I can drop an item in a chest inventory even though it should be cancelled. Here is my code and a video of the error:
    Code (Text):
    Player player = (Player) e.getWhoClicked();
                ArrayList<InventoryType> validTypes = new ArrayList<InventoryType>();
                validTypes.add(InventoryType.ANVIL);
                validTypes.add(InventoryType.ENCHANTING);
                validTypes.add(InventoryType.ENDER_CHEST);
                validTypes.add(InventoryType.WORKBENCH);
                validTypes.add(InventoryType.PLAYER);
                if (e.getClickedInventory() == null || !validTypes.contains(e.getClickedInventory().getType())) {
                    player.sendMessage(
                            ChatColor.RED + "You cannot drop soulbound items! If you are missing the item, please relog");
                    e.setCancelled(true);
                }
    https://i.gyazo.com/f6d05ec78b6a693b94a5e5db7c8cba3f.mp4
     
    #1 Vhbob, Jan 1, 2020
    Last edited: Jan 1, 2020
  2. Maybe look into
    Code (Java):
    InventoryAction.MOVE_TO_OTHER_INVENTORY
     
  3. That only first for shift clicks. I need something that checks for players dropping items into different inventories too
     
  4. If you want to have help, you should consider providing more information, at least the event this is in and how player is defined.
    That being said, have you considered checking the inventory type and not canceling the event only, if the inventory is of type
    Code (Java):
    InventoryType.PLAYER
     
  5. Here is my new code.
    Code (Text):
    @EventHandler
        public void onClick(InventoryClickEvent e) {
            System.out.println(e.getInventory().getType() + " " + e.getClickedInventory().getType());
            if (e.getWhoClicked() instanceof Player && !e.getWhoClicked().hasPermission("soulbound.ignore")
                    && isSoulbound(e.getCurrentItem())) {
                Player player = (Player) e.getWhoClicked();
                ArrayList<InventoryType> validTypes = new ArrayList<InventoryType>();
                validTypes.add(InventoryType.ANVIL);
                validTypes.add(InventoryType.ENCHANTING);
                validTypes.add(InventoryType.ENDER_CHEST);
                validTypes.add(InventoryType.WORKBENCH);
                if (e.getClickedInventory() == null || (!validTypes.contains(e.getInventory().getType())
                        && e.getClickedInventory().getType() != InventoryType.PLAYER)) {
                    player.sendMessage(
                            ChatColor.RED + "You cannot drop soulbound items! If you are missing the item, please relog");
                    e.setCancelled(true);
                }
            }
        }
    However, when I place my item in the 2x2 crafting inventory in my player inventory, although the console prints "CRAFTING CRAFTING" the event is not cancelled. It is only when i try and remove it from the 2x2 crafting table that it cancels the event
     
  6. Simplified the code and changing title / first post
    Code (Text):
    Player player = (Player) e.getWhoClicked();
                ArrayList<InventoryType> validTypes = new ArrayList<InventoryType>();
                validTypes.add(InventoryType.ANVIL);
                validTypes.add(InventoryType.ENCHANTING);
                validTypes.add(InventoryType.ENDER_CHEST);
                validTypes.add(InventoryType.WORKBENCH);
                validTypes.add(InventoryType.PLAYER);
                if (e.getClickedInventory() == null || !validTypes.contains(e.getClickedInventory().getType())) {
                    player.sendMessage(
                            ChatColor.RED + "You cannot drop soulbound items! If you are missing the item, please relog");
                    e.setCancelled(true);
                }
     
  7. Needed to change my check to this: to include dropping items instead of picking up items:
    Code (Text):
    (isSoulbound(e.getCursor()) || isSoulbound(e.getCurrentItem()))