Minecraft Commands – How to Keep NBT Data of Placeable Items

minecraft-commandsminecraft-java-edition

So I'm making a map in adventure mode, and there's 2 items: Redstone (which can be placed only on specific block) and redstone wrench (which can break redstone), but the problem is when I break redstone with redstone wrench, I get regular redstone (without CanPlaceOn nbt tag) and I can't reuse it. Can I fix this issue? If yes how?

*redstone wrench is just any item

Best Answer

Since the item of a broken block always has a slight delay before it can be picked up, you can use that to give it the CanPlaceOn tag:

execute as @e[type=item,nbt={Item:{id:"minecraft:redstone"}}] run data modify entity @s Item.tag.CanPlaceOn set value ["stone","dirt"]

This variant of the command might potentially cause slightly less lag, I haven't tested it:

execute as @e[type=item,nbt={Age:1,Item:{id:"minecraft:redstone"}}] run data modify entity @s Item.tag.CanPlaceOn set value ["stone","dirt"]

It only applies the change on items that were created 1 game tick ago.