Minecraft – How to move items from one chest to another using commands

minecraft-commandsminecraft-java-edition

I am trying to make a mailbox system and want to have the items in a chest teleported to another chest where I can pickup my 'mail'.

Best Answer

The /clone command can copy the chest and place it elsewhere:

/clone <x1> <y1> <z1> <x2> <y2> <z2> <x> <y> <z> [maskMode] [cloneMode] [TileName]

For example, if the chest you're inserting items to is at X0, Y64, Z0, and the mailbox chest is at X100, Y64, Z100:

/clone 0 64 0 0 64 0 100 64 100

Keep in mind that this requires both coordinate locations to be loaded. You can use the /spreadplayers command to forcibly load the chunks using a dummy entity such as an armor stand just before running the /clone command.

/spreadplayers <x> <z> <spreadDistance> <maxRange> <respectTeams> <player …>

/spreadplayers 0 0 0 1 false @e[type=ArmorStand,name=anchor]
/spreadplayers 100 100 0 1 false @e[type=ArmorStand,name=anchor]
/clone 0 64 0 0 64 0 100 64 100

And one other thing to note is that the contents of the mailbox will be completely replaced, so any items that were still in that chest will be deleted. A solution to that is to clone the initial chest above a hopper above the mailbox chest, such that the items will be transferred to the mailbox.

You could also use /setblock to destroy the cloned chest such that the items will be dropped instantly and do not have the risk of being deleted as well.

/setblock 100 64 100 minecraft:air 0 destroy