How to testfor player holding a a certain amount of specified item [duplicate]
Solution 1:
When checking pre-existing data, you must specify data as it's saved. Count
is saved as a byte, so you append its numerical value with a "b". Damage
is saved as a short, so you append its numerical value with an "s". Sticks themselves do not have any other damage values besides 0, so you can exclude that tag to simplify the command.
/testfor @p {SelectedItem:{id:"minecraft:stick",Count:1b,tag:{display:{Name:"Wand"}}}}
Keep in mind though that you're only checking the data for the player closest to the command block. If the closest player doesn't match, the command will fail even if there could've been another player who matched.
You would need to use @a
instead of @p
to check all players. But even then you won't know who matched, just that somebody did. If you need to target that player afterwards, you should not be using /testfor
and instead assign a label using /scoreboard
.
For example, the following will provide players with a "holdingStick" label if they were holding the stick:
/scoreboard players tag @a[tag=holdingStick] remove holdingStick
/scoreboard players tag @a add holdingStick {SelectedItem:{id:"minecraft:stick",Count:1b,tag:{display:{Name:"Wand"}}}}
And you can then target players with that tag:
/say Players holding only 1 stick: @a[tag=holdingStick]