Solved Checking for empty inventory give false positive

Discussion in 'Plugin Development' started by bennie3211, Apr 3, 2016.

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

    bennie3211

    Hello,

    Today I was implementing a methode to check if the player has an empty inventory before he joins a certain map/arena. To do that I typed this methode that returns a true/false value:

    Code:
    public Boolean hasEmptyInventory(Player p) {
            for (ItemStack item : p.getInventory().getContents()) {
                if (item != null) {
                    System.out.println(item.getType().toString());
                    //return false;
                }
            }
           
            for (ItemStack item : p.getInventory().getArmorContents()) {
                if (item != null) {
                    System.out.println(item.getType().toString());
                    //return false;
                }
            }
           
            return true;
        }
    I commented the return false because when I tested it with an emtpy inventory it returned false, so I tried to debug it with a System.out.println() and then print the item name in the console and it returns 'AIR' 4 times for a whole inventory. If I add an item (like a block of flower) it returns the item I've added + 4 times 'AIR' in the console. No errors occure but I can't find out why it returns 'AIR'. Can someone help me with this?

    Thnx
     
  2. Offline

    mcdorli

    1.: Why do you use Boolean (object) instead of boolean (primitive)?
    2.: Then check if item != null gg item.getType() != Material.AIR
    3.: System.out.println() can print out anything, you don't need .toString there
     
  3. Offline

    Lordloss

    you could simply check if Inventory.all is empty.
     
  4. Offline

    bennie3211

    1. Changed.
    2. But why does a empty slot return AIR? If I check if its AIR only it gives an error.
    3. Did it just to be sure.
     
  5. Offline

    mcdorli

    When you use &&, and 1 of the arguments is false, then it's going to stop, so if you do item != null && item.getType() != Material.AIR, then if it is null, it's going to stop before checking, if it is air
     
  6. @bennie3211 why not just check if the getinventory#getcontents() is empty? and if its not do the "you still have -"
     
  7. Offline

    Gonmarte

    Just get the contents of the inventory and if they are null return true if they arent null ( inv is not empty) return false.
     
  8. Offline

    mcdorli

    Have you even read the main post?
    An array can't be empty, it's size is always going to be 36.
     
  9. Offline

    Konato_K

    @bennie3211
    Inventory#getContents returns an ItemStack array that has null for empty slots
    Inventory#getArmorContents returns an ItemStack array that has an ItemStack of AIR for empty slots

    I have no idea why this inconsistency exists (it should either do null or AIR, but not both), but that's just how Bukkit is, annoying and inconsistent.

    So just change the check in the armor (but I would rather do the null and AIR check in both cases in case the behavior is changed in future versions)
     
    teej107 likes this.
  10. Offline

    bennie3211

    Thnx, I didn't read the docs because I thought it would be the same as the inventory contents (null). I do now check if the item != null && item.getType() != Material.AIR to check if the inventory is empty.
     
  11. Offline

    Konato_K

    @bennie3211 Is not in the docs, I found it out by getting an error in one of my plugins because of that
     
    bennie3211 likes this.
  12. Offline

    bennie3211

    Thanks for sharing :)
     
Thread Status:
Not open for further replies.

Share This Page