Is it possible to mark or modify a player with a specific item in their inventory
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.