2

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!

1
  • 2
    /testfor @p[r=10] {SelectedItem:{id:"minecraft:diamond_sword",tag:{display:{Name:"Item Name"}}}}
    – Sean
    Commented Mar 7, 2016 at 21:07

3 Answers 3

4

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]
1
  • 1
    I agree. /testfor is mostly useless. Commented Mar 9, 2016 at 1:05
1

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

0

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)

9
  • I see your point nice catch.
    – Jason_
    Commented Mar 7, 2016 at 23:45
  • @p will only select the nearest one Commented Mar 9, 2016 at 1:04
  • @sharpturn um.. duh. My point is that in command blocks, if you test for everyone you would need to assign them a scoreboard profile, but with @p, you can just effect the player who stepped on a pressure plate (if you built the command blocks in the correct positions).
    – Roke
    Commented Mar 9, 2016 at 1:18
  • 1
    'adventure map' - So using logic (cough cough) he will want the user to power it and if anything you should use @[c=1] if you are doing it globally.
    – Roke
    Commented Mar 9, 2016 at 2:32
  • 1
    @a[c=1] is essentially the same as @p with the difference of targeting dead players, and doesn't really solve anything. With the mechanics available these days, custom maps these days don't (and shouldn't) rely on lag-incuding physical redstone logic, for both the lag and the lack of multiplayer support. The OP did not say anything about only targeting a player based on a pressure plate. Ideally answers should be multiplayer-friendly and cover as many situations as possible (unless clarified by OP), which is not possible with basic /testfor commands.
    – Skylinerw
    Commented Mar 9, 2016 at 3:01

Not the answer you're looking for? Browse other questions tagged .