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_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(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 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:

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.txt · Last modified: 2022/09/10 16:51 by nexus-dino