1

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.

1 Answer 1

1

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
2
  • Yes, thank you for your answer and I tried, but if I had two snowballs and pressed F,, it would be changed to one snowball. Also I can't try using making temporary storage(ex, chest) and then saving items in there, because I am trying to do this in big server.
    – Claypack
    Commented May 9, 2022 at 11:37
  • In that case you must make the step up from command blocks to data packs, which will allow you to use functions to make the commands run in the exact order you want them to. This will mean you will only ever need one storage block, no matter how many players. Do not feel discouraged; data packs are simply the next step up from command blocks—command blocks make up half the knowledge needed for data packs.
    – One 2 Many
    Commented May 9, 2022 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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