Minecraft – Replace normal block with same block that has a special property

minecraft-commandsminecraft-java-edition

I want to use a command block that detects if there is a gold block in the player's inventory and replace it with a gold block that can only be placed on grass.

How it looks

Now, here is what I'm doing: I started a redstone clock which connects to a command block which has this command to clear all gold blocks.

clear @p gold_block

Then I have a comparator that takes that signal and points into another command block that gives the player a gold block that can only be placed on grass.

give @p gold_block 1 0 {CanPlaceOn:[grass]}

What happens is that this special gold block also gets removed by the previous command block. I've tried to give the gold block a name with the display tag, but I don't know how to configure the command to give the block both tags (Display & Can PlaceOn).

Best Answer

Try to give to the first gold block a name:

/give @p gold_block 1 0 {display:{Name:"a name"}}

And then run the /clear command like this:

/clear @p gold_block 0 1 {display:{Name:"a name"}}

This command will clear only the golden block that has the name "a name".

Now give your SPECIAL BLOCK, but with another name:

/give @p gold_block 1 0 {CanPlaceOn:[grass],display:{Name:"SPECIAL BLOCK"}}

You need to put another name that the clear command block doesn't affect.