InventoryClickListener Throwing EventException

Discussion in 'Plugin Development' started by NightTerror6, Sep 21, 2017.

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

    NightTerror6

    I've got an InventoryClickListener that does a number of things, but I can't manage to figure out why it throws an EventException every time it's activated.

    I have pasted the entire class, so you can use it as reference. If you see any other problems/inefficient code, then please tell me.

    https://pastebin.com/5BXGSdy1
     
  2. Offline

    Zombie_Striker

    @NightTerror6
    1. Do not use the bungee chatcolor import if you want to distribute the plugin. Instead ,sue the org.bukkitChatcolor import.
    2. Code:
               if(i.getName().equals(ew.getName())) {
      This checks if the inventories type name is the same as ew.getName(), which will be true for all storage inventories with the same size. If you want to see if the titles of the inventories are the same, use getTitle instead
    3. The item's display name can be null. This is the main problem. Make sure the item has a displayname before getting it.
    4. If the foreach item is "air", the itewstack instance would actually be null. Null check the item instead of checking if it is air
    5. For this, you should null check to make sure the item is not null before using it.
    6. Code:
      }else { //Causes exception when not enough levels..?
                          p.closeInventory();
                          p.sendMessage(ChatColor.RED + "Not enough levels! (Required: 30)");
                      }
      Since levels cannot be negative, yes, it does throw an exception if there are not enough levels. If you really want players to be able to use this even if they do not have enough levels, use Math.max(0, LEVELS) to make sure you do not go out of bounds
    7. For open event:
      Code:
                   ItemStack inHand = p.getItemInHand();
      This can be null. Null check it before using it.
    8. Code:
               if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
      Use == for comparing enums.
     
Thread Status:
Not open for further replies.

Share This Page