Datageneration/Tags

Tags can be generated for a mod by subclassing TagsProvider and implementing #addTags. After implementation, the provider must be added to the DataGenerator.

TagsProvider

The tags provider typically generates tags via #tag.

There is another method called #getOrCreateRawBuilder, but this is typically used only in cases when two registries represent the same object but in different forms (block and items).

When #tag is called, a builder is created to which the following methods can be chained:

Method Description
#add Adds an object to a tag.
#addOptional Adds an object to a tag through its name. If the object is not present, then the object will be skipped when loading.
#addTag Adds a tag to a tag. All elements within the inner tag are now a part of the outer tag.
#addOptionalTag Adds a tag to a tag through its name. If the tag is not present, then the tag will be skipped when loading.
#replace When true, all previously loaded entries added to this tag from other datapacks will be discarded. If a datapack is loaded after this one, then it will still append the entries to the tag.
#remove Removes an object or all objects within a tag from a tag. If the object is not present in the tag, nothing happens.
// In some TagProvider#addTags
this.tag(EXAMPLE_TAG)
  .add(EXAMPLE_OBJECT) // Adds an object to the tag
  .addOptional(new ResourceLocation("othermod", "other_object")); // Adds an object from another mod to the tag

this.tag(EXAMPLE_TAG_2)
  .addTag(EXAMPLE_TAG) // Adds a tag to the tag
  .remove(EXAMPLE_OBJECT); // Removes an object from this tag


Important

If the mod's tags softly depends on another mod's tags (the other mod may or may not be present at runtime), the other mods' tags should be referenced using the optional methods.

Using Existing Tag Providers

Vanilla contains a few tag providers for certain registries that can be subclassed for ease of implementation.

Registry Object Type Tag Provider
Block BlockTagsProvider
Item ItemTagsProvider
EntityType EntityTypeTagsProvider
Fluid FluidTagsProvider
GameEvent GameEventTagsProvider
Biome BiomeTagsProvider
FlatLevelGeneratorPreset FlatLevelGeneratorPresetTagsProvider
WorldPreset WorldPresetTagsProvider
Structure StructureTagsProvider
PoiType PoiTypeTagsProvider
BannerPattern BannerPatternTagsProvider
CatVariant CatVariantTagsProvider
PaintingVariant PaintingVariantTagsProvider
Instrument InstrumentTagsProvider
public class ExampleBlockTagsProvider extends BlockTagsProvider {
    
    public ExampleBlockTagsProvider(DataGenerator gen, String modId, ExistingFileHelper efh) {
        // ...
    }

    @Override
    public void addTags() {
        // Add block tags here
    }
}


ItemTagsProvider#copy

Blocks have item representations such that they can be obtained within an inventory. As such, a number of block tags can also be applied to a similar item tag. To easily generate an item tag with the same entries as a block tag, the #copy method can be used.

// In #addTags within a ItemTagsProvider subclass
this.copy(EXAMPLE_BLOCK_TAG, EXAMPLE_ITEM_TAG);


Custom Tag Providers

A custom tag provider can be created via a TagsProvider subclass which takes in the Registry to generate tags for.

public RecipeTypeTagsProvider(DataGenerator gen, String modId, ExistingFileHelper efh) {
  super(gen, Registry.RECIPE_TYPE, modId, efh);
}


Forge Registry Tag Providers

If a registry is wrapped by Forge or created by a mod, a provider can be created via a ForgeRegistryTagsProvider subclass instead:

public AttributeTagsProvider(DataGenerator gen, String modId, ExistingFileHelper efh) {
  super(gen, ForgeRegistries.ATTRIBUTES, modId, efh);
}