Check if a player has items in their inventory

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

Thread Status:
Not open for further replies.
  1. 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. 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. 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. You can forget this:
    Code (Text):
    1. if(!(p.getInventory().getSize()== 0)){
    2. }
    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 (Text):
    1. boolean isEmpty = true;
    2. for (ItemStack item : player.getInventory().getContents()) {
    3.     if(item != null) {
    4.         isEmpty = false;
    5.         break;
    6.     }
    7. }
    8.        
    9. if(isEmpty) {
    10.     // inventory is empty
    11. } else {
    12.     // inventory is not empty
    13. }
     
  5. K, I'll see what I can do with all this, Thanks everyone!
     
  6. 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. It should be working. The only thing you need to change in that code is
    Code (Text):
    1. 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 (Text):
    1. boolean isEmpty = true;
    2. for (ItemStack item : player.getInventory().getContents()) {
    3.     if(item.getType() != Material.AIR && item !=null) {
    4.         isEmpty = false;
    5.         break;
    6.     }
    7. }
    8.  
    9. if(isEmpty == true) {
    10.     // inventory is empty
    11. } else {
    12.     // inventory is not empty
    13. }
     
  8. This still doesn't work... it wants ItemStack[] not ItemStack... If I change it back it doesn't work...
     
  9. Still just confuzzled
     
  10. Kk. Then try this instead.
    Code (Text):
    1. boolean isEmpty = true;
    2. ItemStack[] item = player.getInventory().getContents();
    3. for (int i = 0; i < item.length; i ++) {
    4.     if(item[i].getType() != Material.AIR && item !=null) {
    5.         isEmpty = false;
    6.         break;
    7.     }
    8. }
    9.  
    10. if(isEmpty == true) {
    11.     // inventory is empty
    12. } else {
    13.     // inventory is not empty
    14. }
     
  11. 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. 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. 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. }
     
  14. 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 (Text):
    1. private boolean inventoryEmpty(Player p){
    2.         PlayerInventory inv = p.getInventory();
    3.         ItemStack[] contents = inv.getContents();
    4.         for(ItemStack slot : contents){
    5.             if(slot.getTypeId() != 0){
    6.                 return false;
    7.             }
    8.         }
    9.         return true;
    10.     }
     
  15. That isn't working either....
    As I said, I need it in an if statement not a foreach.
     
  16. 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. 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. }
     
  18. Code (Text):
    1. 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. Code:java
    1. if(!Arrays.asList(player.getInventory().getContents()).isEmpty()) {
    2. // inventory is not empty
    3. }
     
  20. That will work just fine, you just pass the method the player and it returns if the inventory is empty.
     
  21. K, I'll see what I can do with it.
     
  22. 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