How do you use a command block to detect if a slot in the inventory is empty?

How do you use a command block to detect if a slot in the inventory is empty? - Side view of cute toddler girl sitting on bed barefoot and playing with colorful wooden blocks while mother using laptop in bed enjoying morning coffee

I'm trying to make a cool minigame.
How do I make it where when you throw a snowball and when your inventory slot [for the snowballs] is empty, it gives you another one.



Best Answer

You cannot do it directly with NBT detection because the slot simply does not exist when empty. What you can do instead is initially label the player as having the slot empty first, and then remove the label if they actually do have a snowball in that slot.

For example, the following would be run in the presented order, assuming a dummy objective called "NoSnowball" was created:

  1. Label them as having no snowball.

    /scoreboard players set @a NoSnowball 1
    
  2. Change their score to 0 if they actually do have a snowball in Slot 0.

    /scoreboard players set @a NoSnowball 0 {Inventory:[{id:"minecraft:snowball",Slot:0b}]}
    

Players with a resulting score of 1 did not have a snowball in slot 0:

/give @a[score_NoSnowball_min=1] minecraft:snowball

For 1.9+, you can use scoreboard tags instead of requiring an objective.

  1. Apply label by default.

    /scoreboard players tag @a[tag=!NoSnowball] add NoSnowball
    
  2. Remove the label if they do have a snowball in slot 0.

    /scoreboard players tag @a[tag=NoSnowball] remove NoSnowball {Inventory:[{id:"minecraft:snowball",Slot:0b}]}
    

Players that still have a "NoSnowball" tag will be those without snowballs in slot 0.

/give @a[tag=NoSnowball] minecraft:snowball



Pictures about "How do you use a command block to detect if a slot in the inventory is empty?"

How do you use a command block to detect if a slot in the inventory is empty? - Assorted-color House Facade in Park
How do you use a command block to detect if a slot in the inventory is empty? - Crop hacker silhouette typing on computer keyboard while hacking system
How do you use a command block to detect if a slot in the inventory is empty? - Crop mother and daughter sitting on bed while using laptop



Quick Answer about "How do you use a command block to detect if a slot in the inventory is empty?"

  • Apply label by default. /scoreboard players tag @a[tag=!NoSnowball] add NoSnowball.
  • Remove the label if they do have a snowball in slot 0. /scoreboard players tag @a[tag=NoSnowball] remove NoSnowball {Inventory:[{id:"minecraft:snowball",Slot:0b}]}


  • How do you check to see if something is in inventory in Minecraft?

    Display slotsVia the /scoreboard objectives setdisplay command (see command reference), players' scores in specific objectives can be displayed in certain 'slots' in-game. Each 'display slot' can show one objective at a time, and multiple 'display slots' may be used for the same or different objectives.

    How do you make a command block detect an item?

    If a dropper slot is empty it means the corresponding compound tag with property Slot does not exist. Use /execute unless data to check if the slot is empty: execute unless data block 12 23 34 Items[{Slot:0b}] run ... Is it possible to check multiple slots in a single command by using an "execute unless ...



    ITEM DETECTORS WITH COMMAND BLOCKS | ANY SPECIFIC ITEM (Minecraft Command Blocks)




    Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

    Images: Tatiana Syrikova, Pixabay, Anete Lusina, Tatiana Syrikova