Why is my item with enchantments not being detected?

Why is my item with enchantments not being detected? - Woman in Pink and White Floral Sunglasses

I want to test for an enchanted diamond sword when it is held in the player's hand. I tried this command:

execute if entity @p[nbt={SelectedItem:{id:minecraft:diamond_sword,tag:{Enchantments:[{id:"minecraft:sharpness",lvl:4}]}}}]

For some reason, this isn't detecting a Sharpness sword that I made using an anvil. However, when I /give myself a Sharpness sword with this command, this item is detected:

give @s minecraft:diamond_sword{Enchantments:[{id:"minecraft:sharpness",lvl:4}]}

What is differentiating my /given sword with the anvil-created sword, and how can I make the anvil-created sword be detected by the command?






Pictures about "Why is my item with enchantments not being detected?"

Why is my item with enchantments not being detected? - Young woman watching movie in headphones in messy room
Why is my item with enchantments not being detected? - Leather Samples on the Table for Selection
Why is my item with enchantments not being detected? - Unpacked boxes in middle of room





Minecraft Enchanting: You use Anvils WRONG :(




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

Images: Orhun Rüzgar ÖZ, Andrea Piacquadio, cottonbro, Ketut Subiyanto