Solved InventoryClickEvent not ignoring air?

Discussion in 'Spigot Plugin Development' started by Moderance, May 29, 2016.

  1. So I am setting the clicked item to:

    Code (Text):
    String itemClicked = event.getCurrentItem().getItemMeta().getDisplayName();
    I'm struggling to set it so that it ignores air and if there's nothing in that inventory slot, otherwise it errors for me, I've tried multiple things such as:

    Code (Text):
    if (event.getCurrentItem().getItemMeta().getDisplayName() == null) { return;
  2. Just do if(event.getCurrentItem().getType() == Material.AIR) { return; }
    • Like Like x 1
  3. Add event.getCurrectItem() == null check, if you click around the inventory you'll printout a Error of nullpointerExcep....
  4. Yeah, that's what I'm trying to stop it doing so the console isn't spammed with those errors.
  5. Works, thanks, but it still continues to print NullPointerException's if they click around the inventory.
  6. i'll just give you a tip for Inventories:
    1. always check the Entity WhoClicked.
    2. always Cast the Clicker to Player after the check.
    3. try using else if for Each Item its helpful and Easier to use.
    4. always ignore air and null items. (if (event.getCurrectItem.getType == Material.AIR || event.getCurrectItem == null) event.setCacelled(true);).
  7. event.getCurrectItem == null still errors
  8. Switch the statements in point 4 around, otherwise it'll still throw the error.
    • Like Like x 1
  9. Thanks
  10. take an Example:

    Code (Text):
            if (event.getWhoClicked() instanceof Player) {
                Player e = (Player) event.getWhoClicked();
                if (event.getInventory().getTitle().equalsIgnoreCase("InventoryName")) {
                    if (event.getCurrentItem() == null || event.getCurrentItem().getType() == Material.AIR) event.setCancelled(true);
                 else if (event.getCurrentItem().getType() == Material.SIGN) {
                 } else if (event.getCurrentItem().getType() == Material.DIAMOND_SWORD) {