Skip to content

Trade Factories

Drex edited this page Jun 29, 2024 · 8 revisions

Important

This page is no longer relevant, as of version 4.0!

Trade Factories

Trade Factories are used in Minecraft's internal code, to determine what trades a villager should have. There are currently ten Trade Factories in vanilla, which are used to create all vanilla trades. This article will explain how these and our custom Trade Factories, which are a lot more powerful work, and how you can use them to create your own custom trades.

Custom Trade Factories

Behaviour pack Factory

The behavior trade is a custom trade factory of VillagerConfig, based on the syntax of bedrock behavior packs.

Note: To be compatible with bedrocks trade packs, this factory doesn't need to provide a type entry.

Parameter Type Description Mandatory
wants TradeItem[] An array of TradeItems, specifies what the player needs to pay. The array must contain 1-2 entries. Yes
gives TradeItem[] An array of TradeItems, specifies what the player gets. The array must contain exactly 1 entry. Yes
trader_exp NumberProvider How much the villager will level up when this trade is used No, default 1
max_uses NumberProvider How often this trade can be used, until the villager needs to restock No, default 12

Trade Item

Parameter Type Description Mandatory
item Item The base item id. (eg: "minecraft:stone") Yes
choice TradeItem[] If this value is present all others will be ignored and one value will be randomly chosen from this list No
quantity NumberProvider The item count No, default 1
price_multiplier NumberProvider This defines how much the item count of the first buy item is affected by special prices Can only be specified for first buy item, default 0.2
functions LootFunction[] An array of LootFunctions that can specify how an item may be changed No, default none

{
  "wants": [
    {
      "item": "minecraft:emerald",
      "quantity": 3,
      "price_multiplier": 0.2
    }
  ],
  "gives": [
    {
      "item": "minecraft:fishing_rod",
      "quantity": 1,
      "functions": [
        {
          "function": "enchant_with_levels",
          "treasure": false,
          "levels": {
            "min": 5,
            "max": 19
          }
        }
      ]
    }
  ],
  "trader_exp": 10,
  "max_uses": 3,
  "reward_exp": true
}

VC_EnchantBookFactory

This factory generates trades similar to vanilla EnchantBookFactory but allows way more configuration regarding input and what enchantments are picked.

Parameter Type Description Mandatory
wants TradeItem[] An array of TradeItems, specifies what the player needs to pay. The array must contain 1-2 entries. Yes
enchantments Enchantments What kind of enchanted books the villager sells Yes
treasure_multiplier NumberProvider Price multiplier for treasure enchantment books No, default 2
base_price NumberProvider Used for calculating the price (formula below) No, default 2
level_price NumberProvider Used for calculating the price (formula below) No, default 3
random_base_price NumberProvider Used for calculating the price (formula below) No, default 5
random_level_price NumberProvider Used for calculating the price (formula below) No, default 10
trader_exp NumberProvider How much the villager will level up when this trade is used No, default 1
max_uses NumberProvider How often this trade can be used, until the villager needs to restock No, default 12
type FactoryType Type needs to be set to VC_EnchantBookFactory Yes

The formula for calculating the cost is base_price + random.nextInt(random_base_price + level * random_level_price) + level_price * level

Parameter Type Description Mandatory
treasure boolean Whether or not to include treasure enchantments No, default true
blacklist Enchantment[] A list of enchantments that should be excluded No, default empty
rarities Enchantment.Rarity[] A list of rarities that enchantments may be picked from No, default empty
Rarity Weight
COMMON 10
UNCOMMON 5
RARE 2
VERY_RARE 1

{
  "wants": [
    {
      "item": "minecraft:diamond"
    },
    {
      "item": "minecraft:book",
      "quantity": 5
    }
  ],
  "enchantments": {
    "blacklist": [
      "minecraft:mending"
    ]
  },
  "type": "VC_EnchantBookFactory"
}

{
  "wants": [
    {
      "item": "minecraft:iron_ingot"
    },
    {
      "item": "minecraft:wither_skeleton_skull"
    }
  ],
  "enchantments": {
    "blacklist": [
      "minecraft:binding_curse",
      "minecraft:vanishing_curse"
    ],
    "rarities": [
      "VERY_RARE"
    ]
  },
  "treasure_multiplier": 1.5,
  "base_price": 10,
  "level_price": 8,
  "random_base_price": 0,
  "random_level_price": 0,
  "trader_exp": 10,
  "max_uses": 3,
  "type": "VC_EnchantBookFactory"
}

