How to testfor player holding a a certain amount of specified item [duplicate]
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".
Best Answer
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]
Pictures about "How to testfor player holding a a certain amount of specified item [duplicate]"
![How to testfor player holding a a certain amount of specified item [duplicate] - Woman in Pink and White Floral Sunglasses How to testfor player holding a a certain amount of specified item [duplicate] - Woman in Pink and White Floral Sunglasses](/assets/images/how_to_testfor_player_holding_a_a_certain_amount_of_specified_item_duplicate_1.jpeg)
![How to testfor player holding a a certain amount of specified item [duplicate] - Man in Blue and White Crew Neck T-shirt and Blue Shorts Sitting on Green Grass How to testfor player holding a a certain amount of specified item [duplicate] - Man in Blue and White Crew Neck T-shirt and Blue Shorts Sitting on Green Grass](/assets/images/how_to_testfor_player_holding_a_a_certain_amount_of_specified_item_duplicate_2.jpeg)
![How to testfor player holding a a certain amount of specified item [duplicate] - Man in Blue Shirt Holding Soccer Ball Smiling How to testfor player holding a a certain amount of specified item [duplicate] - Man in Blue Shirt Holding Soccer Ball Smiling](/assets/images/how_to_testfor_player_holding_a_a_certain_amount_of_specified_item_duplicate_3.jpeg)
How do you test if a player is holding a certain item?
How to Enter the CommandMinecraft Bedrock - Test For Items In Inventory (Mobile/Xbox/PS4/Windows 10/Switch)
More answers regarding how to testfor player holding a a certain amount of specified item [duplicate]
Answer 2
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.
Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.
Images: cottonbro, Orhun Rüzgar ÖZ, Thirdman, Thirdman
