Skip to content
Jacky edited this page Oct 16, 2024 · 7 revisions

Fields

Fields are properties of a trade that can be compared and tested in predicates and modified with a set: action. Fields may also contain properties themselves.

All fields

apply-discounts (boolean) - whether player discounts (due to player reputation) should apply to this trade.

max-uses (integer) - maximum number of uses this trade has before it is locked.

uses (integer) - current number of uses

award-experience (boolean) - whether players are given XP for this trade

villager-experience (integer) - XP villager gains for this trade

ingredient-0 (ItemStack) - item in the first buy slot

ingredient-1 (ItemStack) - item in the second buy slot

result (ItemStack) - item in the result slot

villager (Villager) - the villager

index (integer) - the 0-based index of the trade

is-new (boolean) - whether the trade is new. A trade can only be new when initially acquired by the villager. This may be useful for making sure that recipes and Lua scripts only run once.

Since beta8

world (World) - the world the villager is in.

Since beta11

merchant-type (string) - the entity type of the villager (villager/wandering_trader)

ItemStack properties

amount (integer) - amount of items in this ItemStack

(NOTE: when modifying ingredient-0.amount, stacks that exceed the max stack size of the item will be split into two and put in ingredient-0 and ingredient-1)

type (string) - type of the item (namespaced like minecraft:stone)

name (string) - custom name of the item

unbreakable (boolean) - whether the item is unbreakable

damage (integer) - damage of the item

enchantments (enchantments) - enchantments (stored enchantments for enchanted books)

Since v2.0-beta8

safe_enchantments (enchantments) - enchantments (stored enchantments for enchanted books). Modifications will respect enchantment restrictions.

unsafe_enchantments (enchantments) - enchantments (stored enchantments for enchanted books). Modifications will ignore all enchantment restrictions.

Enchantments properties

size (read-only integer) - number of enchantments present

any valid enchantment ID (integer) - level of that enchantment

Examples

To match diamond helmets with 2 or more enchantments:

    when:
      result:
        type: minecraft:diamond_helmet
        enchantments:
          size: '>= 2'

Or:

    when:
      result.type: minecraft:diamond_helmet
      result.enchantments.size: '>= 2'

To give all diamond items curse of vanishing and a custom name:

    when:
      result.type: contains DIAMOND_
    do:
      result:
        name: "§4CURSED DIAMOND ITEM"
        enchantments.vanishing_curse: 1

To quintuple all wandering trader trades:

    when:
        merchant-type: wandering_trader
    do:
        set:
            result.amount: "*= 5"

Villager properties

(Note: all villager properties are currently read-only)

type (string) - type of the villager (usually the biome of their village)

profession (string) - profession of the villager

level (integer) - level of the villager (between 1 - 5)

experience (integer) - experience of the villager

recipe-count (integer) - number of recipes the villager has

world (world) - the world the villager is in

World properties

(Note: all world properties are currently read-only)

name (string) - name of the world

environment (string) - environment of the world (NORMAL/NETHER/THE_END/CUSTOM)

time (integer) - current time of the world

full-time (integer) - total time of the world

is-day-time (boolean) - whether it is currently daytime

weather (string) - weather (clear/rain/thunder)

Examples

To match a librarian:

    when:
      villager:
        profession: LIBRARIAN

To match an weaponsmith or toolsmith with concise syntax:

    when:
      villager.profession: contains SMITH

Operating on different types of fields

Integers

In predicates, you can use < / <= / > / >= / == / != integer or between a and b or in a..b (accepts 3 dots) to test a field against a condition. Due to YAML constraints, you may need to add quotes around the predicate.

For example, to target really expensive trades:

    when:
      ingredient-0.amount: between 32 and 64 # Or just '>= 32'

You can also use these operators when assigning values. They will simply try to clamp the value into the range. Simple arithmetic operations += / -= / *= / /= / %= integer are also supported. Of course you can always assign a constant integer.

For example, to decimate your economy:

    when: # paper trade
      ingredient-0.type: minecraft:paper
    do:
      set:
        ingredient-0.amount: 1

To fix your economy:

    when: # cheap paper trade
      ingredient-0.type: minecraft:paper
      ingredient-0.amount: '< 8'
    do:
      set:
        ingredient-0.amount: '> 48' # Ensures that the price is at least 48 emeralds

Strings

In predicates, you can check whether a string equals or contains another string, or match the string against a regular expression.

For example, to match books, bookshelves, book and quills, written books and enchanted books:

    when:
      ingredient-0.type: contains book

To match paper:

    when:
      ingredient-0.type: minecraft:paper
      # or
      ingredient-0.type: '= minecraft:paper'

Note that string comparisons (apart from regular expression matching) are case insensitive by default. To enforce case sensitivity, add quotes around the string. To match item with weird name:

    when:
      ingredient-0.name: contains "WeIrD cApItAlIzAtIoN"

You can only assign a predefined value to a string field. To give item with weird name:

    set:
      ingredient-0.name: I have WeIrD cApItAlIzAtIoN

Booleans

It's just true or false.

ItemStacks

You can pass an item to set the field to that item, or test whether the field matches the item, ignoring the amount. The item passed must be in /give syntax.

    when:
      result: diamond_pickaxe{display:{Name:'{"text":"test"}'}
    do:
      set:
        result: diamond_pickaxe{display:{Name:'{"text":"test2"}'}

To also test for/set the amount:

    when:
      result: diamond_pickaxe{display:{Name:'{"text":"test"}'}
      result.amount: 1
    do:
      set:
        result: diamond_pickaxe{display:{Name:'{"text":"test2"}'}
        result.amount: 2

Enchantments

In predicates, you can check whether a predefined key exists. For example, to match item with mending:

    when:
      result.enchantments: contains mending

You can also check if all/some/none of the enchantments are treasure/curse enchantments.

    when:
      ingredient-0.enchantments: none are treasure
      ingredient-1.enchantments: all aren't treasures
      result.enchantments: some are curses

Or test whether preexisting enchantments will conflict with another enchantment.

    when:
      result.enchantments: conflicts with minecraft:mending

You cannot assign to maps.

Clone this wiki locally