Skip to content

1.19.2 LootEntry

LLytho edited this page Oct 10, 2023 · 2 revisions

LootEntry

The LootEntry class is used to create a loot entry. Methods like addLoot, replaceLoot, modifyLoot etc. takes a LootEntry as a parameter, but you can still use item ids or other KubeJS item notations. But LootEntry's will give you more control over the loot, as you can directly chain functions onto it or add conditions through .when.

Create a LootEntry

LootEntry.of("minecraft:apple"); // Create a LootEntry from an item id
LootEntry.of("minecraft:apple", 1); // With a count
LootEntry.of("minecraft:apply", { ... }) // With nbt
LootEntry.of("minecraft:apple", 1, { ... }) // With count and nbt
LootEntry.withChance(item, chance); // With a chance. For `item` you can use any KubeJS notation for items

Modify the LootEntry

As mentioned above, you can just chain functions onto the LootEntry. For example:

// Enchant the item with a random enchantment and damage it by 30%
LootEntry.of("minecraft:diamond_sword").enchantRandomly().damage(0.3);

Apply conditions

You can easily add conditions to the LootEntry. This is useful to directly filter inside actions like addLoot, replaceLoot etc.

// Add a condition that the item will only be added if the random chance is 0.3
LootEntry.of("minecraft:apple").when((c) => c.randomChance(0.3));

Example usage

Default addLoot action

LootJS.modifiers((event) => {
    event.addBlockLootModifier("minecraft:oak_log").addLoot(
        /**
         * Creates a LootEntry with 50% chance of dropping a stick.
         */
        LootEntry.of("minecraft:stick").when((c) => c.randomChance(0.5)),
        /**
         * Creates a LootEntry with 50% chance of dropping a stick.
         */
        LootEntry.of("minecraft:apple").when((c) => c.randomChance(0.5))
    );
});

Using addAlternativesLoot action

LootJS.modifiers((event) => {
    /**
     * First loot entry with a condition. Will drop if the player has fortune.
     */
    const stickWhenFortune = LootEntry.of("minecraft:stick")
        .applyOreBonus("minecraft:fortune")
        .when((c) => c.matchMainHand(ItemFilter.hasEnchantment("minecraft:fortune")));

    /**
     * Second loot entry with a condition. Will drop if the player has silk touch and the first entry doesn't match.
     */
    const appleWhenSilkTouch = LootEntry.of("minecraft:apple").when((c) =>
        c.matchMainHand(ItemFilter.hasEnchantment("minecraft:silk_touch"))
    );

    /**
     * No conditions just an item, so this will always drop if the other two don't.
     */
    const ironIngot = "minecraft:iron_ingot";

    event
        .addBlockLootModifier("minecraft:iron_ore")
        .removeLoot(Ingredient.all)
        .addAlternativesLoot(stickWhenFortune, appleWhenSilkTouch, ironIngot);
});

Using addSequenceLoot action

LootJS.modifiers((event) => {
    /**
     * First loot entry with a condition. Will drop if the player has fortune.
     */
    const stickWhenFortune = LootEntry.of("minecraft:stick").when((c) =>
        c.matchMainHand(ItemFilter.hasEnchantment("minecraft:fortune"))
    );

    /**
     * Second loot entry with a condition. Will drop if the player has silk touch.
     */
    const appleWhenEfficiency = LootEntry.of("minecraft:apple").when((c) =>
        c.matchMainHand(ItemFilter.hasEnchantment("minecraft:efficiency"))
    );

    /**
     * Simple item without conditions or anything else, will drop
     */
    const flint = "minecraft:flint";

    /**
     * Random chance is 0 so no diamond will ever drop. Just to show, that it will skip all other entries.
     */
    const diamondNoDrop = LootEntry.of("minecraft:diamond").when((c) => c.randomChance(0.0));

    /**
     * No conditions just an item, but this will not drop, because the previous entry failed.
     */
    const ironIngot = "minecraft:iron_ingot";

    event
        .addBlockLootModifier("minecraft:coal_ore")
        .removeLoot(Ingredient.all)
        .addSequenceLoot(stickWhenFortune, appleWhenEfficiency, flint, diamondNoDrop, ironIngot);
});