How to test for an hold item name in Minecraft?

I'm trying to make a command block that spawns an item in front of you if you are holding an item with a specific name. I've got these two working scripts;
/testfor @p[r=10] {Inventory:[{tag:{display:{Name:"Item Name"}}}]}
/testfor @p[r=10] {SelectedItemSlot:0,Inventory:[{Slot:0b,id:"minecraft:diamond_sword"}]}
but I can't get them together. The block should only look for an item in the active slot.
Best Answer
To combine the two dataTags you need to move the tag
compound from first command into the inventory item compound in the second:
/testfor @p[r=10] {SelectedItemSlot:0,Inventory:[{Slot:0b,id:"minecraft:diamond_sword",tag:{display:{Name:"Item Name"}}}]}
Note: This will only test true if the item is in slot 0 and it is also the selected item.
You can use the SelectedItem tag instead of the SelectedItemSlot tag. This will allow you to target any player who currently has the specified item selected no matter which slot it is in:
/testfor @p[r=10] {SelectedItem:{id:"minecraft:diamond_sword",tag:{display:{Name:"Item Name"}}}}
As of 1.9 you can use scoreboard add tag command to tag the player holding the specific item.
scoreboard players tag @a add <tagName> {SelectedItem:{id:"minecraft:diamond_sword",tag:{display:{Name:"Item Name"}}}}
This allows you to target the players within another command. Lets make the players with the selected sword say hello:
/execute @a[tag=<tagName>] ~ ~ ~ say hello
You could use this tag in the item summon commands.
Then to remove the tag from all player:
scoreboard players tag @a remove <tagName>
If you want to test for any player holding any item with the correct name, simply omit the id portion of the dataTag:
/testfor @p[r=10] {SelectedItem:{tag:{display:{Name:"Item Name"}}}}
Pictures about "How to test for an hold item name in Minecraft?"



[1.17] How to Detect If a Player Is Holding a Named Item
More answers regarding how to test for an hold item name in Minecraft?
Answer 2
If you are using a data pack, for better performance, a predicate should be used to detect holding an item in the main hand.
Predicates are more efficient than /execute if entity @s[nbt=…]
or /execute if data entity @s {…}
because NBT checks on players are heavy on performance.
Predicates go in the /data/<namespace>/predicates
folder of your data pack.
A sample predicate for this purpose would look like this:
{
"condition": "minecraft:entity_properties",
"entity": "this",
"predicate": {
"equipment": {
"mainhand": {
"items": ["minecraft:diamond_sword"],
"nbt": "{CustomItemTag:1b}"
}
}
}
}
Answer 3
/testfor @p[r=10] {SelectedItem:{id:"minecraft:diamond_sword",tag:{display:{Name:"Item Name"}}}}
Would work but a better way to do it would be to use scoreboard objectives with stats ex-
/scoreboard objectives add ItemHold stat.useItem.minecraft.diamond.sword
I have tested it and it works so try that...
Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.
Images: RODNAE Productions, RODNAE Productions, RODNAE Productions, cottonbro