User Tools

Site Tools


Sidebar

← Go back to the homepage

Fabric Tutorials

Setup

Basics

These pages are essential must-reads when modding with Fabric, and modding Minecraft in general, if you are new to modding, it is recommended you read the following.

Items

Blocks and Block Entities

Data Generation

World Generation

Commands

These pages will guide you through Mojang's Brigadier library which allows you to create commands with complex arguments and actions.

Events

These pages will guide you through using the many events included in Fabric API, and how to create your own events for you or other mods to use.

Entities

Fluids

Mixins & ASM

These pages will guide you through the usage of SpongePowered's Mixin library, which is a highly complex topic. We recommend you read these pages thoroughly.

Miscellaneous

Yarn

Contribute to Fabric

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, LootTableEvents.MODIFY. You can register an event listener for it in your initializer. Let’s also check that the current loot table is the coal ore loot table.

// No magic constants!
private static final Identifier COAL_ORE_LOOT_TABLE_ID = Blocks.COAL_ORE.getLootTableId();
 
// Actual code
 
LootTableEvents.MODIFY.register((resourceManager, lootManager, id, tableBuilder, source) -> {
    // Let's only modify built-in loot tables and leave data pack loot tables untouched by checking the source.
    // We also check that the loot table ID is equal to the ID we want.
    if (source.isBuiltin() && 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 pool 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 LootPool$Builder, and add it to the loot table:

LootTableEvents.MODIFY.register((resourceManager, lootManager, id, tableBuilder, source) -> {
    if (source.isBuiltin() && COAL_ORE_LOOT_TABLE_ID.equals(id)) {
        LootPool.Builder poolBuilder = LootPool.builder();
 
        tableBuilder.pool(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:

LootTableEvents.MODIFY.register((resourceManager, lootManager, id, tableBuilder, source) -> {
    if (source.isBuiltin() && COAL_ORE_LOOT_TABLE_ID.equals(id)) {
        LootPool.Builder poolBuilder = LootPool.builder()
                .with(ItemEntry.builder(Items.EGG));
 
        tableBuilder.pool(poolBuilder);
    }
});

tutorial/adding_to_loot_tables.txt · Last modified: 2022/06/21 22:54 by juuz