Add item to players inventory if inventory is empty

Discussion in 'Plugin Development' started by glasseater, Jan 22, 2016.

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

    glasseater

    Hey everyone,

    Today I am wondering how can I add an item to a players inventory on join, but only if their inventory is empty.
    For example if they have anything else in their inventory, it won't add the item. I don't want it to add if their inventory is full.
    I tried:
    Code:
     if(inv.firstEmpty() ==-1)
    then adding the item but that did not work. If you have any ideas they would be greatly appreciated! Thanks!

    Here is full event

    Code:
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent evt) {
        PlayerInventory inv = evt.getPlayer().getInventory();
     
       if(inv.firstEmpty() ==-1)
            inv.setItem(4, guiItem);
               
            }
     
  2. Offline

    HenrySartori

    You forget to put { in your if.
    Anyway, this will not solve anything. inv.firstEmpty can't be used here, because: FirstEmpty search for the first empty slot in the inventory. If it's all full, will cause an NPE.
    Did you tried --> ?
    Code:
    //This check all items in the inventory but DOESNT check armor. If you want to check armor just do the same with inv.getArmorContents
    
    for (ItemStack item : inv.getContents()){
        if (item == null){
            //the inventory is cleared
            return false;
        } else {
            //the inventory have an item/is full
            return false;
        }
    }
    Yes, I edited this many times because I'm stupid.
     
    Last edited: Jan 22, 2016
  3. Offline

    Evonoucono

    Last edited: Jan 22, 2016
  4. Offline

    HenrySartori

  5. Offline

    Evonoucono

    @HenrySartori your edit was posted after my reply... lol
     
  6. Offline

    HenrySartori

    Sorry, I didn't see that. Anyway, we expect it help the guy =D
    EDIT: Thats because I edited without refreshing the page, so I didn't see your post, sorry
     
    Evonoucono likes this.
  7. Offline

    Evonoucono

    this won't check if the inventory is cleared because if the first slot of the inventory is empty this code will assume the rest of the slots are empty too, because you returned false at the first sight of an empty inventory slot. The code will only check for the first slot.
     
    Last edited: Jan 22, 2016
  8. Offline

    HenrySartori

    inv.getContents() get ALL the contents from player's inventory...
    So if inv.getContents() == null, the inventory is empty...
    WTF you just said?
     
  9. Offline

    Evonoucono

    Think about it. You are looping through every inventory slot starting with the first slot and checking if it's null. If that first inventory slot is null, then your code states that the inventory is empty, even though slot #2 could contain an item.
     
  10. Offline

    HenrySartori

    I got what you just said, but I didn't gived the code for him, so I just put "return false;" for an example '-'
     
  11. Offline

    Evonoucono

    Yes but... Why did you say if the ItemStack is null then the inventory is empty... That shouldn't be there at all...
     
  12. Offline

    HenrySartori

    I tried and it worked correctly. Thats because it is an ItemStack that represents ALL ItemStacks in player's inventory, so if the variable 'item' is null, there's no item in Player's Inventory, and i put return false because that was just an example, he could do whatever he want, I just ended my example there.
    Try it by yourself, you'll see it works.
     
  13. Offline

    Evonoucono

    Just tried it and it didn't work, your code:
    Code:
    for (ItemStack item : p.getInventory().getContents()){
                    if (item == null){
                        Bukkit.broadcastMessage("EMPTY");
                        return false;
                    } else {
                        Bukkit.broadcastMessage("NOT EMPTY");
                        return false;
                    }
                }
    and this is what happened...
    http://prntscr.com/9to135
    this will only work for the first inventory slot (which is the first hotbar slot)
    and lol no! The ItemStack does not represent all itemstacks in the players inventory! When you use : in a for loop you are iterating over everything in the list. In this case the list is the players inventory, and you are iterating individually over every inventory slot.
     
    Last edited: Jan 22, 2016
  14. Offline

    HenrySartori

    But you put return false...
    *facepalm*
    You answered yourself...
     
  15. Offline

    Evonoucono

    A) why did you put "return false" originally if you knew it wouldn't work
    B) Without it, it doesn't work anyways
    Edit: C) I didn't want to spoonfeed @glasseater but I feel I need to prove a point.
    Code:
                for(ItemStack item : p.getInventory().getContents()){
                    if(item != null) return false;
                }
                //Do What You Want
    This works because it doesn't instantly assume your inventory is one slot big.
     
    Last edited: Jan 22, 2016
  16. Offline

    HenrySartori

    A) I put it because its an example (I said this over 3 times), i just wanted to stopped there!
    B) It doesn't works if you put return false, its obviously, and yes, without it, it pretty works, if you have brain and know you need to put return false after for.
    C) LOL dude, your right, it doesn't instantly assumes that, only one line before, wow
    I will don't answer you anymore, just search 'check if inventory is empty' in google and you'll see many threads with similar codes than my one.
    Bye bye!
     
Thread Status:
Not open for further replies.

Share This Page