Minecraft – how to testfor a player that selected an item

minecraft-commandsminecraft-java-edition

How to use the scoreboard command to testfor for a player that selected a snowball, namely "Blast" in Minecraft 1.9?

Best Answer

The SelectedItem compound holds a copy of the player's currently-held item (minus the Slot tag). All item data that is not the root id, Damage, Count, or Slot, must be placed within a single tag compound.

/scoreboard players set @a OBJ 0
/scoreboard players set @a OBJ 1 {SelectedItem:{id:"minecraft:snowball",tag:{display:{Name:"Blast"}}}}

/say Players holding the item: @a[score_OBJ_min=1]

As of 1.9 you will want to use "tags" instead of scores when labeling the player, which is more efficient and does not require an objective:

/scoreboard players tag @a[tag=holding] remove holding
/scoreboard players tag @a add holding {SelectedItem:{id:"minecraft:snowball",tag:{display:{Name:"Blast"}}}}

/say Players holding the item: @a[tag=holding]
Related Topic