How can I detect if a player is wearing a specific piece of armour?

How can I detect if a player is wearing a piece of armour?

Let's say it was leather and named bob with a single lapis dye added to it. what would I do? I can't use comparators and such, only command blocks - it will have a constant clock attached


Since 1.13 you can do the check and do something based on it in one command. This for example does something as the closest player who wears elytra:

/execute as @p[nbt={Inventory:[{id:"minecraft:elytra",Slot:102b}]}] run <command>

Slot 102 is the chestplate slot, 103 is the head, 101 the legs and 100 the feet.

This for example does something only if a player with a Protection 4 diamond helmet exists somewhere in the world, but still in the same position that the command is executed at and as the same executor:

/execute if @p[nbt={Inventory:[{id:"minecraft:diamond_helmet",Slot:103b,tag:{Enchantments:[{id:"minecraft:protection",lvl:4s}]}}]}] run <command>

Use scoreboard partial nbt test

Setup:

/scoreboard objectives add armor dummy

Clock:

/scoreboard players set @a armor 0
/scoreboard players set @a armor 1 {Inventory:[{Slot:103,id:"leather_helmet", tag:{color:255}}]}
<Run command with selector score_armor_min=1>

The slot is 100 for boot, 101 for leg, 102 for chest, 103 for helm. The color is calculated as Red << 16 + Green << 8 + Blue.