Skip to content

1.19.2 Functions

LLytho edited this page Feb 3, 2024 · 4 revisions

Intro

Loot functions help you to transform items before they will be dropped. If you ever worked with vanilla loot tables, you will be familiar with them.

Functions

enchantRandomly() or enchantRandomly(...enchantments)

Randomly enchants the item. You can provide enchantments to choose from.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:emerald_block").pool((p) => {
        p.addLoot("minecraft:diamond_axe");
        p.enchantRandomly();
    });
});

enchantWithLevels(NumberProvider) or enchantWithLevels(NumberProvider, allowTreasure)

Randomly enchants with specific levels. Roughly equivalent to using an enchantment table. allowTreasure can be true or false if [treasure items][https://minecraft.fandom.com/wiki/enchanting#summary_of_enchantments] are allowed.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:emerald_block").pool((p) => {
        p.addLoot("minecraft:diamond_pickaxe");
        p.enchantWithLevels([2, 4]);
    });
});

applyLootingBonus(NumberProvider)

Adjusts the item stack size based on the looting enchantment level the killer has.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:emerald_block").pool((p) => {
        p.addLoot("minecraft:emerald");
        p.applyLootingBonus([1, 3]);
    });
});

applyBinomialDistributionBonus(enchantment, probability, n)

Adjusts the item stack size based on a binomial distribution bonus.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:emerald_block").pool((p) => {
        p.addLoot("minecraft:emerald");
        p.applyBinomialDistributionBonus("minecraft:fortune", 0.2, 3);
    });
});

applyOreBonus(enchantment)

Adjusts the item stack size based on ore bonus. Used to simulate fortune effect. In the example we are using "minecraft:fortune" this does not mean the player's item must be enchanted with fortune. The given enchantment is used to calculate the bonus: bonus = random(enchantmentLevel + 2) + 1

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:emerald_block").pool((p) => {
        p.addLoot("minecraft:emerald");
        p.applyOreBonus("minecraft:fortune");
    });
});

applyBonus(enchantment, multiplier)

Adjusts the item stack size based on a multiplier.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:emerald_block").pool((p) => {
        p.addLoot("minecraft:emerald");
        p.applyBonus("minecraft:fortune", 3);
    });
});

simulateExplosionDecay()

Removes some items from the stack if there was an explosion.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:emerald_block").pool((p) => {
        p.addLoot(Item.of("minecraft:emerald", 50));
        p.simulateExplosionDecay();
    });
});

smeltLoot()

Smelts the loot if possible. Can be used to simulate fire aspect. The example uses functions() to apply an ItemFilter.

LootJS.modifiers((event) => {
    event.addEntityLootModifier("minecraft:chicken").functions(Item.of("chicken"), (f) => {
        f.smeltLoot();
    });
});

damage(NumberProvider)

Damages the item by friction between 0 and 1 where 0 is zero durability.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:emerald_block").pool((p) => {
        p.addLoot("minecraft:netherite_sword");
        p.damage([0.3, 0.9]);
    });
});

addPotion(potion)

Applies the potion nbt to the item.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:emerald_block").pool((p) => {
        p.addLoot("minecraft:potion");
        p.addPotion("poison");
    });
});

limitCount(NumberProvider, NumberProvider)

Limits the stack size against the given number providers, where the first provider stands for min and the second one for max.

The following example will limit the stack size to at least 5 - 10 and max 30 - 64.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:emerald_block").pool((p) => {
        p.addLoot("minecraft:diamond");
        p.limitCount([5, 10], [30, 64]);
    });
});

addAttributes(callback)

Add attribute modifiers to the item. In the callback, you can use multiple functions to add attributes. probability can be used to have a random chance the attribute will be applied.

If using simple, it will use the default equipment slot for the item.

  • simple(attribute, NumberProvider)
  • simple(probability, attribute, NumberProvider)
  • forSlots(attribute, NumberProvider, ...slots)
  • forSlots(probability, attribute, NumberProvider, ...slots)

Possible attributes:

  • generic.max_health
  • generic.follow_range
  • generic.knockback_resistance
  • generic.movement_speed
  • generic.flying_speed
  • generic.attack_damage
  • generic.attack_knockback
  • generic.attack_speed
  • generic.armor
  • generic.armor_toughness
  • generic.luck
LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:emerald_block").pool((p) => {
        p.addLoot("minecraft:diamond_sword");
        p.addAttributes((attributes) => {
            attributes.simple("generic.max_health", 1);
            attributes.simple(0.99, "generic.max_health", 2);
            attributes.forSlots("generic.max_health", 3, [SLOT_MAINHAND]);
            attributes.forSlots(0.99, "generic.max_health", 4, [SLOT_OFFHAND]);
        });
    });
});

addLore(...components)

Adds lore to the item. Can be used for some custom tooltips. You can use any Text.of(...) notation from KubeJS or just simple strings. You can also pass multiple components if you need multiple lines.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:emerald_block").pool((p) => {
        p.addLoot("");
        p.addLore(["This is a lore", Text.red("This is a red lore")]);
    });
});

replaceLore(...components)

Replaces the current item lore.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:emerald_block").pool((p) => {
        p.addLoot("");
        p.replaceLore(Text.of("Some lore text for your item"));
    });
});

setName(component)

Sets the item's custom name.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:emerald_block").pool((p) => {
        p.addLoot("minecraft:emerald");
        p.setName(Text.blue("This is a blue name"));
    });
});

addNBT(nbt) or addNbt(nbt)

Adds nbt to the item.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:emerald_block").pool((p) => {
        p.addLoot("minecraft:iron_sword");
        p.addNBT({ Damage: 20 });
    });
});

functions(ItemFilter, callback)

Can be used to apply one or multiple functions to a specific ItemFilter. As a function will be applied to all items in the current pool, this or pools can be used to scope them.

LootJS.modifiers((event) => {
    event.addEntityLootModifier("minecraft:chicken").functions(ItemFilter.FOOD, (f) => {
        f.smeltLoot();
    });
});

customFunction(json)

Can be used to add a modded function via json. In the example we will use irons_spellbooks:randomize_spell and only apply it to weapons.

LootJS.modifiers((event) => {
    event.addLootTableModifier(/.*/).functions(ItemFilter.WEAPON, (f) => {
        f.customFunction({
            function: "irons_spellbooks:randomize_spell",
            quality: {
                min: 0.5,
                max: 1,
            },
        });
    });
});