You can append
an enchantment to the item using the /data modify
command, but you would need to throw the item outside your inventory or put it inside a container block, since it's not possible to modify player data. (There is a work-around, but it's rather complicated) Here's are some examples:
For targeting the item entity:
data modify entity <targetEntity> Item.tag.Enchantments append value {id: "minecraft:mending", lvl: 1s}
<targetEntity>
refers to the item entity that we'll be modifying the NBT of.
Item.tag.Enchantments
is the NBT path for the enchantments of an item. We'll be appending an object in this list.
append value
indicates that the data after will be appended to the target NBT path, which in this case, is Item.tag.Enchantments
{id: "minecraft:mending", lvl: 1s}
is the mending enchantment we'll be appending to the Item.tag.Enchantments
list
For targeting a container block that contains the item:
data modify block x y z Items[].tag.Enchantments append value {id: "minecraft:mending", lvl: 1s}
x
, y
, and z
refers to the container block's coordinates. In this example, let's say the block is a chest.
Items[].tag.Enchantments
is the NBT path to the item that's stored in the chest. Items[]
refers to the chest's NBT, which is used for storing items. You can select a certain index by adding a number inside []
.
e.g: Items[0].tag.Enchantments
would target the item in the first slot of the chest, like Items[-1].tag.Enchantments
would target the item in the last slot of the chest.