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

minecraft-commandsminecraft-java-edition

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?

Best Answer

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 ...