User Tools

Site Tools


tutorial:datagen_tags

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(FabricDataOutput output, CompletableFuture<RegistryWrapper.WrapperLookup> registriesFuture) {
        super(output, RegistryKeys.ITEM, registriesFuture);
   }
 
   @Override
   protected void configure(RegistryWrapper.WrapperLookup lookup) {
 
   }
}
 
// ...
 
@Override
public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) {
    FabricDataGenerator.Pack pack = fabricDataGenerator.createPack();
    // ...
    pack.add(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 configure(RegistryWrapper.WrapperLookup lookup) method like so:

public static final TagKey<Item> SMELLY_ITEMS = TagKey.of(RegistryKeys.ITEM, Identifier.of("mymod", "smelly_items"));
 
@Override
protected void configure(RegistryWrapper.WrapperLookup lookup) {
     // 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 "data/mymod/tags/item/smelly_items.json" in the "generated" folder.
}

Output from the example above:

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:

public static final TagKey<Item> SMELLY_ITEMS = TagKey.of(RegistryKeys.ITEM, Identifier.of("mymod", "smelly_items"));
 
@Override
protected void configure(RegistryWrapper.WrapperLookup lookup) {
     getOrCreateTagBuilder(SMELLY_ITEMS)
                    .add(Items.SLIME_BALL)
                    .add(Items.ROTTEN_FLESH)
                    .addOptionalTag(ItemTags.DIRT)
                    .add(Identifier.of("minecraft", "wooden_planks"))
                    .forceAddTag(ItemTags.BANNERS)
                    .setReplace(true)
                    .add(Items.ROTTEN_FLESH.getRegistryEntry().registryKey());
}

tutorial/datagen_tags.txt · Last modified: 2024/10/15 19:51 by skycatminepokie