Minecraft – Copy player’s armor and offhand items into a chest

minecraft-commandsminecraft-java-edition

I have tried to copy all items from a player's inventory into a chest in 1.16.
I have used this command for the hot bar and the 2 first rows in the inventory:

/data modify block 227 76 77 Items set from entity @a[tag=inbuild,limit=1] Inventory

For the 3rd row I have used these commands:

/summon minecraft:item ~ 69.00 85.46 {Item:{id:"minecraft:stone",Count:1b},Age:5990s}
execute store success score itemdetect CopyInv run data modify entity @e[y=69.00,z=85.46,distance=0..1,type=item,limit=1,sort=nearest] Item set from entity @a[tag=inbuild,limit=1] Inventory[{Slot:35b}]
execute if score itemdetect CopyInv matches 1 run data modify block 227 76 79 Items[{Slot:8b}] merge from entity @e[y=69.00,z=85.46,distance=0..1,type=item,limit=1,sort=nearest] Item

It works, but now I need to do the same with the armor slots and the second hand but i cant get it to work 🙁 Do I need to do something else to get the armor and the second hand to work, or am I just using the wrong slot IDs? For the armor i use these ids: 100, 101, 102 and 103, and for the second hand i use this -106. I can have missed something else when I did it, but I have done it 3 times and I never got it to work!

I'm playing in 1.16 (pre-release 5)

Best Answer

The reason why this happens is that the Slot tag also gets copied, so the item gets copied into the chest and moved to slot 100, -106, etc., which makes it disappear again immediately.

Since there's nothing else with that many slots and you can't directly modify player data, you'll have to either copy id, Count and tag independently or go through something that doesn't care about the Slot tag, which can for example be an item entity (which automatically removes it, but that would place the item at slot #0 when copying it into the chest) or an arbitrary-NBT place like storage, where you can edit the slot number yourself.

The first method:

/data modify block ~ ~ ~ Items[{Slot:8b}] merge value {id:"stone",Count:1}
/data modify block ~ ~ ~ Items[{Slot:8b}].id set from entity @s Inventory[{Slot:-106b}].id
/data modify block ~ ~ ~ Items[{Slot:8b}].Count set from entity @s Inventory[{Slot:-106b}].Count
/data modify block ~ ~ ~ Items[{Slot:8b}].tag set from entity @s Inventory[{Slot:-106b}].tag

The second method:

/data modify storage fabian:copy_help temp set from entity @s Inventory[{Slot:-106b}]
/data modify storage fabian:copy_help temp.Slot set value 8
/data modify block ~ ~ ~ Items append from storage fabian:copy_help temp

The parameter "append" puts the new entry at the end of the list, it can even become a 28th entry, but the list actually gets automatically reordered to be in the expected order and any item that was in that slot previously gets overwritten.