6

I am on a multiplayer Minecraft realm in 1.16 and I was wondering if there was any way to set a chest's Items to a player's entire Inventory including the offhand and armor slots.

Using data modify <chest coordinates> Items set from entity <player> Inventory, I was only able to copy over the hotbar and the top two rows of a player's inventory.

I honestly don't care if it takes 50 command blocks, any response is gladly appreciated.

1 Answer 1

3

What is the problem with your trials?

The only reason your old command works is because the chest shares slot numbers with your inventory's hotbar and top two rows.

So, with your command, the item in inventory slot 0 (first hotbar) goes to chest slot 0, inventory slot 1 to chest slot 1, and so on.

Inventory Slots

A single chest uses slot numbers from 0–26, numbered in reading order starting with 0 in the top left. These share slot numbers with your inventory hotbar and the top two rows, so no wonder they are copied there! The third row, with slot numbers 27–35, is not copied, because their slot numbers are higher than the allowed slots in a chest. When you try to set them, they are automatically deleted by the system.

But what about a double chest? Shouldn't it have double the number of slots, 0–53?

Apparently, no.

A double chest takes up the width of two blocks. Although it looks like one block entity, it is really two chests joined together. This applies to how the items are stored, half the chest using up slots 0–27 of one of the two sides of the block, and the other half using slots 0–27 of the other side.
This is why, when you break a double chest filled to the brim with items, only half its items drop. The other half is still stored in the chest you didn't break.

So what can you do to solve this problem?


Solving

1.17+

Say hello to /item, taking such burdens off of our bodged workarounds. This lets us copy item slots with ease, rather than having to worry about having invalid slot numbers.

Now, it'll only take one command per slot, instead of three, and we don't need to create such a big setup.

Simply:

/item block 12 23 34 container.0 copy entity @p hotbar.0

And there you go! Remember, chest slots only go up to 27. Use the other coordinates of the double chest to get the others.

1.16−

I had this idea first of moving items to the chest first and manipulating the slot numbers so they're fixed, but no, items with invalid IDs or invalid slot numbers are immediately deleted, leaving no time to manipulate the slot numbers. So instead, we'll have to take the long road. Here is my outline:

  1. Clone a chest filled with template items to the target location.
  2. Change the IDs of dirt to the IDs of the item in the matching inventory slot. Also change the item counts to match, and change the tag compound (which contains all the item's properties) to match those from the inventory slot.
  3. Remove all the template items from the chest.

Why do we need the template items? Because we can only modify IDs, counts and other data if they already exists. We place template items into the chest so that they can be modified, because it's hard to manifest data with values set from another source.

Setup

Place down a double chest in a template location. Make sure it faces the same way you want the target location to face.

Fill it with an item you can distinguish easily. This can easily be done by changing the item's name, but to be on the safe side, I gave it a custom NBT tag so that the item can't be obtained without cheats:

/give @s dirt{udt:{Tags:["template"]}} 54

And yes, you'll need 54 of them. Fill the chest so that each item slot has one of the template items.

Command chain

This is a very verbose command chain you'll be making. It will consist of 3 command blocks to start, plus 3 times the number of slots you want to modify. To do the full inventory, it will take 126 command blocks. (41 slots in your inventory × 3tags per item to modify + 1to clone the chest) I recommend using functions to save space and make it easier to edit should a change of plans arise. If you choose to use regular command chainsnot recommended, place down an impulse command chain of 126 command blocks.

Command 1

Clone the template chest to the target location

/clone T1x T1y T1z T2x T2y T2z Rx Ry Rz

T1: Position of the left half of your chest.
T2: Position of the right half of your chest.
R: Target location. Use the northernmost or westernmost coordinates of the two-block area.

Commands 2 through 124

This is the meat of the machine: to copy the slots. For each slot, run the following commands:

/data modify block x y z Items[{Slot:0b}].id set from entity @p[tag=player2target,limit=1] Inventory[{Slot:0b}].id
/data modify block x y z Items[{Slot:0b}].Count set from entity @p[tag=player2target,limit=1] Inventory[{Slot:0b}].Count
/data modify block x y z Items[{Slot:0b}].tag set from entity @p[tag=player2target,limit=1] Inventory[{Slot:0b}].tag

This is highly customizable, and you can choose where each inventory slot is copied to. Here is how you can customize the command:

  1. To choose whether to copy the item to the bottom or top of the area, change the chest coordinates. Use the coordinates of the right half of the chest for the bottom 3 rows, or the left half for the top 3 rows.
  2. Of these rows, the slots are numbered in reading order. Replace the <NUM> in Items[{Slot:<NUM>b}] with the number of the slot to copy to, and replace the second reference Inventory[{Slot:<NUM>b}] for the slot to copy from.

Final commands

/data remove block Cx Cy Cz Items[{id:"minecraft:dirt",tag:{YOUR_CUSTOM_TAGS_HERE}}]

To remove template items. Repeat with the second half of the chest using the other coordinates.

3
  • 1
    Can you reduce the number of commands by doing a standard /data modify <chest coordinates> Items set from entity <player> Inventory for the first chest, and then only using your custom trick for the second chest?
    – pppery
    Commented Aug 8, 2020 at 4:10
  • @pppery Yes. That works, but I would rather not because the user might want to change the order of the items, so that the hotbar items are below the inventory just like in the display when pressing E.
    – One 2 Many
    Commented Aug 8, 2020 at 4:23
  • 1
    Thank you so much for taking the time to leave such a descriptive and easily understandable response! I really appreciate it and I'm glad to say that it works. I was originally trying to modify each slot with one command to no avail. I had no idea to try to copy each tag.
    – edrick
    Commented Aug 8, 2020 at 8:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .