/data modify command not working [duplicate]
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}
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