Is it possible to mark or modify a player with a specific item in their inventory

I'm building a CTF map of sorts, using command blocks in vanilla 1.8. I've discovered two commands for detecting if an item is in a players inventory:

/clear @a[team=Red] banner 4 0

/testfor @a[team=Red] {Inventory:[{id:"minecraft:banner"}]}

Those work fine, unless you want to perform a command ON the player carrying that item. For example: I want to cause the player who picks up a banner of the opposite team, to automatically equip that banner in their head slot with a command similar to this:

/replaceitem entity @a[???????] slot.armor.head banner 1 4

Can this be done? If not, is there a way to mark a specific player carrying an item using scoreboards?


As of 1.13, doing this no longer needs a scoreboard and can be done in one command block:

/execute as @a[nbt={Inventory:[{id:"blue_banner"}]}] run ...

@ModDL had the right idea, although his commands error in game.

Here are the commands I ultimately used to mark players with the flag, and perform actions on them:

To set up the tracking, run these once at the start:

/scoreboard objectives add hasBlueFlag dummy
/scoreboard players set @a[team=Red] hasBlueFlag 0

To detect and mark who has the flag, run this on a clock:

/scoreboard players set @a[team=Red,score_hasBlueFlag=0] hasBlueFlag 1 {Inventory:[{id:"minecraft:banner",Damage:4s}]}

To reset who has the flag, run this command on a clock:

/testfor @a[score_hasBlueFlag=1] {Inventory:[{id:"minecraft:banner",Damage:4s}]}

and, invert the output into a command block with this:

/scoreboard players set @a[score_hasBlueFlag=1] hasBlueFlag 0

To perform an action on the flag carrier, you can just fire something like this off, whenever you'd like. The following commands replace the flag carriers helmet slot with the flag:

/clear @p[score_hasBlueFlag=1] banner 4 -1
/replaceitem entity @p[score_hasBlueFlag=1] slot.armor.head banner 1 4

Notes:

  • These commands are testing and running in 1.8.1
  • In the above commands, you'll notice Damage:4s, this is actually the base banner color (Blue). Read more about the Damage NBT property here.
  • More banner detecting options can be found here

First, you need to set up a dummy scoreboard objective:

scoreboard objectives add hasFlag dummy

Now, create a 20 Hz. clock, such as a fill clock or use minecraft 1.9's repeat and chain command blocks to run the following commands in order:

scoreboard players set @a hasFlag 0
scoreboard players set @a hasFlag 1 {Inventory:[{id:"minecraft:banner",Damage:4s}]}

This ensures that the flag-bearer (i.e. the player with a blue banner) has a hasFlag score of 1, and it's 0 for everyone else. Note that you can modify the data tag in the second command to your liking, using any of the NBT tags available for players.

Now, we can do whatever we want with that scoreboard, by using the score_hasFlag_min=1 target selector argument to target only flag-bearers, or score_hasFlag=0 to target everyone else. These commands have to be run on the same clock, after the above commands. For example:

replaceitem entity @a[score_hasFlag_min=1] slot.armor.head minecraft:banner 1 4 
replaceitem entity @a[score_hasFlag=0] slot.armor.head minecraft:air 1

Note: In 1.9, you can opt for using scoreboard tags instead of a dummy objective.


The command to put it on a players head is:
/replaceitem entity slot.armor.head minecraft:banner [amount] [datatag]
and that would place the banner in the helmet position, but the banner will be sticking out of the back of the head which doesn't look all that good but it still works and you can always try using wool if you prefer that. Also the datatag is that color of the flag whichever number in place of that will determine the color of the banner and you have to remember that you have to have an amount if you have a datatag.

Datatags:
Black = 0
Red = 1
Green = 2
Brown = 3
Blue = 4
Purple = 5
Cyan = 6
Light Grey = 7
Grey = 8
Pink = 9
Lime = 10
Yellow = 11
Light Blue = 12
Magenta = 13
Orange = 14
White = 15