User Tools

Site Tools


Sidebar

โ† Go back to the homepage

Fabric Tutorials

Setup

These pages will help you setup a productive development environment and semi-automated releases.

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:datagen_loot

Before reading this, make sure you have a class that implements DataGenerationEntrypoint

To get started, make a class (or a few, you need one for blocks, chests and entities) that extends SimpleFabricLootTableProvider and register it like so:

Setting Up

To get started with block loot, create a block loot table generator

private static class MyBlockLootTables extends SimpleFabricLootTableProvider {
    public MyBlockLootTables(FabricDataGenerator dataGenerator) {
         super(dataGenerator, LootContextTypes.BLOCK);
    }
 
    @Override
    public void accept(BiConsumer<Identifier, LootTable.Builder> biConsumer) {
        // ...
    }
}
 
// ...
 
 
@Override
public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) {
    fabricDataGenerator.addProvider(MyBlockLootTables::new);
}

Let's just create a simple ore block and an item to drop from it for a block loot table. Add this to your block init or Tutorial class in this case.

public static final Block TEST_ORE = Registry.register(Registry.BLOCK, new Identifier("tutorial", "test_ore"), new Block(...));
 
public static final Item TEST_ITEM = Registry.register(Registry.ITEM, new Identifier("tutorial", "test_item", new Item(...));
// Let's just ignore the fact that there isn't a block item ๐Ÿ˜…

ADDING BLOCK LOOT

private static class MyBlockLootTables extends SimpleFabricLootTableProvider {
    public MyBlockLootTables(FabricDataGenerator dataGenerator) {
         super(dataGenerator, LootContextTypes.BLOCK);
    }
 
    @Override
    public void accept(BiConsumer<Identifier, LootTable.Builder> biConsumer) {
    // The BlockLootTableGenerator class contains a behemoth of utility methods. Just take some time and go through the methods available to override.
        biConsumer.accept(new Identifier("tutorial", "test_block"), BlockLootTableGenerator.drops(Tutorial.TEST_BLOCK, Tutorial.TEST_ITEM, ConstantLootNumberProvider.create(9.0F)));
    }
}

Now that we successfully adding a block. Now let's add chest loot.

ADDING CHEST LOOT

Firstly, we need an identifier. This identifier points to a json file that contains your chest loot.

// In Tutorial class
public static final Identifier TEST_CHEST = new Identifier("tutorial", "chests/test_loot");

Let's create a chest loot table generator and register it like so.

private static class MyChestLootTables extends SimpleFabricLootTableProvider {
    public MyChestLootGenerator(FabricDataGenerator dataGenerator) {
	super(dataGenerator, LootContextTypes.CHEST);
    }
 
    @Override
    public void accept(BiConsumer<Identifier, LootTable.Builder> biConsumer) {
        biConsumer.accept(Tutorial.TEST_CHEST, LootTable.builder()
                  .pool(LootPool.builder().rolls(ConstantLootNumberProvider.create(1.0F))
                  .with(ItemEntry.builder(Items.DIAMOND)
				.apply(SetCountLootFunction.builder(ConstantLootNumberProvider.create(1.0F)))
                  .with(ItemEntry.builder(Items.DIAMOND_SWORD)).apply(EnchantWithLevelsLootFunction.create(UniformLootNumberProvider.create(20.0F, 39.0F))))
        );
    }
}
 
// ...
 
@Override
public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) {
    fabricDataGenerator.addProvider(MyChestLootTables::new);
}
tutorial/datagen_loot.txt ยท Last modified: 2022/09/16 16:45 by nexus-dino