I can't modify a shulker box's items correctly

So I ran into a possible bug with Minecraft 1.16.4. I used the data command to modify the items of a shulker box to whatever helmet I'm wearing. The command is the following.

data modify block 0 0 0 Items[0] set from entity @s Inventory[{Slot:103b}]

I am running the command manually. It says the data is already the same. My syntax is correct, but it just doesn't work. The shulker box doesn't have the Items tag until I put an item in the container. If I put an item in the shulker box and run the command, it just starts removing items out one by one and it doesn't give me an error. I have in the past had a problem with the world I was using so I created a fresh world and it didn't solve anything. Any help? Is anyone else having the same problem? Any workarounds? Please ask questions.


Solution 1:

That behaviour is no bug; it is by design.

The command in question will also copy the slot number to the shulker box, causing the shulker box's data to look like this:

{Items:[{Slot:103b,id:"minecraft:dirt",tag:{...}}]}

And 103 is an invalid slot number for shulker boxes. So what happens?

It gets deleted, automatically, and immediately.

To workaround this, use three /data commands: one to copy the item ID, one to copy the Count tag, and one to copy the tag compound. Basically, copy everything except the slot number.

data modify block 0 0 0 Items[0]id set from entity @s Inventory[{Slot:103b}].id
data modify block 0 0 0 Items[0].Count set from entity @s Inventory[{Slot:103b}].Count
data modify block 0 0 0 Items[0].tag set from entity @s Inventory[{Slot:103b}].tag