This question already has an answer here:

I'm working on a current map and this is the only hitch I'm at so far. I know that I can do a testfor command with a command block to see if anyone is holding a specific item, but I also want to give the person that has that item on them, a new item. For example, if someone has a diamond on them, I want to be able to, through command blocks, find that person and give them a blaze rod. Appreciate all help!

marked as duplicate by Skylinerw, Dragonrage, aytimothy, Mathias711, Frank Mar 27 '16 at 13:08

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
    Do you want this to be like an exchange? Like, taking away the diamond block and replacing it? Or just a one-time thing that doesn't take that item away? – Unionhawk Mar 26 '16 at 19:48
  • I dont want it to take the item away, just identify that it is there and give them a new item – B-DigidyDog Mar 26 '16 at 20:10
  • The top answer displayed in that question would work, however, my question is completely different. Which is why when I searched for an answer before posting my question; I didnt come across it. – B-DigidyDog Mar 27 '16 at 12:08

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+:

If you want to be able to target players after finding them, you should use a scoreboard because testfor can only target @a, @p, @e. With a scoreboard you are not limited to testing one player or all the players.

If you use the HasItem tag then you can test if a player has the item and target them after

/scoreboard players tag @a[tag=HasItem] remove HasItem

/scoreboard players tag @a[tag=!HasItem] add HasItem {Inventory:[{id:"minecraft:dirt"}]}

Then you can target the tag. Example:

/effect @a[tag=HasItem] minecraft:speed 1 3

You could also test to see if the payer is holding the item and then target them:

The SelectedItem compound holds a copy of the player's currently-held item.

/scoreboard players tag @a[tag=HoldingItem] remove HoldingItem

/scoreboard players tag @a[tag=!HoldingItem] add HoldingItem {SelectedItem:{id:"minecraft:diamond_sword"}

Then you can target them. Example:

/kill @a[tag=HoldingItem]


/scoreboard objectives add HasItem dummy

Run on a clock in presented order.

/scoreboard players set @a HasItem 0

/scoreboard players set @a HasItem 1 {Inventory:[{id:"minecraft:dirt"}]}

The last command will set "HasItem" to 1 if the player has a dirt. You can then target the player based on their score.

/effect @a[score_HasItem_min=1] minecraft:speed 1 3

How can I target a player after detecting an item in their inventory

  • Glad to be of help. – Jason_ Mar 26 '16 at 20:37
  • So if the player has the item you are using something like /give (player) minecraft:blaze_rod? – Jason_ Mar 26 '16 at 23:09
  • If you've read my last comment, I meant to delete it; it was some weird bug which sort of cancelled itself out. Otherwise, ? – B-DigidyDog Mar 27 '16 at 1:05
  • Yeah I read it, and noticed it was deleted. (: – Jason_ Mar 27 '16 at 1:20

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