How can i summon a villager with a trade that i can only place down on certain blocks and break certain blocks?

I have seen maps with villagers with custom trades. The item that can be traded have qualities that make it so that it can only be placed down on certain blocks or break certain blocks and I want to know how to do this.


Here is the command, assuming you are using Java Edition. You must use a command block to execute this command.

/summon minecraft:villager ~ ~ ~ {VillagerData:{type:plains,profession:farmer,level:99},Offers:{Recipes:[{maxUses:999999,priceMultiplier:0,demand:1,specialPrice:0,buy:{id:dirt},sell:(What you want)}}]}}

Replace the (What you want) with the item and its NBT that you want. Here are some examples:

minecraft:stone{HideFlags:16,CanPlaceOn:["minecraft:grass_block"]} 1
minecraft:wooden_pickaxe{HideFlags:8,CanDestroy:["minecraft:stone"]} 1

This website may also help with your efforts.

For the full command, here it is:

/summon minecraft:villager ~ ~ ~ {VillagerData:{type:plains,profession:farmer,level:99},Offers:{Recipes:[{maxUses:999999,priceMultiplier:0,demand:1,buy:{id:dirt,Count:1},sell:{id:stone_button,Count:1,tag:{HideFlags:16,CanPlaceOn:["minecraft:grass_block"]}}}]}}

I hope this helps.