Minecraft – How to detect if a player is wearing a specific piece of armour

minecraft-commandsminecraft-java-edition

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

Best Answer

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>