How do you use commands to move items inside inventories from one slot to another?

Using /replaceitem can only delete items and re-create them with fixed properties.
Using /data merge block overwrites the item completely.
There is also no way to get all of an item's NBT into scoreboards or similar.


I'll start with the simplest example, a chest created with the command:

/setblock ~ ~ ~ chest{Items:[{Slot:0,id:"stone",Count:1}]}

That command places a chest at your position that contains 1 stone in the first slot (slot numbers start with 0).

First create a scoreboard to hold the target slot number. Not only is it required, it also gives the additional bonus that you can make the target slot depend on something without needing more commands.

/scoreboard objectives add slot dummy

Set the scoreboard to the target slot number, this uses the executing player as score holder, but it might as well be a fake player (just enter a name instead of @s) or any other selector, just use the same selector later for the movement.

/scoreboard players set @s slot 3

Now the actual movement: You just have to change the slot number of the item in the chest using /execute store. That command allows defining a path and only writing to that path while keeping everything around it unchanged.

/execute store result block ~ ~ ~ Items[0].Slot byte 1 run scoreboard players get @s slot

Note that MC-123307 doesn't apply here, so you cannot use this for player inventories.

And here's an even bigger catch: You have to specify the current index of the item to be moved. Not the slot number, the index. If you for example have items in slot 0, 1, 2, 3 and 15, but nothing in 4-14, then the items in slot number 15 have the slot index 4, not 15, since there are no entries in the NBT array for empty slots.
So moving from slot 0 is the only safe one to do, all other ones would require you to check exactly which slots before your chosen slot are full and which ones empty. It might even be possible for the slots to be in the wrong order in the NBT (for example if you moved them backwards with this method), in which case you would have to check the slot number for all items in the chest and do some Maths on them.
It's not impossible, it's just much more complicated.

Special cases:

  • Moving items onto a slot that already contains items deletes the old items.
  • Moving items to negative slot numbers or other non-existent slots (e.g. too high) deletes them.

There is a new method in 1.14 and it does not have the downside of having to know which slots exist and which don't, to access the correct array indices. This method works completely with slot numbers (and one array index of a temporary block under full control of the commands).

Let's say you want to move a stack from the fifth to the ninth slot of a chest at the coordinates 12 34 56. There might also be other stacks in other slots (for example half a stack of dirt in the last slot and a single diamond in the first slot), but your commands don't need to know that, they work regardless.

What you need is a temporary chest (or anything else with at least 9 slots) where you can move the items while knowing its array index. So you set this chest:

setblock 0 0 0 chest

Now you can copy the stack you want over from the original chest to the temporary chest:

data modify block 0 0 0 Items append from block 12 34 56 Items[{Slot:4b}]

The result: The temporary chest contains 64 stone blocks in the fifth slot, just like the original chest. But the temporary chest has no other items in it, so you know that the only slot will have the index 0.
Now you can easily move it over, either with the trick from my other answer, or, even easier:

data modify block 0 0 0 Items[0].Slot set value 8

Now you have the stack in the right slot, just in the wrong chest. So you can copy it back:

data modify block 12 34 56 Items append from block 0 0 0 Items[0]

Here you could have used the same Items[{Slot:8b}] notation, but it's not needed and would cost slightly more performance.
Then you can delete the stack in its original location:

data remove block 12 34 56 Items[{Slot:4b}]

And finally you can empty or delete the temporary chest, if you want:

data remove block 0 0 0 Items
setblock 0 0 0 air

I've found a way involving chests!

All you need to do is have a chest near a command block (or somewhere easy to target with commands). You'll need one command block and chest for every slot you want it to work on. In the command block(s), type:

/data modify block ~ ~1 ~ Items[] set from entity @s Inventory[0]

Add 1 to Inventory[0] every time you want to do another slot. To 'recall' the slot, simply /fill the area where your chests are with chests (using the destroy option) and /tp the items to the player.

This may work with an entire chest, but the way I found only works for one slot at a time.

  • This is the new 1.14 syntax, I already thought that something like this would probably be possible. Don't you need a slot number in the first Items[]? – Fabian Röling Oct 30 '18 at 20:18
  • Actually, this does not work, even with numbers. I only tried it now, forgot to do that before. You're probably trying to move items from the player to a chest, which is interesting, but not what the question is about. Also, you still have to move the item to a different slot if you don't want it in the exact same slot. – Fabian Röling Jan 17 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.