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

How can I detect if a player is wearing a specific piece of armour? - A Woman Smiling and Holding Tennis Racket

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>



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

How can I detect if a player is wearing a specific piece of armour? - A Woman in White Shirt Holding a Tennis Racket
How can I detect if a player is wearing a specific piece of armour? - Chess pieces placed on chessboard square
How can I detect if a player is wearing a specific piece of armour? - Focused ethnic female player making chess move while playing game at table against gray background





How to detect when players are wearing armor Minecraft(JAVA)




More answers regarding how can I detect if a player is wearing a specific piece of armour?

Answer 2

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.

Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

Images: Artem Podrez, Artem Podrez, beytlik, emre keshavarz