Solved InventoryClickEvent error

Discussion in 'Plugin Development' started by Krotass, May 3, 2014.

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

    Krotass

    Hey guys. I'm having a problem with a code. It works but it gives me an error in the console

    It gives me an error if I click on a normal glass pane
    Line number 37 is when it checks for the item of the display name cobblestone.

    Code:
    Code:
        @EventHandler
        public void OnInventoryClicks(InventoryClickEvent e) {
            HumanEntity p = e.getWhoClicked();
            if (e.getCurrentItem() != null && e.getCurrentItem().getType() == Material.THIN_GLASS) {
             
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Cobblestone")) {
                    e.setCancelled(true);
                    p.setGameMode(GameMode.CREATIVE);
                }
             
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Wood")) {
                    e.setCancelled(true);
                    p.setGameMode(GameMode.SURVIVAL);
                }
            }
         
        }

    Error:
    [​IMG]
     
  2. Offline

    Mr360zack

    try making sure you check if .item.hasItemMeta()
     
  3. Offline

    rfsantos1996

    if(e.getCurrentItem().getItemMeta().hasDisplayName()) {
     
  4. Offline

    Drkmaster83

    ItemStacks are retarded, do this:
    Code:
    if((e.getCurrentItem() != null && (!e.getCurrentItem.getType().equals(Material.AIR))) && e.getCurrentItem().getType().equals(Material.THIN_GLASS)) {}
    
    It isn't null if the type is AIR, but it is used as if it were, as AIR is essentially a null object.
     
  5. Offline

    coasterman10

    I have never seen CraftBukkit or Spigot (99% of bukkit servers) return an item stack of air. There is nothing wrong with checking the item type.

    What is probably returning null is getDisplayName(). Check if this is null before your two if statements that check if it equals "Cobblestone" or "Wood".
     
  6. Offline

    Krotass

    Mr360zack

    I was to lazy to test for that so I did something else which works. Would never have found out about this if you wouldn't have had said that :D

    Code:
    @EventHandler
        public void OnInventoryClicks(InventoryClickEvent e) {
            HumanEntity p = e.getWhoClicked();
            if (e.getCurrentItem() != null && e.getCurrentItem().getType() == Material.THIN_GLASS) {
         
              if (!e.getCurrentItem().hasItemMeta()) {
                    return;
                }
         
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Cobblestone")) {
                    e.setCancelled(true);
                    p.setGameMode(GameMode.CREATIVE);
                }
         
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Wood")) {
                    e.setCancelled(true);
                    p.setGameMode(GameMode.SURVIVAL);
                }
            }
     
        }
    I added this:
    Code:
    if (!e.getCurrentItem().hasItemMeta()) {
                    return;
                }



    Thanks for helping me everyone!
     
  7. Offline

    Drkmaster83

    When iterating through an inventory's contents, I've had it return an ItemStack of AIR and throw an NPE when I tried to use it later.
    I never said there was anything wrong with checking the type, in fact, I said that could be one of the causes.
     
  8. Offline

    coasterman10

    Drkmaster83 That's interesting, thanks for sharing. I'll probably be including air checks from now on, though in this case checking if the item was of another material actually implicitly rules out the possibility of it being air.
     
  9. Offline

    Drkmaster83

    I was just looking for any way to solve it, really. xD Sometimes it's the things you don't think about that cause you grief.
     
Thread Status:
Not open for further replies.

Share This Page