The bug
/data modify
and /execute store
seem to copy NBT by reference. For most NBT this has no effect, but for NBT in the tag
tag of items, it results in a link between the two, where later modifying one affects the other (other examples in MC-114104).
How to reproduce
Get an item with NBT, summon a mob, and data modify
your item into its hand:
/replaceitem entity @p weapon.mainhand diamond_sword{Damage:5}
/summon husk
/data modify entity @e[type=husk,sort=nearest,limit=1] HandItems[0] set from entity @p SelectedItem
/enchant @p sharpness
→ ❌ The husk's sword is enchanted as well.
This link will persist until either the destination or source is reloaded for any reason.
Additional test
Attached is a datapack that uses a jukebox to store a list of data, and a command block used to compare two strings. Using a temporary list, the object that contains the matching string within the jukebox's RecordItem
tag also contains an ID tag which is type int. After finding the correct object, the item's Item.tag.ID
is set from here, then to show the bug the ID of the source is changed to 42 (from 6).
Expected behavior: The item's ID tag is set to 6.
Actual behavior: The item's ID tag is set to 42.
To test, start a new world and go to 0,0 before loading the datapack. Then run /function lookup:test
Linked issues
Attachments
Comments 3


Kind of a duplicate of MC-114104, but I will leave this report open since the other one is not up to date.
Updated a bit to make it easier to reproduce
I'd like to have this as an option, intentionally copying by reference, but I don't think it should be the default. Having copy by reference by default is unintuitive IMO, having a change to the original causing a change in the copy is unexpected