Skip to content

VillagerUtils

LLytho edited this page Feb 4, 2024 · 2 revisions

VillagerUtils

// Returns an array of villager professions.
const professions = VillagerUtils.getProfessions();
// `.name` or `.getName()` returns the name of the profession. You can use a map to get an array of names.
const professionNames = VillagerUtils.getProfessions().map((profession) => profession.name);

Trade objects

These utils can be used to create a trade object, which can be added to villager trades through morejs.villager.trades or morejs.wanderer.trades event.
All additional functions for the trades are chainable!

Simple

/*
 * input is either one item or an array with two items. You can always use `Item.of()` too to create an item.
 * Example:
 *      - createSimpleTrade(["minecraft:wheat", "minecraft:wheat"], "minecraft:bread")
 *      - createSimpleTrade("3x minecraft:diamond", "minecraft:diamond_sword")
 */
const simpleTrade = VillagerUtils.createSimpleTrade([...input], output);

Treasure Map Trades

/**
 * For the structures you can use the id, the tag or a weighted list.
 * Example:
 *    - VillagerUtils.createStructureMapTrade("minecraft:diamond", "minecraft:stronghold");
 *    - VillagerUtils.createStructureMapTrade("minecraft:diamond", "#minecraft:mineshaft"); // # is the prefix for the tag.
 *    - VillagerUtils.createStructureMapTrade("minecraft:diamond", MoreJS.weightedList().add(10, "#minecraft:mineshaft").add(2, "minecraft:stronghold"));
 */
const structureTrade = VillagerUtils.createStructureMapTrade([...input], structures);

/**
 * For the biome you can use the id, the tag or a weighted list.
 * Example:
 *    - VillagerUtils.createBiomeMapTrade("minecraft:diamond", "minecraft:jungle");
 *    - VillagerUtils.createBiomeMapTrade("minecraft:diamond", "#minecraft:is_forest"); // # is the prefix for the tag.
 *    - VillagerUtils.createBiomeMapTrade("minecraft:diamond", MoreJS.weightedList().add(10, "#minecraft:is_forest").add(2, "minecraft:jungle"));
 */
const biomeTrade = VillagerUtils.createBiomeMapTrade([...input], structures);

createStructureMapTrade and createBiomeMapTrade returns a treasure trade object to set additional data.

  • .displayName(component) sets the display name for the map.
  • .marker(marker) sets the marker for the map. See https://minecraft.fandom.com/wiki/Map#Map_icons. Default is red_x
  • .noPreview() disables the biome preview for the map. When not set it will render the preview.
  • .scale(scale) sets the scale for the map. Default is 2.

Enchantment Trades

/**
 * The item is the output which will get enchanted.
 * `createEnchantedItemTrade` will also return the trade object to set additional data.
 */
const enchantmentTrade = VillagerUtils.createEnchantedItemTrade([...input], item);
//  enchantmentTrade.enchantments(...enchantments)  // Sets the enchantments for the trade.
//                  .amount(min, max)               // Sets the amount of enchantments for the trade.
//                  .amount(amount);                // Sets the amount of enchantments for the trade.

Stew Trades

/**
 * Duration in ticks!
 */
const stewTrade = VillagerUtils.createStewTrade([...input], [....effects], duration);

Potion Trades

/**
 * Duration in ticks!
 */
const potionTrade = VillagerUtils.createPotionTrade([...input]);
//   potionTrade.potions(...potions)    // Sets the potions for the trade.
//              .onlyBrewablePotion()   // Only potions which can be brewed are allowed.
//              .noBrewablePotion()     // No potions which can be brewed are allowed.
//              .item(item)             // Sets the item for the trade. Default is potion.

Get a random trade (1.19+ Only)

VillagerUtils provide you some useful functions to get a random trade.

const trade = VillagerUtils.getRandomVillagerTrade(profession);
const trade = VillagerUtils.getRandomVillagerTrade(profession, level);
const trade = VillagerUtils.getRandomWandererTrade(level);

When you have the trade, you still have to create the offer from it, if you want to add it at runtime to a villager.

const trade = VillagerUtils.getRandomVillagerTrade(profession);
const offer = trade.createOffer(player, level.getRandom()); // The event where u use it should provide you the player and the level

Get list of possible trades (1.19+ Only)

const trades = VillagerUtils.getVillagerTrades(profession);
const trades = VillagerUtils.getVillagerTrades(profession, level);
const trades = VillagerUtils.getWandererTrades(level);

Update abstract trades from other mods (1.19+ Only)

Some mods don't use the vanilla trades and MoreJS can't load them to make them editable within the MoreJSEvents.villagerTrades event. For this MoreJS has a function to modify them. This still can't cover all possible ways modders do stuff, but it's a try.

// In our example we will use the PlagueDoctorTrades.
// Load the class with KubeJS
const PlagueDoctorTrades = Java.loadClass("com.github.alexthe666.rats.server.misc.PlagueDoctorTrades");
const TRADES = PlagueDoctorTrades.PLAGUE_DOCTOR_TRADES; // for other mods you have to read their source code to find the trades.

// Now we can just set them
VillagerUtils.setAbstractTrades(TRADES, 1, [
    VillagerUtils.createSimpleTrade("minecraft:diamond", "minecraft:diamond_sword"),
]);

// You can also get a list of trades
// This creates a copied list. If you alter the list, you have to set it again.
const pdt_trades = VillagerUtils.getAbstractTrades(TRADES, 1);