User Tools

Site Tools


tutorial:datagen_setup

This is an old revision of the document!


Getting started using 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"
 
            ideConfigGenerated = true
            runDir "build/datagen"
        }
    }
}
 
// We should run datagen whenever we build.
remapJar.dependsOn datagenClient
 
// 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.1659986781.txt.gz · Last modified: 2022/08/08 19:26 by mineblock11