How do i testfor a item a player is holding? [duplicate]

How do i testfor a item a player is holding? [duplicate] - Free stock photo of activity, athlete, boy

I am trying to testfor a player holding a diamond sword named Notch's Legendary Sword. How can I do that? Who ever gets the best answer will be able to get the first look at my NEW adventure map!



Best Answer

The SelectedItem compound holds a copy of the player's currently-held item. If you need to target the player after finding them, you should not use /testfor because that does not mark matching players for you to target afterwards.

You should instead use a command such as /scoreboard to assign a label, either a score in pre-1.9 or a "tag" in 1.9+:

/scoreboard players tag @a[tag=HoldingItem] remove HoldingItem
/scoreboard players tag @a[tag=!HoldingItem] add HoldingItem {SelectedItem:{id:"minecraft:diamond_sword",tag:{display:{Name:"Notch's Legendary Sword"}}}}

You would then be able to target the specific players holding the item:

/say @a[tag=HoldingItem]



Pictures about "How do i testfor a item a player is holding? [duplicate]"

How do i testfor a item a player is holding? [duplicate] - Man Holding a Ball Beside a Net
How do i testfor a item a player is holding? [duplicate] - Woman Holding a Ball Beside The Volleyball Net
How do i testfor a item a player is holding? [duplicate] - Young Man Standing by Volleyball Net with Ball in Hands





[1.17] How to Detect If a Player Is Holding a Named Item




More answers regarding how do i testfor a item a player is holding? [duplicate]

Answer 2

If it has a custom name you may not need to test for the diamond sword just the custom name: You could use a command like this: /testfor @p[r=1000] {SelectedItemSlot:0,Inventory:[{Slot:0b,­tag:{display:{Name:"Notch's Legendary Sword"}}}]} This will testfor the the player holding the custom named item in slot 0. You may choose to have the radius if you would like.

If you wish to know more watch this video on testfor:

By Dragnoz

This is a picture of the numbered slots you can use for reference when testing for an item in that slot:enter image description here

Answer 3

As @Sean said, you can type:

/testfor @a {SelectedItem:{id:"minecraft:diamond_sword",tag:{display:{Name:"Notch's Legendary Sword"}}}}

This will test for all (@a) players HOLDING a diamond sword (`minecraft:diamond_sword) named Notch's Legendary Sword

@Jason_ 's command only works in command blocks. If you choose to use the command in command blocks, I suggest using @p as well.

If you want to do this globally I would suggest @a[c=1] (as in 1 command block to rule them all)

Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

Images: Kindel Media, cottonbro, Kampus Production, RODNAE Productions