Search code examples
javaminecraftbukkitinventory

How to get the name of a clicked item in an Inventory


So, I need to get the name of a Custom Item that's in a Custom inventory using the InventoryClickEvent event. I've tried to get the Item Meta but it returns a NullPointerException. So, I made a little if statement to take care of the Item that's clicked not having Meta and the If statement fired. So, the Item doesn't have an Item Meta even though it has a lore and Display name. Please note that I don't deal with Inventories much so I'm kinda new to it. Also, I am setting the Item Meta with Display name and Lore in another class but I don't think that should affect anything. Here's my code for the InventoryClickEvent class.

public class clickEvent implements Listener
{
    private Files files = new Files();
    private KitPvP kit = KitPvP.getInstance();
    private Kit kits = new Kit();
    private InvUtils invUtils = new InvUtils();

    @EventHandler
    public void onInventoryClick(InventoryClickEvent e)
    {
        HumanEntity player = e.getWhoClicked();

        Inventory inv = e.getClickedInventory();
        ItemStack item = e.getCurrentItem();

        if (player instanceof Player)
        {
            if (inv != null && inv.getContents() != null && !inv.getContents().equals(Material.AIR) && inv.getName() != null && !inv.getName().equalsIgnoreCase(""))
            {
                String name = ChatColor.stripColor(inv.getName());

                if (name.equalsIgnoreCase("Kits: ") || name.equalsIgnoreCase("Kits:"))
                {
                    try
                    {
                        if (!kit.getDataFolder().exists())
                        {
                            kit.getDataFolder().mkdirs();
                        }

                        File kFolder = files.getFolder("Kits");
                        if (!kFolder.exists())
                        {
                            files.createFolder("Kits");
                        }

                        File menu = files.getFolder("GUI Menu");
                        if (!menu.exists())
                        {
                            files.createFolder("GUI Menu");
                        }
                        if (!e.getCursor().hasItemMeta())
                        {
                            Logger.log("Clicked Item does not have Item Meta!");
                            return;
                        }

                        if (!e.getCursor().getItemMeta().hasDisplayName())
                        {
                            Logger.log("Clicked Item does not have Display Name!");
                            return;
                        }
                        String check1Name = ChatColor.stripColor(e.getCursor().getItemMeta().getDisplayName());
                        String check2Name = check1Name.replace(" Kit", "");
                        File gItem = files.getGuiItem(check2Name);

                        if (!gItem.exists())
                        {
                            Logger.log("Cannot find required Files!");
                            return;
                        }
                        String itemName = gItem.getName();

                        if (itemName.equalsIgnoreCase(check2Name))
                        {
                            kits.equipKit(player, itemName);
                            e.setCancelled(true);
                        }
                    } catch (Exception ex)
                    {
                        ex.printStackTrace();
                    }
                }
            }

        }
    }
}

Solution

  • It's a bit hard to understand your problem as it needs for information but here goes.

    1) I suppose that you use your event to carry over items etc and thus e.getCursor() resolves to an Item. Are you sure that the retrieved object contains meta when it's created, bundled in the event and passed around? My first though would be to check that one that the emitter of the event is creating the correct information.

    2) Before the first if clause you do e.getCurrentItem();. This is a bit confusing since the return object seems to be an ItemStack. Your naming convention should be revisited. Same goes for the rest of the code. What is the ItemStack object holding and why can't you retrieve the item in question from there?

    3) As mentioned in #1 check what the emitter of the event is doing. Most probably the ItemMeta are not sent due to this.

    Also a quick hint. Avoid long method bodies like that. Try to breakdown your problem into smaller ones. This would make the debugging and maintenance process a whole lot easier. Finally avoid doing String equality check against constants the way you are. Instead of someVar.equals("someConstant") due "someConstant".equals(someVar). This is a quite nice trick into avoiding NPEs.