3

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.

2

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.
| improve this answer | |
1

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 uses the new NBT Path syntax that can select matching entries in lists. It's also just one single command now.

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.

This is the command:

/data modify block 12 34 56 Items[{Slot:4b}].Slot set value 8

(Note that slot numbers start with 0, so everything is one lower.)

If there is already an item in the ninth slot, this overwrites it, just like in the other answer. And if you enter a number higher than the maximum slot number (for example 5 for hoppers, which only have 0 to 4), the moved stack gets deleted.

If you want to use variable indices, the other answer is still preferable. You could also make it not rely on which slots are filled by combining it with my dynamically sized array answer.

| improve this answer | |
0

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.

| improve this answer | |
  • 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 '19 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.