User Tools

Site Tools


tutorial:datagen_tags

This is an old revision of the document!


Tag Generation

The FabricTagProvider<T> class allows you to generate tag json files.

This page will follow you through all aspects of this provider.

To create a tag generator, create a class that extends FabricTagProvider<T>, where T is the type of tag you want to generate, and register it at your datagen entrypoint like so:

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

The FabricTagProvider<T> class supports all supported tags, make sure to change the Registry in the constructor to the type you are generating your tags for.

Adding a tag

To add a tag, simply call the getOrCreateTagBuilder method with your supplied TagKey<T> in the generateTags() method like so:

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.
}

Output from above example:

smelly_items.json
{
  "replace": false,
  "values": [
    "minecraft:slime_ball",
    "minecraft:rotten_flesh",
    {
      "id": "#minecraft:dirt",
      "required": false
    }
  ]
}

Adding stuff to the builder

To add an item to your tag builder, simply call add(Item item).

If you want to add a tag to your builder, make sure to call addOptionalTag(TagKey<T> tag), as the tag's contents may not be loaded during data generation. If you are certain the tag is loaded, call addTag(TagKey<T> tag)

Various overloads exist for addTag, addOptionalTag and addItem that accepts Identifiers, Strings and RegistryKey<T>s

If you want to forcefully add a tag, ignoring the broken format, you can use forceAddTag(TagKey<T> tag)

Finally, if you want your tag to replace pre-existing tags, you can use setReplace(boolean replace)

An example that uses all of these methods can be found here:

private static final TagKey<Item> SMELLY_ITEMS = TagKey.of(Registry.ITEM_KEY, new Identifier("mymod:smelly_items"));
 
@Override
protected void generateTags() {
     getOrCreateTagBuilder(SMELLY_ITEMS)
                    .add(Items.SLIME_BALL)
                    .add(Items.ROTTEN_FLESH)
                    .addOptionalTag(ItemTags.DIRT)
                    .add(new Identifier("minecraft", "wooden_planks"))
                    .forceAddTag(ItemTags.BANNERS)
                    .setReplace(true)
                    .add(Items.ROTTEN_FLESH.getRegistryEntry().registryKey());
}
tutorial/datagen_tags.1660676393.txt.gz · Last modified: 2022/08/16 18:59 by mineblock11