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?
Code:JAVA int i = 0;for(Slot s : Inventory.getSlots()) {// psuedocode, I don't know the methods off the top of my headif(s.getType() == Material.AIR) {i++;}}
Here, got the actually code for you: Code:JAVA int count = 0;for(ItemStack i : player.getInventory().getContents()) {if(i.getType() == Material.AIR) {count++;}} Alternatively, there is a chance that .getContents() will not contain the slots that are empty, if so, do this: Code:JAVA int count = 0;for(ItemStack i : player.getInventory().getContents()) {count++;}count = player.getInventory().getSize() - count; AlexLeporiday I think he's just asking how to count the number of empty slots lol
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"
Doesn't work, it gives me an unhandeled exception error on the if(i.getType() == Material.Air) { line
Code:JAVA int count = 0;for(ItemStack i : player.getInventory().getContents()) {if(i.getType() == Material.AIR) {count++;}} doesn't give me any errors in eclipse. Can you post the rest of your code?
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
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
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++; } }
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.