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_setup

Getting started with Data Generation

Data Generation is a new module of the Fabric API, it allows you to dynamically generate Recipes, Language Files, Loot Tables, Advancements and pretty much anything with Custom Providers.

To implement data generation, add the following to your build.gradle:

loom {
    runs {
        // This adds a new gradle task that runs the datagen API: "gradlew runDatagenClient"
        datagenClient {
            inherit client
            name "Data Generation"
            vmArg "-Dfabric-api.datagen"
            vmArg "-Dfabric-api.datagen.output-dir=${file("src/main/generated")}"
            vmArg "-Dfabric-api.datagen.strict-validation"
 
            runDir "build/datagen"
        }
    }
}
 
// Add the datagenned files into the jar.
sourceSets {
    main {
        resources {
            srcDirs += [
                    'src/main/generated'
            ]
        }
    }
}

Next, create a class that extends DataGeneratorEntrypoint, and add it to your fabric.mod.json:

public class MyModDatagen implements DataGeneratorEntrypoint {
    @Override
    public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) {
        // ...
    }
}

This will register your datagen entrypoint, allowing onInitializeDataGenerator to be called when the gradlew runDatagenClient task is ran.

"entrypoints": {
    "fabric-datagen": [
          "com.fabric.examplemod.MyModDatagen"
    ]
}

Adding Providers

This is a generic guide on how to add a provider, for more in depth infomation on each provider, see their respective pages:

In this example, we will be creating a tag provider, as it is the simplest to understand.

Firstly, inside your MyModDatagen class, create a new private static class that extends FabricTagProvider<T>:

If you want, you can place this class in a seperate file, but we recommend it stays in your datagen entrypoint class.

private static class MyTagGenerator extends FabricTagProvider<Item> {
        public MyTagGenerator(FabricDataGenerator dataGenerator) {
            super(dataGenerator, Registry.ITEM);
        }
 
        @Override
        protected void generateTags() {
 
        }
}

We will add some basic tags inside generateTags() for example's sake.

// We will create an item tag called "smelly_items".
private static final TagKey<Item> SMELLY_ITEMS = TagKey.of(Registry.ITEM_KEY, new Identifier("mymod:smelly_items"));
 
@Override
protected void generateTags() {
     // This creates a tag builder, where we add slime balls, rotten flesh and everything in the minecraft:dirt item tag.
     getOrCreateTagBuilder(SMELLY_ITEMS)
              .add(Items.SLIME_BALL)
              .add(Items.ROTTEN_FLESH)
              .addOptionalTag(ItemTags.DIRT);
     // This will automatically generate "assets/mymod/tags/items/smelly_items.json" in the "generated" folder.
}

Now, we will need to register this provider to the data generator in the onInitializeDataGenerator in your entrypoint class like so:

public class MyModDatagen implements DataGeneratorEntrypoint {
    @Override
    public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) {
        fabricDataGenerator.addProvider(MyTagGenerator::new);
    }
}

When we run gradlew runDatagenClient, we should see the generated folder forms, and the item tag json should be present:

smelly_items.json
{
  "replace": false,
  "values": [
    "minecraft:slime_ball",
    "minecraft:rotten_flesh",
    {
      "id": "#minecraft:dirt",
      "required": false
    }
  ]
}
tutorial/datagen_setup.txt · Last modified: 2022/08/29 18:39 by modmuss50