Minecraft – Is it possible to change villager trades

minecraft-java-edition

I'm trying to make a custom world/map that allows you to trade concrete powder. Since this is not a trade normal villagers trade, I will need to find some way to do it. Note that my goal is to change villager trades globally, not just adding a new villager with custom trades.
It might be kind of hard to understand, but for example if I want to change say, a cleric to trade concrete powder for an emerald. Can I do that?

Best Answer

There is (currently) no way to change a villager type's trades using a data pack. But you can detect the career using commands and then change the trades directly.

This example command makes all clerics trade one stone for one stone one time until it refreshes (so they become the most useful villagers ever):

/execute as @e[type=villager,nbt={Profession:2,Career:1}] run data merge entity @s {Offers:{Recipes:[{buy:{id:stone,Count:1},sell:{id:stone,Count:1},maxUses:1}]},CareerLevel:3}

This command has to be put into an always active repeating command block in the spawn chunks or into a ticking function.

Explanation:
You have to use execute as with the selector and then data merge entity @s instead of directly using the selector in /data, because for some reason that command only allows a single entity. /execute works around that.
{Profession:2,Career:1} is the NBT of clerics, so you can filter the villager type that way.
The changed NBT should be pretty self-explanatory, except for CareerLevel:3, that makes it so that the villager doesn't generate any new trades after the ones you have given him.

I used this villager summon command generator to get a /summon command for the villager with the correct trades (because I'm lazy, the wiki page (archive) with all the NBT tags would work as well), copied the NBT and removed profession and career from it, since I don't want to change those. Here (archive) are the villager profession/career IDs for matching the right type.

Related Topic