Check if a player has items in their inventory

Discussion in 'Plugin Development' started by travja, Apr 26, 2012.

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

    travja

    I need to confirm that my current code is correct, I want to check if the players inventory has anything in it and if it does, return an error message to the player. This is currently what I have:
    Code:java
    1. if(!(p.getInventory().getSize()== 0)){
    2. p.sendMessage(ChatColor.RED + "You are not allowed to join until you have a clear inventory!");
    3. }


    So I just need to confirm that this will check if the players inventory has something in it.
    Thanks in advance everyone!
     
  2. Offline

    r0306

    You need to take into account whether or not they are wearing armor as simply getting their inventory size will not get their equips. You need to remove the ! in the if statement as that is saying that if their inventory is not equal to 0, then send them the message, which is the opposite of what you are trying to do. If this is a command, you should return false after the statement.
     
  3. Offline

    travja

    Umm... I am trying to see if there are items in their inventory, so the ! is right as it says if their inventory size is not 0 therefore it is > 0 meaning there is things in the inventory, also, I will add the armor check too, I am just trying to confirm that this code is right.
     
  4. Offline

    Seadragon91

    You can forget this:
    Code:
    if(!(p.getInventory().getSize()== 0)){
    }
    getSize() returns how big the inventory is, not how many items are in there. You can use this to check if the inventory is empty:
    Code:
    boolean isEmpty = true;
    for (ItemStack item : player.getInventory().getContents()) {
        if(item != null) {
            isEmpty = false;
            break;
        }
    }
           
    if(isEmpty) {
        // inventory is empty
    } else {
        // inventory is not empty
    }
     
  5. Offline

    travja

    K, I'll see what I can do with all this, Thanks everyone!

    K, this isn't really working... I have a string of if and else statements, this is going in and else if() statement, I made a few changes but it isn't working.... Anybody else?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  6. Offline

    r0306

    It should be working. The only thing you need to change in that code is
    Code:
    if (isEmpty == true){
    Edit: Ok. I found out why it wasn't working. Inventory contents don't show up as null if the slot is empty. It shows up as Air, so you would add a check:

    Code:
    boolean isEmpty = true;
    for (ItemStack item : player.getInventory().getContents()) {
        if(item.getType() != Material.AIR && item !=null) {
            isEmpty = false;
            break;
        }
    }
     
    if(isEmpty == true) {
        // inventory is empty
    } else {
        // inventory is not empty
    }
     
  7. Offline

    travja

    This still doesn't work... it wants ItemStack[] not ItemStack... If I change it back it doesn't work...

    Still just confuzzled

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  8. Offline

    r0306

    Kk. Then try this instead.
    Code:
    boolean isEmpty = true;
    ItemStack[] item = player.getInventory().getContents();
    for (int i = 0; i < item.length; i ++) {
        if(item[i].getType() != Material.AIR && item !=null) {
            isEmpty = false;
            break;
        }
    }
     
    if(isEmpty == true) {
        // inventory is empty
    } else {
        // inventory is not empty
    }
     
  9. Offline

    Darkman2412

    Code:java
    1. ItemStack[] contents = player.getInventory().getContents();
    2. If(!contents.isEmpty())
    3. // inv is not empty

    I don't know if this works because I'm not near my computer :p
     
  10. Offline

    travja

    I don't think there even is a isEmpty method for player inventories like this... I have tried things like this, but none are working....

    The main problem with this is I am checking for multiple things and this is only one of them, so I have something like this:
    Code:java
    1. if(some stuff to check){
    2. //error message
    3. }else if(some other stuff){
    4. //another error message
    5. }else if(Inventory Checking){
    6. //the error message I want for items in the inventory
    7. }else{
    8. //code goes through
    9. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  11. Offline

    nala3

    Code:java
    1. if(some stuff to check){
    2. //error message
    3. }else if(some other stuff){
    4. //another error message
    5. }else if(inventoryEmpty(Player)){
    6. //the error message I want for items in the inventory
    7. }else{
    8. //code goes through
    9. }
    10.  

    Code:
    private boolean inventoryEmpty(Player p){
            PlayerInventory inv = p.getInventory();
            ItemStack[] contents = inv.getContents();
            for(ItemStack slot : contents){
                if(slot.getTypeId() != 0){
                    return false;
                }
            }
            return true;
        }
     
  12. Offline

    travja

    That isn't working either....
    As I said, I need it in an if statement not a foreach.
     
  13. Offline

    Seadragon91

    You want to execute the code more than one time? If yes you could paste the code into a own method that returns a boolean. Then you could use this in a simple if statement, where you need to add the ItemStack array.
     
  14. Offline

    travja

    I don't quite understand... I am running the code for this once, after I have checked for other things. For instance:
    Code:java
    1. if(player.isSleeping){
    2. player.sendMessage(ChatColor.RED + "You can't do this while you are sleeping!");
    3. }else if(player.isFlying){
    4. player.sendMessage(ChatColor.RED + "You can't do this while you're flying!");
    5. }else if(player. Whatever inventory stuff we figure out){
    6. player.sendMessage(ChatColor.RED + "You can't do this until your inventory is empty!");
    7. }
     
  15. Offline

    Seadragon91

    Code:
    if(player. Whatever inventory stuff we figure out){
    Here you want to check if the inventory is empty? If yes, you could call from there a method that includes the code to check if the inventory is empty or not.
     
  16. Offline

    Darkman2412

    Code:java
    1. if(!Arrays.asList(player.getInventory().getContents()).isEmpty()) {
    2. // inventory is not empty
    3. }
     
  17. Offline

    nala3

    That will work just fine, you just pass the method the player and it returns if the inventory is empty.
     
  18. Offline

    travja

    K, I'll see what I can do with it.
     
  19. Offline

    JeterLP

    If any other people are searching for something like this:

    Code:java
    1. public static boolean isEmpty(Player player) {
    2. int empty = 0;
    3. int armors = 0;
    4. for (ItemStack item : player.getInventory().getContents()) {
    5. if (item == null || item.getType() == Material.AIR) empty++;
    6. }
    7. for (ItemStack armor : player.getInventory().getArmorContents()) {
    8. if (armor == null || armor.getType() == Material.AIR) armors++;
    9. }
    10. return empty == player.getInventory().getContents().length && armors == player.getInventory().getArmorContents().length;
    11. }
     
Thread Status:
Not open for further replies.

Share This Page