Something like player.getInventory().getFreeSpace()

Discussion in 'Plugin Development' started by AndyMcB1, Oct 14, 2013.

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

    AndyMcB1

    I need to work out if a player has any empty slots in their inventory... How would I go about doing this?
     
  2. Offline

    RealDope

    Loop over inventory slots, if it's empty, +1 to a count of open slots.
     
  3. Offline

    AndyMcB1

    How?
     
  4. Offline

    bobbob1870

    Make a int named emptyCount and set it to 0 then make an enhanced for loop looping through the contents of the inventory inside the loop check if the current item == null and if it does add 1 to emptyCount.

    If you're simply trying to see if the inventory has at least one open slot instead do this: Make a boolean named hasEmpty or something similar then make an enhanced for loop looping through the contents of the inventory and inside the for loop check if the current item == null and if it does set hasEmpty to true and break out of the loop.
     
  5. Offline

    Appljuze

    You could also use the .getContents() method and test the length of it. If it's less than 36, then there's free space. This might be a slightly more efficient way of doing it. Either would work
     
  6. Offline

    bobbob1870

    Since .getContents() returns an array wont it always have the same length?

    Edit: However doing .contains(null) might give the desired effect.
     
  7. Offline

    Appljuze

    .contains(null) would work. Does the .getContents() create an array filled up to the items It finds or does it fill the empty slots with a null and always return a size 36 array?
     
  8. Offline

    The_Doctor_123

    bobbob1870
    Not exactly, I'm pretty sure that Bukkit specified the array's length to the number of used slots in the inventory.

    So with that being said, this could easily be done:
    Code:java
    1. int openSlots = player.getInventory().getSize() - player.getInventory().getContents().length;
     
  9. Offline

    bobbob1870

    From what I know .getContents() returns an ItemStack array of the inventory so the length will always be the size of the inventory with empty slots being null.

    The_Doctor_123 Just tested it and yes .getInventory().size() and .getInventory().getContents().length both return the same value even when there are empty items in the inventory
     
  10. Offline

    AndyMcB1

    Hm... Thanks guys - stuck now.. Not sure what to do.
     
  11. Offline

    bobbob1870

    player.getInventory().contains(null); should give you what you're looking for
     
  12. Offline

    AndyMcB1

    Oh - right! Thanks!

    I'm surprised there's no method for this.
     
  13. Offline

    bobbob1870

    No problem, and I guess the bukkit devs hadn't thought of it? Or maybe just thought people would figure it out. Either way don't forgot to add the [Solved] prefix if it works :D
     
  14. Offline

    AndyMcB1

    bobbob1870

    "The method contains(Material) is ambiguous for the type PlayerInventory"
     
  15. Offline

    bobbob1870

    Darn I was really hoping that would work :/ okay well then you can still do it this way:
    Code:java
    1. boolean hasEmpty = false;
    2. for (ItemStack item : player.getInventory().getContents()) {
    3. if (item == null) {
    4. hasEmpty = true;
    5. break;
    6. }
    7. }
     
  16. Offline

    xTrollxDudex

    Are you building against bukkit or craftbukkit?
     
  17. Offline

    AndyMcB1

    Bukkit.
     
  18. Offline

    Garris0n

    I think he might have forgot the .getContents() part, although you can't call .contains() anyway afaik.
     
Thread Status:
Not open for further replies.

Share This Page