1.16.5 Prevent moving specific items with numbers in inventory

Discussion in 'Spigot Plugin Development' started by Black1200, Jun 26, 2021.

  1. Hi!

    I would need some help with moving specific items in inventories using the number keys.
    I don't wanna disable the whole NUMBER_KEY click type, just if one specific item gets moved by numbers.

    This is what I got so far:
    Code (Java):
    public ItemStack getMiniShopItem() {
            ItemStack itemStack = new ItemStack(Material.CHEST, 1);
            ItemMeta meta = itemStack.getItemMeta();
            meta.setDisplayName("┬žeMiniShop");
            itemStack.setItemMeta(meta);
            return itemStack;
        }
    Code (Java):
    @EventHandler
        public void onClick(InventoryClickEvent event) {
            if(event.getCurrentItem() != null && event.getCurrentItem().isSimilar(shopManager.getMiniShopItem()) || event.getClick().equals(ClickType.NUMBER_KEY)) event.setCancelled(true);
        }
    The getMiniShopItem() function doesn't matter here, it's just that I want that item to not be moveable by numbers keys.

    Thanks in advance for any help!
     
  2. Well If you are trying to cancel it with the ClickType#NUMBER_KEY, by the looks of your current statement it should be
    Code (Text):

    if(event.getCurrentItem() != null && event.getCurrentItem().isSimilar(shopManager.getMiniShopItem()) && event.getClick().equals(ClickType.NUMBER_KEY)) event.setCancelled(true);
     
     
  3. For some reason it doesn't matter if its an AND or an OR because it doesn't block the NUMBER_KEY action at all anyways.
     
  4. Are you sure you are registering the listener?
    Try debugging first.
     
  5. I'm sure it's registered and working because it blocks all the other ClickTypes, just not the NUMBER_KEY one.