Skip to content

1.19.2 Actions

LLytho edited this page Mar 13, 2024 · 4 revisions

Intro

Actions are used to change the current loot pool outcome or to trigger effects. You can simply chain multiple actions together. For every Loot Modification, you need at least one action.

Actions

addLoot(...items)

Adds one or multiple items to the current loot pool.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:gravel").addLoot("minecraft:flint");
});

addAlternativesLoot(...items)

According to the Minecraft Wiki: Will only add the first successful (conditions are met) item to the loot pool. If no item is successful, no item will be added.

LootJS.modifiers((event) => {
    event
        .addBlockLootModifier("minecraft:coal_ore")
        .removeLoot(Ingredient.all)
        .addAlternativesLoot(
            LootEntry.of("minecraft:apple").when((c) => c.randomChance(0.8)),
            LootEntry.of("minecraft:stick").when((c) => c.randomChance(0.3)),
            LootEntry.of("minecraft:diamond").when((c) => c.randomChance(0.7)),
            LootEntry.of("minecraft:coal").when((c) => c.randomChance(0.99)),
            LootEntry.of("minecraft:torch").when((c) => c.randomChance(0.2))
        );
});

Another more complex example can be found here

addSequenceLoot(...items)

Will add multiple items which will be rolled one after another. According to the Minecraft Wiki, it will add all items until one condition fails. After that no more items will be added.

In our example, all items in order may be added to the loot pool. But if one item fails, this item and all following items will not be added.

LootJS.modifiers((event) => {
    event
        .addBlockLootModifier("minecraft:coal_ore")
        .removeLoot(Ingredient.all)
        .addSequenceLoot(
            LootEntry.of("minecraft:apple").when((c) => c.randomChance(0.8)),
            LootEntry.of("minecraft:stick").when((c) => c.randomChance(0.3)),
            LootEntry.of("minecraft:diamond").when((c) => c.randomChance(0.7)),
            LootEntry.of("minecraft:coal").when((c) => c.randomChance(0.99)),
            LootEntry.of("minecraft:torch").when((c) => c.randomChance(0.2))
        );
});

Another more complex example can be found here

addWeightedLoot(NumberProvider, [...items]) or addWeightedLoot([...items])

Adds one or multiple items with weights to the current loot pool. If a NumberProvider is given, it will roll multiple items based on it.

Example: [3, 10] is the number provider with 3 min and 10 max rolls. LootJS will randomly select a value between min and max.

LootJS.modifiers((event) => {
    event
        .addEntityLootModifier("minecraft:creeper")
        .addWeightedLoot(
            [3, 10],
            [Item.of("minecraft:gunpowder").withChance(50), Item.of("minecraft:nether_star").withChance(5)]
        );
});

You can also use a number only which is then treated as [n, n].

LootJS.modifiers((event) => {
    event
        .addEntityLootModifier("minecraft:creeper")
        .addWeightedLoot(5, [
            Item.of("minecraft:gunpowder").withChance(50),
            Item.of("minecraft:nether_star").withChance(5),
        ]);
});

If you don't use a number provider, it's equal to [1, 1].

LootJS.modifiers((event) => {
    event
        .addEntityLootModifier("minecraft:creeper")
        .addWeightedLoot([
            Item.of("minecraft:gunpowder").withChance(50),
            Item.of("minecraft:nether_star").withChance(5),
        ]);
});

removeLoot(ItemFilter)

Removes all items from the current loot pool which matches the given ItemFilter.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:gravel").removeLoot("minecraft:flint");
});

replaceLoot(ItemFilter, item) or replaceLoot(ItemFilter, item, preserveCount)

Replaces all items from the current loot pool which match the given ItemFilter.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:gravel").replaceLoot("minecraft:flint", "minecraft:diamond");
    // or .replaceLoot("minecraft:flint", "minecraft:diamond", true) if you want to preserve the stack size
});

In this example, we want to replace flint with diamonds in the gravel loot pool.

modifyLoot(ItemFilter, callback)

For every item in the current loot pool which matches the given ItemFilter, a callback will be called. LootJS will pass the item into the callback to modify it and return the item. Make sure to always return an item.

LootJS.modifiers((event) => {
    event
        .addLootTypeModifier(LootType.ENTITY)
        .weatherCheck({
            raining: true,
        })
        .modifyLoot(Ingredient.all, (itemStack) => {
            itemStack.setCount(itemStack.getCount() * 2);
            return itemStack;
        });
});

In this example, we will double all loot when it's raining.

triggerExplosion(radius, destroy, fire)

Triggers an explosion on the position where the loot will be dropped. The items will not be destroyed. The radius can be any number. For destroy and fire you can pass true or false.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:gravel").triggerExplosion(1, false, false);
});

triggerLightningStrike(shouldDamage)

Triggers a lightning strike on the position where the loot will be dropped. The items will not be destroyed. Use true or false for shouldDamage.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:gravel").triggerLightningStrike(false);
});

dropExperience(amount)

Drops amount of experience on the position where the loot will be dropped.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:gravel").dropExperience(5);
});

pool(callback)

Roll a dynamic created pool. The callback provides a pool you can work with. If an NumberProvider is given through .rolls(), it will roll the pool multiple times.

Example: [1, 3] is the number provider with 1 min and 3 max rolls. LootJS will randomly select a value between min and max.

LootJS.modifiers((event) => {
    event.addEntityLootModifier("minecraft:creeper").pool((pool) => {
        pool.rolls([1, 3]);
        pool.randomChance(0.3)
            .or((or) => {
                or.anyBiome("minecraft:jungle");
                or.lightLevel(0, 7);
            })
            .addLoot("minecraft:diamond");
    });
});

playerAction(callback)

Use playerAction to trigger custom actions to a player. The callback provides you the vanilla player, not the PlayerJS class from KubeJS.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:diamond_block").playerAction((player) => {
        player.giveExperiencePoints(100);
    });
});

apply(callback)

With apply, you apply a custom callback onto the current loot pool. LootJS will provide you the LootContextJS.

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:gravel").apply((context) => {
        // do whatever you like
        // example: context.level to access the level
    });
});