3

I'm wondering about whether it is possible to change part of an NBT or add NBT to an item that a player is holding. My goal is to make custom enchants. For example when a player uses an item to enchant a sword they get the score "EnchantMight" 5, which would cause the next weapon they hold to get "Might V". This, in reality, would just be +5 additive damage in the NBT. I know /replaceitem would be an option, but that only works if the new item is predetermined.

I hoped

/data modify entity @p {SelectedItem:[{AttributeModifiers:[{AttributeName:"generic.attack_damage",Amount:5,Operation:0,UUID:[I;-120919,1237,151734,-2474],Name:"generic.attack_damage"}]}]} insert/append/merge

would work, but obviously it didn't.

If anyone even just has an idea I can try out, it'd be of help. If you give a datapack related answer, please explain clearly as I don't know a lot about it.

0
3

1.16−

An interesting workaround to change NBT of a custom item is to copy it to a chest, modify it in the chest (where it can be modified) and then use /loot in replace mode to replace the item slot.

Note: Once 1.17 fully comes out, you can replace this method with the /item command.

Outside preparation

You'll need a special loot table to make this happen. Click this link to access a data pack with the loot table that you'll need.

This loot table will cause a yellow shulker box to drop its contents instead of itself when broken with a tool with the custom tag {drop_contents:1b}.

Minecraft preparation

You'll need to first place down a yellow shulker box, and place a temporary item (like a piece of dirt) in slot 0 (upper-left corner).

Important: Ensure the shulker box is otherwise empty, and always write to it with the NBT path Items[0].

The standard location for this shulker box is (-30000000, 0, 1602).

Commands

The first step is to copy the item to the shulker box:

data modify block (x) (y) (z) Items[0] merge from entity (selector) SelectedItem

(Note, if your copy source includes the Slot tag, you'll need to use three separate commands to copy ID, Count, and tag separately.)

The second step is to modify that item, use whatever commands you want here to reference the shulker box. Here are some sample commands. Try these ones out for your first try. These commands will set the Sharpness enchantment level to the scoreboard value you specify.

data modify block (x) (y) (z) Items[0] merge value {tag:{Enchantments:[{id:"minecraft:sharpness", lvl:1s}]}}
execute store result block (x) (y) (z) Items[0].tag.Enchantments[0].lvl short 1.0 run scoreboard players get FakePlayer myObjective

Once the modification is complete, here's the magic: use /loot to replace the item in the specified slot.

loot replace entity (selector) weapon.mainhand 1 mine block (x) (y) (z) minecraft:air{drop_contents:1b}
0
0

1.17+ Update

As of 1.17, a loot table is no longer required, but the shulker box still is.

Preparation

You'll need to first place down a yellow shulker box, and place a temporary item (like a piece of dirt) in slot 0 (upper-left corner).

Important: Ensure the shulker box is otherwise empty, and always write to it with the NBT path Items[0].

The standard location for this shulker box is (-30000000, 0, 1602).

Commands

With 1.17 out, you can use /item to copy the item to the shulker box.

item replace block -30000000 0 1602 container.0 from entity @s weapon.mainhand

Then, you can modify your item as much as you want inside the shulker box using any commands you like. For example:

data modify block (x) (y) (z) Items[0] merge value {tag:{Enchantments:[{id:"minecraft:sharpness", lvl:1s}]}}
execute store result block (x) (y) (z) Items[0].tag.Enchantments[0].lvl short 1.0 run scoreboard players get FakePlayer myObjective

Finally, replace the item with /item:

item replace entity @s weapon.mainhand from block -30000000 0 1602 container.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.