How do I test for Air in Slot 9? with the /execute command?
I've came up with this so far but it doesn't work
/execute if entity @a[nbt={Inventory:[{Slot:8b,id:"minecraft:air"}]}] run give @a minecraft:stick
What did I do wrong?
minecraft-java-edition
How do I test for Air in Slot 9? with the /execute command?
I've came up with this so far but it doesn't work
/execute if entity @a[nbt={Inventory:[{Slot:8b,id:"minecraft:air"}]}] run give @a minecraft:stick
What did I do wrong?
Best Answer
The Inventory array only contains slots with items in them. If the slot is empty, it is removed from the array.
You can observe this by running
/data get entity @s Inventory
. For example, if you had an apple in the first slot of your Hotbar and you run the command, the following output would be displayed.^ as you can see, only populated slots are displayed.
You can detect an empty slot by testing if it has data. Example:
^ this command will return either 1 or 0 depending on whether or not the specified slot has any data.
Now store the output of this test into a scoreboard objective so you can actually use it:
and finally, you can now do something like
/execute if score @s <obj> matches 1 run say item detected!