Armor piece doesnt grant special effects when worn in Minecraft (command Blocks)
-
In minecraft 1.19.2, I used this command to give myself the iron boots:
/give @p iron_boots{Unbreakable:1,Tags:[Special],display:{Name:'[{"text":"Heavy Boots","italic":false,"color":"green"}]'},Enchantments:[{id:vanishing_curse,lvl:1},{id:protection,lvl:2}],HideFlags:4} 1
I then used this command to check if one is wearing the boots and grant the effect but it doesnt add the effect.
execute as @a if entity @s[nbt={Inventory:[{Slot:100b,id:"minecraft:iron_boots",tag:{Tags:["Special"],display:{Name:"{\"text\":\"Heavy Boots\"}"}}}]}] run effect give @s minecraft:slowness 1 0 true
The problem is this command doesnt the effect. Although after removing the tag of the item check, that is this command.
execute as @a if entity @s[nbt={Inventory:[{Slot:100b,id:"minecraft:iron_boots"}]}] run effect give @s slowness 1 0 true
It works without a problem, on every pair of iron boots that is, so I found out that I'm probably doing something wrong with formatting.
Please can you explain what I did wrong and not only provide a solution? It would really help me in avoiding this mistake in the future. Thanks!
-
In your
/give
, you wrote this:Name: '[{"text":"Heavy Boots","italic":false,"color":"green"}]'
But in your
/execute
, you wrote this:Name: "{\"text\":\"Heavy Boots\"}"
When you compare item names, the NBT processor is doing a plain text comparison on the raw JSON source of the item name, not the rendered output of that JSON source.
In fact, the NBT processor does not even know that
display: {Name: '…'}
contains JSON text, and frankly, it does not even care.What it does care about is that
'[{"text":"Heavy Boots","italic":false,"color":"green"}]'
and"{\"text\":\"Heavy Boots\"}"
are undoubtedly different strings, even though the produced text is the same. But since the JSON text is different, the item names do not match.But wait! Before you change the item names to match, consider this:
You are already using a custom item tag in your
/give
command:give @p iron_boots{Tags:[Special]}
This is the right way to do it! Instead of testing for the custom item name, test for a custom item tag instead!
In fact, you are already doing this in your current command:
execute as @a if entity @s[nbt={Inventory:[{Slot:100b,id:"minecraft:iron_boots",tag:{Tags:["Special"],display:{Name:"{\"text\":\"Heavy Boots\"}"}}}]}]
But, in addition to having the custom tag check, you also have a custom name check there too. This defeats the purpose of a custom tag, since the custom tag is supposed to help you suppress the custom name check, but you still had one anyways.
So, to sum up what you need to do, the best way to fix your problem using good practices is only to test for the custom item tag, and not test for the custom item name.
Main article and further examples: https://gaming.stackexchange.com/q/384468/250180