2

This question already has an answer here:

Is there anyway that I can testfor a player holding 1 stick and run a command through a clock? If the player is not holding 1 stick (meaning if the player holding 2 or more sticks) the clock will not be activated.

Is there anything wrong with this command?

/testfor @p {SelectedItem:{id:minecraft:stick,Count:1,Damage:0}}

If possible, I want to testfor a stick named "Wand".

marked as duplicate by Fabian Röling, greg-449, Schism, RudolfJelin, Wondercricket May 13 at 12:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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]
0

If you want to check if the player is holding a stick named "Wand," you'd use this command:

/testfor @a {SelectedItem:{id:"minecraft:stick",tag:{display:{Name:"Wand"}}}}

You wouldn't know the player that was holding it, but you would know that somebody was.

Not the answer you're looking for? Browse other questions tagged or ask your own question.