Check to see if Inventory is full?

Discussion in 'Plugin Development' started by ShadowLAX, Aug 7, 2013.

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

    ShadowLAX

    Hello bukkit,I have a problem. I'm trying to make a kit plugin, and I don't know how to check to see if the player's inventory is full, or when it fills up to place it on the ground at their location, like the essentials /kit. Thanks!
     
  2. Offline

    JPG2000

    ShadowLAX Check if player.getinventory .contains null, I belive
     
  3. Offline

    david_rosales

    Here is something i coded quickly.. I would look it over and make sure it does what you are asking
    Code:java
    1.  
    2. public boolean isFull(Player p){
    3. for(int x = 0; x <= p.getInventory().getSize(); x++){
    4. if(p.getInventory().getItem(x) == null){
    5. return false;
    6. }
    7. }
    8. return true;
    9. }
    10.  

    That should work... If not tag me and tell me.. I will see if i can find another way after that. Hope it works! :D
     
  4. Offline

    ShadowLAX

  5. Offline

    ShadowLAX

    JPG2000 I can't seem to get it to compare to null :S How would I do that?
     
  6. Offline

    flaaghara


    Try this.

    Code:java
    1. if(p.getInventory.getItem(x).getMaterial() == Material.AIR) return false;
     
  7. Offline

    ShadowLAX

  8. Offline

    flaaghara

    There is no item x; x is an index, which means it's an integer. Anyways, the code should check if the slot is empty. Otherwise I can't find the implementation of PlayerInventory so I don't know what getItem() returns if there's no item at that index.
     
  9. Offline

    raGan.

    This code returns number of free spots in player's inventory.
    Code:
    int freeSpots = 0;
    for(final ItemStack item : player.getInventory().getContents()) {
        if(item == null) {
            freeSpots++;
        }
    }
    return freeSpots;
     
Thread Status:
Not open for further replies.

Share This Page