How can I swap two specific items using a command block? [duplicate]

I'm making a map where you have to craft items to destroy more items, and so on.
You need to craft this Stone Pickaxe and you are in Adventure Mode so it can't break anything. I want to swap it with another Stone Pickaxe which can actually destroy blocks. I know the /give <item>{CanDestroy:["<block>"]} 1.

Please help me swap items.


Solution 1:

Put this command in an always active repeating command block:

execute as @a[nbt={SelectedItem:{id:"minecraft:stone_pickaxe"}}] run replaceitem entity @s weapon.mainhand minecraft:stone_pickaxe{CanDestroy:["minecraft:stone"]}

This will check if the player is holding a stone pickaxe, if so, it will change it so that it is able to break stone.

Also make sure that you turn of command block output like so:

/gamerule commandBlockOutput false

This is so your chat does not get spammed