Vanilla Trade Factories

The following article explains how the vanilla factories work. It is however advised to use VillagerConfigs custom factories because they are way more powerful and designed to be configurable.

BuyForOneEmeraldFactory

This is one of the basic vanilla trades that is used by most vanilla trades and allows you to create trades, where the player inputs the price amount of items and receives one emerald in return.

{
  "buy": "minecraft:wheat",
  "price": 20,
  "maxUses": 16,
  "experience": 2,
  "multiplier": 0.05,
  "type": "BuyForOneEmeraldFactory"
}

SellItemFactory

This factory is the opposite BuyForOneEmeraldFactory and allows the player to buy count amount of items for one emerald.

{
  "sell": {
    "id": "minecraft:bread",
    "count": 1
  },
  "price": 1,
  "count": 6,
  "maxUses": 16,
  "experience": 1,
  "multiplier": 0.05,
  "type": "SellItemFactory"
}

SellSuspiciousStewFactory

In vanilla, SellSuspiciousStewFactory is used for creating the farmer's suspicious stew trades.

{
  "effect": "minecraft:night_vision",
  "duration": 100,
  "experience": 15,
  "multiplier": 0.05,
  "type": "SellSuspiciousStewFactory"
}

ProcessItemFactory

This factory allows you to create trades that convert one item to another item for the cost of price emeralds.

{
  "secondBuy": {
    "id": "minecraft:cod",
    "count": 1
  },
  "secondCount": 6,
  "price": 1,
  "sell": {
    "id": "minecraft:cooked_cod",
    "count": 1
  },
  "sellCount": 6,
  "maxUses": 16,
  "experience": 1,
  "multiplier": 0.05,
  "type": "ProcessItemFactory"
}

SellEnchantedToolFactory

This is used by a couple of trades in vanilla to generate trades with various random enchantments.

{
  "tool": {
    "id": "minecraft:fishing_rod",
    "count": 1,
    "tag": "{Damage:0}"
  },
  "basePrice": 3,
  "maxUses": 3,
  "experience": 10,
  "multiplier": 0.2,
  "type": "SellEnchantedToolFactory"
}

TypeAwareBuyForOneEmeraldFactory

This factory allows you to generate trades depending on the profession of the villager.

{
  "map": {
    "plains": "minecraft:oak_boat",
    "taiga": "minecraft:spruce_boat",
    "snow": "minecraft:spruce_boat",
    "desert": "minecraft:jungle_boat",
    "jungle": "minecraft:jungle_boat",
    "savanna": "minecraft:acacia_boat",
    "swamp": "minecraft:dark_oak_boat"
  },
  "count": 1,
  "maxUses": 12,
  "experience": 30,
  "type": "TypeAwareBuyForOneEmeraldFactory"
}

SellPotionHoldingItemFactory

This factory is used by the fletcher to create potion arrow trades.

{
  "sell": {
    "id": "minecraft:tipped_arrow",
    "count": 1
  },
  "sellCount": 5,
  "price": 2,
  "maxUses": 12,
  "experience": 30,
  "secondBuy": "minecraft:arrow",
  "secondCount": 5,
  "priceMultiplier": 0.05,
  "type": "SellPotionHoldingItemFactory"
}

EnchantBookFactory

This trade is used by the librarian to create its enchanted book trades. The experience variable does not affect the level of the enchantments.

{
  "experience": 1,
  "type": "EnchantBookFactory"
}

SellMapFactory

This is a special factory used by the cartographer to create explorer maps, which can be used to find certain structures.

{
  "price": 13,
  "structure": "minecraft:monument",
  "iconType": "MONUMENT",
  "maxUses": 12,
  "experience": 5,
  "type": "SellMapFactory"
}

SellDyedArmorFactory

This factory is only used by the fletcher and creates randomly colored armor pieces.

{
  "sell": "minecraft:leather_leggings",
  "price": 3,
  "maxUses": 12,
  "experience": 1,
  "type": "SellDyedArmorFactory"
}