Trading Behavior
Making an entity a trader is accomplished via minecraft:trade_table
or minecraft:economy_trade_table
components. Both of them will open a trading UI from the given path, but the economy trades component has some more options referring to some Village and Pillage trading mechanics. Other AI goals you'll need are minecraft:behavior.trade_with_player
, optionally minecraft.behavior:trade_interest
(allows the mob to hold/offer an item) and, potentially, "minecraft:trade_resupply": {}
.
For a simple trading UI, trade_table
+ trade_with_player
components should do the trick.
- Add
"minecraft:behavior.trade_with_player": {}
to your entity's components. - Copy the following code into a component group of your entity. I'll call mine
"wiki:trader"
;
"minecraft:trade_table": {
"display_name": "Trading Entity", // Text to be displayed.
"table": "trading/trading_entity_trades.json", // Path to the trade table file
"new_screen": true //If set to false, the UI will display as the pre-Village&Pillage one.
}
- Now make sure the component group is added to the entity via an event. It's a good idea to add it in
minecraft:entity_spawned
event, since it triggers on spawn. If you don't feel confident with events and component groups, make sure you're familiar with entity definition rules/concepts. See the Intro to Entities.
WARNING
If you add the component in components, it will cause all kinds of problems, including blank trading UIs for all entities in the world. Because of an issue with the trading AI goals, they must be added in component groups.