Solved Inventory Click Event Trouble

Discussion in 'Plugin Development' started by BaddCamden, Jul 1, 2021.

Thread Status:
Not open for further replies.
  1. Offline

    BaddCamden

    I have been trying to make the event not allow you to click or place down anything but the specific items I would allow, but it returns a null error whenever I try to get the item meta of the cursor item or the current item. (I think cursor is what the player is holding and the current item is the item they clicked, if I am not wrong)

    Code:
    Code:
        @EventHandler
        public void onClick(InventoryClickEvent event) {
            for(String key : config.getConfigurationSection("CharmBundles").getKeys(false)) {
            
                if(event.getInventory().getItem(8) != null) {
                
                    if(event.getInventory().getItem(8).getItemMeta().getLocalizedName().equalsIgnoreCase(key)) {
                        boolean foundCharm = false;
                        for(String charm : config.getConfigurationSection("Charms").getKeys(false)) {
                        
                            if(event.getCurrentItem() != null)
                                    if(event.getCurrentItem().getItemMeta().getLocalizedName().equals(charm)) foundCharm = true;
                        
                            if(event.getCurrentItem() == null && event.getCursor() != null)
                                if(event.getCursor().getItemMeta().getLocalizedName().equals(charm)) foundCharm = true;
                        
                        
                            if(!foundCharm) {
                                event.setCancelled(true);
                            }
                        }
              
                    }
                }
            }
        }
    


    For a more specific area to where the issue it
    Code:
                            if(event.getCurrentItem() != null)
                                    if(event.getCurrentItem().getItemMeta().getLocalizedName().equals(charm)) foundCharm = true;
                         
                            if(event.getCurrentItem() == null && event.getCursor() != null)
                                if(event.getCursor().getItemMeta().getLocalizedName().equals(charm)) foundCharm = true;
    

    And the error

    Caused by: java.lang.NullPointerException: Cannot invoke "org.bukkit.inventory.meta.ItemMeta.getLocalizedName()" because the return value of "org.bukkit.inventory.ItemStack.getItemMeta()" is null
     
  2. Offline

    KarimAKL

    @BaddCamden The error states the problem. #getItemMeta() returns null.
     
    davidclue likes this.
  3. Offline

    BaddCamden

    Right, but why does it do that was my question, since I had already added things to prevent it from returning null. I found out just now that it was because it was Material.AIR, not just null.


    Anybody having the same problem, this will fix you issue:
    Code:
    event.getCursor() != null && event.getCursor().getType() != Material.AIR
    

    instead of
    Code:
    event.getCursor() != null
    

    I understand why they do it this way specifically for referring to ItemStacks, but it is still annoying.
    What they do is:
    Material.AIR == null as a Material, but not as an ItemStack, but an ItemStack can still == null.
     
Thread Status:
Not open for further replies.

Share This Page