Inventory Slots

Discussion in 'Plugin Development' started by Vinceguy1, Feb 8, 2013.

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

    Vinceguy1

    if(player.getInventory().getSize() > 7)

    getSize() always returns 36, is there anyway i can do the same thing but if they have like 1 slot in use like say all they have is 1 sand block in there inventory its using 1 slot and it would return 35 or they have 2 sand blocks but 1 in each slot so it would return 34, help please?
     
  2. Offline

    RealDope

    Code:JAVA
    1.  
    2. int i = 0;
    3. for(Slot s : Inventory.getSlots()) {// psuedocode, I don't know the methods off the top of my head
    4. if(s.getType() == Material.AIR) {
    5. i++;
    6. }
    7. }
    8.  
     
  3. Offline

    Vinceguy1

    There is nothing even like that in a players inventory.
     
  4. Offline

    RealDope

    Here, got the actually code for you:

    Code:JAVA
    1.  
    2. int count = 0;
    3. for(ItemStack i : player.getInventory().getContents()) {
    4. if(i.getType() == Material.AIR) {
    5. count++;
    6. }
    7. }
    8.  


    Alternatively, there is a chance that .getContents() will not contain the slots that are empty, if so, do this:
    Code:JAVA
    1.  
    2. int count = 0;
    3. for(ItemStack i : player.getInventory().getContents()) {
    4. count++;
    5. }
    6. count = player.getInventory().getSize() - count;
    7.  

    AlexLeporiday
    I think he's just asking how to count the number of empty slots lol
     
  5. .getContents() always returns 36 ItemStacks including slots filled with air.
     
  6. Offline

    Vinceguy1

    Your right it does >.>

    Does anyone have an idea on how to do this?
     
  7. Offline

    RealDope

    I just gave you the code... The first code block in my previous post, counts all the slots that are filled with air. Hence, all the slots that are "open"
     
  8. Offline

    Vinceguy1

    Doesn't work, it gives me an unhandeled exception error on the if(i.getType() == Material.Air) { line
     
  9. Offline

    Yukari

    Code:JAVA
    1.  
    2. int count = 0;
    3. for(ItemStack i : player.getInventory().getContents()) {
    4. if(i.getType() == Material.AIR) {
    5. count++;
    6. }
    7. }
    8.  

    doesn't give me any errors in eclipse. Can you post the rest of your code?
     
  10. Code:
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
     
    Player player = event.getPlayer();
    int count = 0;
    for(ItemStack i : player.getInventory().getContents()) {
        if(i.getType() == Material.AIR) {
            count++;
        }
    }
    Check your imports and ignore how the player got assigned, my demo of this code was in an event
     
  11. Offline

    Vinceguy1

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    I still get an unhandled exception on the line that it checked to see if its air or not.

    Its not an error in eclipse, its an error when executing the command
     
  12. D'oh!
    for(ItemStack i : player.getInventory().getContents()) {
    i is null when there is no item, not air (sometimes)! Hah.
    Code:
    int count = 0;
    for(ItemStack i : player.getInventory().getContents()) {
        if(i == null) {
            count++;
        } else if(i.getType() == Material.AIR) {
            count++;
        }
    }
     
  13. Offline

    Vinceguy1

    AlexLeporiday Thanks, this worked perfect.
     
  14. Offline

    Ugleh

    Empty slots are filled with air?
    Every time I get getType on an empty slot it returns null.
     
  15. Offline

    Badeye

    This post is kinda old, but still: Yes, empty slots are filled with air. to be exact, they are filled with: 0:-1
    That is why it returns null, the durability data is negative.
     
Thread Status:
Not open for further replies.

Share This Page