8

I would like to use command blocks to swap the contents of two specific inventory slots dynamically, while retaining the item, data value, and stack size. I know it is possible to set a slot to contain a specific item, but I am unsure as to whether or not it is possible to switch them (without a ridiculous amount of command blocks, accounting for every possible item in the game). Is this possible?

3
  • Can you detect what is in a slot and store it in a scoreboard? Spoiler: Haven't used cmds in a while
    – rivermont
    Jul 10, 2016 at 1:44
  • @rivermont-WillB. Not that I know of.
    – 2Cubed
    Jul 10, 2016 at 1:54
  • I'll go and investigate, do you have a limit on how many command blocks you are willing to use? Jun 27, 2018 at 14:49

3 Answers 3

1

Possible as of 1.14, which allows you to use the new /data modify command to copy NBT between sources.

You need to have three commands per slot: one to copy the ID, one to copy the Count, and one to copy the tag compound (which stores the item's other data). You don't want to copy the Slot tag because it'll mess things up. Instead, you'll rely on it for selecting which slots to modify.

It won't be this easy though. Coding practice says you need a third temporary location when you swap two values. So free up a third slot in the block you want to modify as a utility slot. In this case, I'm going to swap slots 0 and 1 of the chest at coordinates (12, 23, 34) with my utility slot at slot 0 of block (0, 0, 0). Make sure to place one piece of dirt in the utility slot.

data modify block 0 0 0 Items[0].id set from block 12 23 34 Items[{Slot:0b}].id
data modify block 0 0 0 Items[0].Count set from block 12 23 34 Items[{Slot:0b}].Count
data modify block 0 0 0 Items[0].tag set from block 12 23 34 Items[{Slot:0b}].tag

data modify block 12 23 34 Items[{Slot:0b}].id set from block 12 23 34 Items[{Slot:1b}].id
data modify block 12 23 34 Items[{Slot:0b}].Count set from block 12 23 34 Items[{Slot:1b}].Count
data modify block 12 23 34 Items[{Slot:0b}].tag set from block 12 23 34 Items[{Slot:1b}].tag

data modify block 12 23 34 Items[{Slot:1b}].id set from block 0 0 0 Items[0].id
data modify block 12 23 34 Items[{Slot:1b}].Count set from block 0 0 0 Items[0].Count
data modify block 12 23 34 Items[{Slot:1b}].tag set from block 0 0 0 Items[0].tag
0

I can't remember whether the 10th hotbar slot came up in 1.9 or 1.10, but if you are running one of those you can press F (or your set button) to toggle the blocks in your hand and the 10th slot.

3
  • 1
    This works, but it's not what I'm asking for. I want to do this for any two slots using a command block, as indicated by the minecraft-commands tag.
    – 2Cubed
    Jul 15, 2016 at 4:28
  • BTW, it's 1.9. I haven't played MC since the 1.9 betas, and I remember being amazed at the 10th slot and the new arrows (and shulkers). Apr 2, 2017 at 17:00
  • it was 1.9, along with shields and attack speeds. Oct 19, 2017 at 16:19
0

You can use a mod to do it. For example, hotbarswapper. It involves no command blocks just if you want to use it on a real server like Hypixel or Mineplex you will most likely be banned for "unauthorized mods." By holding Ctrl and scrolling, you can swap the bars in your inventory, up or down.

1
  • This isn't what the question is asking for. The question is asking for a way to swap slips slots on command, not when the player wants to.
    – One 2 Many
    Dec 18, 2020 at 18:45

You must log in to answer this question.

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