Functions

loottweaker.vanilla.loot.Functions

JSON can be verbose and difficult to write. This type can help. It provides methods for creating simple loot functions, but if you wish to use complex loot functions you still have to write them in JSON. minecraft:set_attributes does not have a helper method as it is too complex.

All methods on this page, except parse() create a vanilla loot function, so their parameters are equivalent to the parameters of the equivalent loot function. All vanilla loot functions are listed and documented here.

The corresponding type for loot conditions is Conditions.

Methods

See here for an explanation of the method documentation format used on this page.

static LootFunction enchantRandomly(String[] enchantIDList)
Equivalent to:

minecraft:enchant_randomly

Errors:

if any enchantment ID does not resolve to an enchantment

import loottweaker.vanilla.loot.Functions;

// somePool is a LootPool created elsewhere
somePool.addItemEntry(
    <minecraft:potato>, 1, 0,
    [
        Functions.enchantRandomly(["minecraft:looting"])
    ],
    [] // Arbitrary value for example purposes
);
static LootFunction enchantWithLevels(int min, int max, boolean isTreasure)
Equivalent to:

minecraft:enchant_with_levels

import loottweaker.vanilla.loot.Functions;

// somePool is a LootPool created elsewhere
somePool.addItemEntry(
    <minecraft:potato>, 1, 0,
    [
        // Enchant with a level from 5 to 10, allowing treasure-only enchantments
        Functions.enchantWithLevels(5, 10, true)
    ],
    [] // Arbitrary value for example purposes
);
static LootFunction lootingEnchantBonus(int min, int max, int limit)
Equivalent to:

minecraft:looting_enchant

import loottweaker.vanilla.loot.Functions;

// somePool is a LootPool created elsewhere
somePool.addItemEntry(
    <minecraft:potato>, 1, 0,
    [
        // 1 to 3 extra items per level of looting, maximum of 5 in total
        Functions.lootingEnchantBonus(1, 3, 5)
    ],
    [] // Arbitrary value for example purposes
);
static LootFunction setCount(int min, int max)
Equivalent to:

minecraft:set_count

import loottweaker.vanilla.loot.Functions;

// somePool is a LootPool created elsewhere
somePool.addItemEntry(
    <minecraft:potato>, 1, 0,
    [
        // Stack size of 1 to 3
        Functions.setCount(1, 3)
    ],
    [] // Arbitrary value for example purposes
);
static LootFunction setDamage(float min, float max)
Equivalent to:

minecraft:set_damage

Errors:

if max is greater than 1.0

import loottweaker.vanilla.loot.Functions;

// somePool is a LootPool created elsewhere
somePool.addItemEntry(
    <minecraft:potato>, 1, 0,
    [
        // 75% to 90% durability remaining (10% to 25% damage)
        Functions.setDamage(0.75, 0.90)
    ],
    [] // Arbitrary value for example purposes
);
static LootFunction setMetadata(int min, int max)
Equivalent to:

minecraft:set_data

import loottweaker.vanilla.loot.Functions;

// somePool is a LootPool created elsewhere
somePool.addItemEntry(
    <minecraft:potato>, 1, 0,
    [
        // Metadata of 1 to 3
        Functions.setMetadata(1, 3)
    ],
    [] // Arbitrary value for example purposes
);
static LootFunction setNBT(DataMap nbtData)
Equivalent to:

minecraft:set_nbt

Errors:

if nbtData is not a compound tag

import loottweaker.vanilla.loot.Functions;

// somePool is a LootPool created elsewhere
somePool.addItemEntry(
    <minecraft:potato>, 1, 0,
    [
        // Makes item unbreakable via NBT
        Functions.setNBT({"Unbreakable": true})
    ],
    [] // Arbitrary value for example purposes
);
static LootFunction smelt()
Equivalent to:

minecraft:furnace_smelt

import loottweaker.vanilla.loot.Functions;

// somePool is a LootPool created elsewhere
somePool.addItemEntry(
    <minecraft:potato>, 1, 0,
    [
        Functions.smelt()
    ],
    [] // Arbitrary value for example purposes
);
static LootFunction parse(DataMap json)

Deprecated. 0.3.0 introduced entry addition methods capable of automatically parsing Maps into LootConditions. Parses a DataMap into a LootFunction.

Parameters:
  • json - an instance of DataMap representing a LootCondition in JSON form. It is recommended that the keys are enclosed in quotes to avoid conflicts between JSON key names and ZenScript keywords.

Returns:

json as a LootFunction.

Errors:

if json does not parse successfully.

static LootFunction zenscript(loottweaker.CustomLootFunction zenFunction)

Adapts zenFunction into a LootFunction.

Parameters:
  • zenFunction - a ZenScript function <https://docs.blamejared.com/1.12/en/AdvancedFunctions/Custom_Functions> with parameters (IItemStack, IRandom, LootContext) and return type IItemStack.

Returns:

a loot condition which changes the generated item using zenFunction.

See:
import crafttweaker.item.IItemStack;
import crafttweaker.util.IRandom;
import loottweaker.LootContext;
import loottweaker.vanilla.loot.Functions;

// somePool is a LootPool created elsewhere
somePool.addItemEntry(
    <minecraft:potato>, 1, 0,
    [
        Functions.zenscript(function(input as IItemStack, rng as IRandom, context as LootContext) as IItemStack
        {
            return input.withDisplayName("Super Potato");
        })
    ],
    [] // Arbitrary value for example purposes
);
loottweaker.CustomLootFunction

A ZenScript function <https://docs.blamejared.com/1.12/en/AdvancedFunctions/Custom_Functions> with parameters (IItemStack, IRandom, LootContext) and return type IItemStack.