Minecraft – Store a players third inventory row to a shulker box (1.14.4)

minecraft-commandsminecraft-java-edition

I am currently trying to store the entirety of a players inventory into four skulker boxes. Three for the players hotbar, armor, and offhand, and the other for the players main inventory.

I currently know that you can store the players top two rows of the inventory as well as the hotbar using this command:

/data modify block ~ ~-1 ~ Items set from entity @s Inventory

This is helpful for the hotbar and the top two rows of a players inventory, however I don't know how to store the third row of the players inventory using commands.

I have tried to use the following command for slots 27-35, however it just leaves the slots in the skulker box empty.

/data modify block ~ ~-1 ~ Items[{Slot:0b}] set from entity @s Inventory[{Slot:27b}]

Does anyone know how to do this?

Best Answer

That was a tricky one. What happens is that you copy the entire item NBT, so even the slot number. That way the item ends up in a slot that doesn't exist and gets deleted. There is no error message for this and if you try to copy into Items[0] instead, it even says "Nothing changed. The specified properties already have these values", even if the shulker box is empty, which is very confusing (and probably a bug).

The workaround: Copy the id, Count and tag tags individually:

/execute if data entity @s Inventory[{Slot:27b}] run replaceitem block ~ ~-1 ~ container.0 stone
/execute if data entity @s Inventory[{Slot:27b}] run data modify block ~ ~-1 ~ Items[{Slot:0b}].id set from entity @s Inventory[{Slot:27b}].id
/execute if data entity @s Inventory[{Slot:27b}] run data modify block ~ ~-1 ~ Items[{Slot:0b}].Count set from entity @s Inventory[{Slot:27b}].Count
/execute if data entity @s Inventory[{Slot:27b}] run data modify block ~ ~-1 ~ Items[{Slot:0b}].tag set from entity @s Inventory[{Slot:27b}].tag

(repeat for 28 to 36 and 1 to 8)

The /execute if part of the second to fourth command are not strictly needed, but avoiding error messages is always nice and I think it also improves performance, especially in a function.