Could not pass event InventoryClickEvent to HDS

Discussion in 'Spigot Plugin Development' started by MrWouter, May 24, 2016.

  1. Hi, my plugin crashes if i click outside of the inventory, or on an "Air" block.
    Code:
    http://pastebin.com/JTCBEk3b

    Please help me, and tell me where i should put the code, i am not that experienced.. Thanks in advance!!
     
  2. Post the stacktrace?
     
  3. Make a null check....

    Not even needed. He doesn't check if the click or slot is null or air. Read post and code next time.
     
  4. Ah, sorry didn't check the pastebin yet :p
     
    • Like Like x 1
    • You don't check if the item is null
    • You don't check if the item has item meta
    • You don't check if the item meta has a display name
     
  5. @TheBlackTeddy Where should i put the null check, and can i have an example?
     
  6. Check if inventory is not null
    Check if inventory name is not null
    Check if the click is not null

    I am not going to spoonfeed you, but you should check those before your code.
     
  7. Should also check if item has itemmeta
     
  8. Yeah forgot that;


     
    • Like Like x 1
  9. This is what I would use. I hope this is ok? Anyone think this is fine?

    Code (Text):
                if (e.getCurrentItem().getItemMeta() == null) {
                  return;
                }
    Thx Pixl
     
    • Like Like x 1
  10. I usually just use
    Code (Text):

              if(item.hasItemMeta())
              {
                     // Do stuff
              }
     
     
    • Like Like x 1
  11. That is the same. But check if it has a displayname etc.
     
    • Like Like x 1
  12. Code (Text):

    if(item.hasItemMeta() && item.hasDisplayName())
    {
          //Do stuff
    }
     
    Better?
     
    • Like Like x 1
  13. Well normally when I register InventoryClickEvents, I only need two objects to make the code run smoothly. This for example is what I would use...

    Code (Text):
          @EventHandler
          public void onMyGui(InventoryClickEvent e){
                Player p = (Player) e.getWhoClicked();
                if (!e.getInventory().getName().equalsIgnoreCase(MyGui.GUI.getName())) {
                  return;
                }
                if (e.getCurrentItem().getItemMeta() == null) {
                  return;
                }
                //Then to get the individual item displayname

                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Store")){
                    // Do Stuff in here ofc
                    e.setCancelled(true);

                }
     
    Hope this helps, this is a really simple way of coding just so you know but for me it works.
    Thx Pixl
     
    • Like Like x 1
  14. let's see

    Code (Java):
    void event {

    if(!e.getCurrentItem().hasItemMeta())
     return;

     // check more check things

    // the event
     
    • Like Like x 1