User Tools

Site Tools


Sidebar

Setup

Basics

Items and Item Groups

Blocks and Block Entities

Entities

World Generation

Miscellaneous

Advanced

Documentation

Examples


Fabric Сontributors

If you'd like to contribute to Fabric, you might be interested in these links:


Extremely Strange People


Wiki Meta

  • Wiki Meta - Starting point for contributing to the wiki
  • Wiki Agenda - See what is on the current agenda, and what other contributors are currently working on.
tutorial:adding_to_loot_tables

Adding items to existing loot tables

Introduction

Sometimes you want to add items to loot tables, for example adding your own drops to a vanilla block or entity. The simplest solution, replacing the loot table file, can break other mods – what if they want to change them as well? We’ll take a look at how you can add items to loot tables without overriding the table.

Our example will be adding eggs to the coal ore loot table.

Listening to loot table loading

Fabric API has an event that’s fired when loot tables are loaded, LootTableLoadingCallback. You can register an event listener for it in your initializer. Let’s also check that the current loot table is minecraft:blocks/coal_ore.

// No magic constants!
private static final Identifier COAL_ORE_LOOT_TABLE_ID = new Identifier("minecraft", "blocks/coal_ore");
 
// Actual code
 
LootTableLoadingCallback.EVENT.register((resourceManager, lootManager, id, supplier, setter) -> {
    if (COAL_ORE_LOOT_TABLE_ID.equals(id)) {
        // Our code will go here
    }
});

Adding items to the table

In loot tables, items are stored in loot entries, and entries are stored in loot pools. To add an item, we’ll need to add a pool with an item entry to the loot table.

We can make a pool with FabricLootPoolBuilder, and add it to the loot table:

LootTableLoadingCallback.EVENT.register((resourceManager, lootManager, id, supplier, setter) -> {
    if (COAL_ORE_LOOT_TABLE_ID.equals(id)) {
        FabricLootPoolBuilder poolBuilder = FabricLootPoolBuilder.builder()
                .withRolls(ConstantLootTableRange.create(1)); // Same as "rolls": 1 in the loot table json
 
        supplier.withPool(poolBuilder);
    }
});

Our pool doesn’t have any items yet, so we’ll make an item entry and add it to the pool, and we're done:

LootTableLoadingCallback.EVENT.register((resourceManager, lootManager, id, supplier, setter) -> {
    if (COAL_ORE_LOOT_TABLE_ID.equals(id)) {
        FabricLootPoolBuilder poolBuilder = FabricLootPoolBuilder.builder()
                .withRolls(ConstantLootTableRange.create(1))
                .withEntry(ItemEntry.builder(Items.EGG));
 
        supplier.withPool(poolBuilder);
    }
});

tutorial/adding_to_loot_tables.txt · Last modified: 2019/08/13 06:46 by liach