Solved InventoryClickEvent console error on empty slot

Discussion in 'Plugin Development' started by baenii, Dec 29, 2017.

Thread Status:
Not open for further replies.
  1. Hello again! I have a problem.
    If i click on an item in my inventory with the name "§7Choose a gamemode" it will check the item name i click and that works perfectly. But when i click on an empty slot or out of the inventory so anywere else in the game (when inventory is open) the console gets a big error.
    Here is my code.. Normally the player should get a Message ("test") if he don't click one of the items i think. But it didn't work. Someone know how to fix?

    Code:
    if(e.getInventory().getName().equalsIgnoreCase("§7Choose a gamemode")) {
                e.setCancelled(true);
                if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§aSpawn")) {
                    p.teleport(location);
                    p.playSound(p.getLocation(), Sound.ENDERMAN_TELEPORT, 1F, 2F);
                } else if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§cSG-FFA")) {
                    p.teleport(location2);
                    p.playSound(p.getLocation(), Sound.ENDERMAN_TELEPORT, 1F, 2F);
                } else if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§cComing Soon..")) {
                    p.closeInventory();
                    p.playSound(p.getLocation(), Sound.VILLAGER_NO, 1F, 1F);
                } else p.sendMessage("test");
            }
     
  2. Offline

    RunsWithShovels

    Check if the item is null prior to trying to get metadata.

    If (!item is null){
    Check if item has data{
    Check if items data is what you need{
    //Do stuff
    }
    }
    }
     
  3. I don't really understand :/
     
  4. Offline

    RunsWithShovels

    @baenii What's not to understand?

    You check if the item is not null > if it's not > check if the item has metadata > if it does > check if the metadata matches what you need it match > do stuff
     
  5. I want to cancel the event when the player is clicking on an empty slot or out of the inventory.
     
  6. Offline

    RunsWithShovels

    You're already cancelling the click event.
    You need to check if the item is null or not before checking what the item is.

    pseudo code:
    if (inventory is right inventory){
    cancelClickEvent;
    if (!event.getitem == null){
    then check for the metadata.

    }
    }
     
  7. Is this what u mean?
    Code:
    if(e.getInventory().getName().equalsIgnoreCase("§7Choose a gamemode")) {
                e.setCancelled(true);
                if(!(e.getCurrentItem() == null)) {
                    if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§aSpawn")) {
                        p.setVelocity(p.getVelocity().setY(0.5D));
                        p.teleport(locSpawn);
                        Bukkit.getServer().getWorld("world").playSound(p.getLocation(), Sound.ENDERMAN_TELEPORT, 1F, 2F);
                        Bukkit.getServer().getWorld("world").playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 20);
                    } else if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§cSG-FFA")) {
                        p.setVelocity(p.getVelocity().setY(0.5D));
                        p.teleport(locSGFFA);
                        Bukkit.getServer().getWorld("world").playSound(p.getLocation(), Sound.ENDERMAN_TELEPORT, 1F, 2F);
                        Bukkit.getServer().getWorld("world").playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 20);
                    } else if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§cComing Soon..")) {
                        p.setVelocity(p.getVelocity().setY(0.2D));
                        p.damage(0);
                        p.closeInventory();
                        p.playSound(p.getLocation(), Sound.VILLAGER_NO, 1F, 1F);
                    }
                }
            }
    The error is stille alive.
     
  8. Offline

    RunsWithShovels

    @baenii What error? Where is the stacktrace?
     
  9. This is the error.
    I tried to check if the clicked slot != 0 and when i clicked on the first slot in the inventory the error is not coming. But first i don't want to list all slots in the code so for example "slot != 0 || slot != 1 || slot != 2 ..."
    And also if i click out of the inventory, the error is coming. So this would not be the best solution.

    Code:
    if(e.getInventory().getName().equalsIgnoreCase("§7Choose a gamemode")) {
                if(e.getSlot() != 1-1) {
                    if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§aSpawn")) {
                        p.setVelocity(p.getVelocity().setY(0.5D));
                        p.teleport(locSpawn);
                        Bukkit.getServer().getWorld("world").playSound(p.getLocation(), Sound.ENDERMAN_TELEPORT, 1F, 2F);
                        Bukkit.getServer().getWorld("world").playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 20);
                    } else if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§cSG-FFA")) {
                        p.setVelocity(p.getVelocity().setY(0.5D));
                        p.teleport(locSGFFA);
                        Bukkit.getServer().getWorld("world").playSound(p.getLocation(), Sound.ENDERMAN_TELEPORT, 1F, 2F);
                        Bukkit.getServer().getWorld("world").playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 20);
                    } else if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§cComing Soon..")) {
                        p.setVelocity(p.getVelocity().setY(0.2D));
                        p.damage(0);
                        p.closeInventory();
                        p.playSound(p.getLocation(), Sound.VILLAGER_NO, 1F, 1F);
                    }
                }
            }
    EDIT: With "if(!(e.getCurrentItem() == null)) { ... }" the error doesn't occur when i click out of the inventory. just when i click an empty slot. Now i could add "slot != 0 || slot != 1 || slot != 2 ..." but that would be much work for checking all the slots.. Any idea?

    EDIT EDIT:
    Code:
    if(e.getInventory().getName().equalsIgnoreCase("§7Choose a gamemode")) {
                if(!(e.getCurrentItem() == null)) {
                    if(e.getSlot() == 12-1 || e.getSlot() == 14-1 || e.getSlot() == 16-1) {
                        if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§aSpawn")) {
                            p.setVelocity(p.getVelocity().setY(0.5D));
                            p.teleport(locSpawn);
                            Bukkit.getServer().getWorld("world").playSound(p.getLocation(), Sound.ENDERMAN_TELEPORT, 1F, 2F);
                            Bukkit.getServer().getWorld("world").playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 20);
                        } else if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§cSG-FFA")) {
                            p.setVelocity(p.getVelocity().setY(0.5D));
                            p.teleport(locSGFFA);
                            Bukkit.getServer().getWorld("world").playSound(p.getLocation(), Sound.ENDERMAN_TELEPORT, 1F, 2F);
                            Bukkit.getServer().getWorld("world").playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 20);
                        } else if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§cComing Soon..")) {
                            p.setVelocity(p.getVelocity().setY(0.2D));
                            p.damage(0);
                            p.closeInventory();
                            p.playSound(p.getLocation(), Sound.VILLAGER_NO, 1F, 1F);
                        }
                    }
                }
            }
    That one works now perfectly with checking if the clicked slot is one of the slots where the items are but it's not very nice that i have to add all not empty slots in the code if there are many items.. U understand?
     
    Last edited: Dec 30, 2017
  10. Offline

    RunsWithShovels

    USE THIS.......

    Also you really... need to learn the API, that's what the javadocs are for.

    Code:
    Code:
    
    if(e.getInventory().getName().equalsIgnoreCase("§7Choose a gamemode")) {
                if(e.getCurrentItem() == null) || !e.getCurrentItem.hasMetaData()) {
             
                        if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§aSpawn")) {
                            p.setVelocity(p.getVelocity().setY(0.5D));
                            p.teleport(locSpawn);
                            Bukkit.getServer().getWorld("world").playSound(p.getLocation(), Sound.ENDERMAN_TELEPORT, 1F, 2F);
                            Bukkit.getServer().getWorld("world").playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 20);
                        } else if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§cSG-FFA")) {
                            p.setVelocity(p.getVelocity().setY(0.5D));
                            p.teleport(locSGFFA);
                            Bukkit.getServer().getWorld("world").playSound(p.getLocation(), Sound.ENDERMAN_TELEPORT, 1F, 2F);
                            Bukkit.getServer().getWorld("world").playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 20);
                        } else if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§cComing Soon..")) {
                            p.setVelocity(p.getVelocity().setY(0.2D));
                            p.damage(0);
                            p.closeInventory();
                            p.playSound(p.getLocation(), Sound.VILLAGER_NO, 1F, 1F);
                        }               
                }
            }
    
    
     
  11. @RunsWithShovels
    This error occur when i use this code:
    Code:
    if(e.getInventory().getName().equalsIgnoreCase("§7Choose a gamemode")) {
                if(e.getCurrentItem() == null || !(e.getCurrentItem().hasItemMeta())) {
            
                        if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§aSpawn")) {
                            p.setVelocity(p.getVelocity().setY(0.5D));
                            p.teleport(locSpawn);
                            Bukkit.getServer().getWorld("world").playSound(p.getLocation(), Sound.ENDERMAN_TELEPORT, 1F, 2F);
                            Bukkit.getServer().getWorld("world").playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 20);
                        } else if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§cSG-FFA")) {
                            p.setVelocity(p.getVelocity().setY(0.5D));
                            p.teleport(locSGFFA);
                            Bukkit.getServer().getWorld("world").playSound(p.getLocation(), Sound.ENDERMAN_TELEPORT, 1F, 2F);
                            Bukkit.getServer().getWorld("world").playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 20);
                        } else if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§cComing Soon..")) {
                            p.setVelocity(p.getVelocity().setY(0.2D));
                            p.damage(0);
                            p.closeInventory();
                            p.playSound(p.getLocation(), Sound.VILLAGER_NO, 1F, 1F);
                        }              
                }
            }
     
  12. Offline

    KingOfTheEast01

    It's hard to tell what the error is pointing to when we can't see which line of code is which. When I put this code into an IDE, there isn't even the amount of lines of code for an error to be on line 28, which the error says there is. If you were to supply your full class, it would help us read the error message more accurately.
     
  13. Offline

    RunsWithShovels

    Use it this way, not sure why the initial code I posted was altered..

    Notice the check for null or no meta. Do it exactly this way.

    if(e.getInventory().getName().equalsIgnoreCase("§7Choose a gamemode")) {


    if(e.getCurrentItem() == null) || !e.getCurrentItem.hasMetaData()) {
    Return;
    }

    if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§aSpawn")) {
    ......}
     
  14. Offline

    KingOfTheEast01

    This would definitely work. I noticed that you checked to see if it was null, and used it if it was, but there is another way to do all of this. You could check to see if it's not null, and if it isn't continue with the code, and have an else statement that returns it.
     
  15. Offline

    RunsWithShovels

    You don't want to continue with the code at all. You want to stop it there and return so there is no possibility to throw an error. Nesting if statements makes code confusing and harder to debug. Also if you would have read his previous attempts, you would have seen that he tried it without success.
     
  16. Thank you guys!
     
  17. Offline

    KingOfTheEast01

    He wasn't successful because he was checking to see if it was null. If it was null, it continued on. He forgot to add an exclamation mark to see if it was not null. Anyhow, I was just trying to make the point that you could do it that way. You wouldn't have anymore if statements that way then how you did it.
     
  18. Offline

    RunsWithShovels

    I'm fully aware of the reasons, believe me.......
    Tim can get a lock please.
     
Thread Status:
Not open for further replies.

Share This Page