Use
/blockdata x y z {Items:[0:{id:minecraft:wooden_button,tag:{CanPlaceOn:["minecraft:stonebrick"]},Count:1}]}
Which can add to an already filled chest. Replace x y z
with the coordinates.
The /blockdata
command changes, well, the data of a block (go figure!) Basically, I change the data of a chest to include a wooden button with the tag CanPlaceOn
. Pretty simple, really.
MC-131346 was fixed, so the new answer to this question is: It just works. /give @s water_bucket{CanPlaceOn:["stone"]}
gives you the proper water bucket that can only be placed on stone.
Here is the old answer for reference, in case you're playing in an older version:
As OnePointZero said, usually CanPlaceOn
should just work for buckets, but due to MC-131346 it doesn't. But there's an easier way to work around it:
Give the player a retextured item that he doesn't usually have anywhere else in your map and that also doesn't appear as a block in it. One idea would be a structure void or, if the item remodeling is too difficult, a sapling, bed, rail or other placeable item with a different texture than the block, as long as it's not anywhere else in the map.
That item has to have the CanPlaceOn
tag, here demonstrated with a stone:
/give @s stone{CanPlaceOn:["sandstone"]}
Then you track the usage of this item, like this:
/scoreboard objectives add placedStone minecraft.used:minecraft.stone
And whenever someone places it, you replace the block in the world with water:
/execute at @a[scores={placedStone=1..}] run fill ~-7 ~-7 ~-7 ~7 ~7 ~7 water replace stone
Luckily /fill
already updates all blocks consistently, so it will begin to flow automatically and you don't have to bother with the inconsistent updates of /setblock
.
Now you just reset the scoreboard for later use:
/scoreboard players set @a[scores={placedStone=1..}] placedStone 0
The last two commands go into repeating command chain or a function. The second command block can be conditional.
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:This variant of the command might potentially cause slightly less lag, I haven't tested it:
It only applies the change on items that were created 1 game tick ago.