Check if a player has items in their inventory

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

  1. Offline

    travja

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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!

    This post has been edited 3 times. It was last edited by travja Apr 27, 2012.
  2. Offline

    r0306

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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
    }

    This post has been edited 2 times. It was last edited by Seadragon91 Apr 27, 2012.
  5. Offline

    travja

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    K, I'll see what I can do with all this, Thanks everyone!
  6. Offline

    travja

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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?
  7. Offline

    r0306

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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
    }

    This post has been edited 7 times. It was last edited by r0306 Apr 27, 2012.
  8. Offline

    travja

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    This still doesn't work... it wants ItemStack[] not ItemStack... If I change it back it doesn't work...
  9. Offline

    travja

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Still just confuzzled
  10. Offline

    r0306

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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
    }
  11. Offline

    Darkman2412

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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
  12. Offline

    travja

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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....
  13. Offline

    travja

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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. }

    This post has been edited 3 times. It was last edited by travja Apr 28, 2012.
  14. Offline

    nala3

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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;
        }

    This post has been edited 2 times. It was last edited by nala3 Apr 28, 2012.
  15. Offline

    travja

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    That isn't working either....
    As I said, I need it in an if statement not a foreach.
  16. Offline

    Seadragon91

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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.
  17. Offline

    travja

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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. }

    This post has been edited 1 time. It was last edited by travja Apr 28, 2012.
  18. Offline

    Seadragon91

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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.
  19. Offline

    Darkman2412

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Code:java
    1. if(!Arrays.asList(player.getInventory().getContents()).isEmpty()) {
    2. // inventory is not empty
    3. }
  20. Offline

    nala3

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    That will work just fine, you just pass the method the player and it returns if the inventory is empty.
  21. Offline

    travja

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    K, I'll see what I can do with it.
  22. Offline

    JeterLP

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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. }

Share This Page