Solved Debbuging InventoryClickEvent

Discussion in 'Spigot Plugin Development' started by stelioshah, Jun 15, 2021.

  1. So, I am using InventoryClickEvent to track when a player clicks on the resulting item in an unvil in order to remove a lore that I previously added on the item. My code works fine overall, the error occurs when the player clicks in a random place like outside of the anvil's gui. This is my code:

    Code (Text):
    public void onPickingResultingItem(InventoryClickEvent eventItem) {
           
            if(eventItem.getView().getType() == InventoryType.ANVIL) {
               
                if(eventItem.getWhoClicked() instanceof Player) {
                   
                    Player player = (Player)eventItem.getWhoClicked();
                   
                    if(!eventItem.getCurrentItem().getType().equals(Material.AIR)) {
                       
                        if(storedItemMeta.containsKey(player) && initializedInventory.get(player)[2].equals(eventItem.getCurrentItem())) {
                           
                            if(eventItem.getCurrentItem().getItemMeta().hasLore()) {
                               
                                if(player.getExpToLevel() < expCost.get(player)) {
                                   
                                    eventItem.setCancelled(true);
                                   
                                }else {
                                   
                                    removeLore(eventItem.getCurrentItem(), player);
                                }
                               
                            }
                           
                        }
                    }
                   
                   
                }
               
               
            }
           
        }
    The problem obviously occurs when I check if the eventItem is not of Type Air. I tried checking if it's null first, but it still resulted in an error. How do I avoid this? If the player clicks in a slot, since the type of the slot is Air no error is produced.
     
  2. add this check before you proceed on checking if it's anvil
    Code (Java):
    if(eventItem.getClickedInventory() == null) return;
     
    • Agree Agree x 1
  3. Yes... the problem was that I was using .equals(); which resulted in an error again. It works this way, I just figured it out.