How can I swap between offhand and mainhand with commands?



  • Of course I know that if I press F key, offhand and mainhand's item swaps. But I want to do this with command.
    I tried:

    execute as @a[nbt={Inventory:[{id:"minecraft:snowball",Slot:-106b}]}] at @s run tag @s add snowball
    execute as @a[tag=snowball] at @s run item replace entity @s weapon.mainhand from entity @s weapon.offhand
    execute as @a[tag=snowball] at @s run item replace entity @s weapon.offhand from entity @s weapon.mainhand
    

    But I don't think this is possible.



  • The reason this isn't working is because your third command is using the new item in the mainhand, which just got replaced with the snowball by the second command. So both items will end up becoming snowballs.

    But it looks like you're testing for a snowball in the offhand and immediately replacing it. If you know that the offhand will always be a snowball when you do the replacement, that is very simple:

    execute as @a[tag=snowball] run item replace entity @s weapon.offhand from entity @s weapon.mainhand
    execute as @a[tag=snowball] run item replace entity @s weapon.mainhand with minecraft:snowball
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2