Skip to main content
Version: 1.20.x

Tag Generation

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

// On the MOD event bus
@SubscribeEvent
public void gatherData(GatherDataEvent event) {
event.getGenerator().addProvider(
// Tell generator to run only when server data are generating
event.includeServer(),
// Extends net.minecraftforge.common.data.BlockTagsProvider
output -> new MyBlockTagsProvider(
output,
event.getLookupProvider(),
MOD_ID,
event.getExistingFileHelper()
)
);
}

TagsProvider

The tags provider has two methods used for generating tags: creating a tag with objects and other tags via #tag, or using tags from other object types to generate the tag data via #getOrCreateRawBuilder.

!!! note Typically, a provider will not call #getOrCreateRawBuilder directly unless a registry contains a representation of objects from a different registry (blocks have item representations to obtain the blocks in the inventory).

When #tag is called, a TagAppender is created which acts as a chainable consumer of elements to add to the tag:

MethodDescription
addAdds an object to a tag through its resource key.
addOptionalAdds an object to a tag through its name. If the object is not present, then the object will be skipped when loading.
addTagAdds a tag to a tag through its tag key. All elements within the inner tag are now a part of the outer tag.
addOptionalTagAdds a tag to a tag through its name. If the tag is not present, then the tag will be skipped when loading.
replaceWhen 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.
removeRemoves an object or tag from a tag through its name or key.
// 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.

Existing Providers

Minecraft contains a few tag providers for certain registries that can be subclassed instead. Additionally, some providers contain additional helper methods to more easily create tags.

Registry Object TypeTag Provider
BlockBlockTagsProvider*
ItemItemTagsProvider
EntityTypeEntityTypeTagsProvider
FluidFluidTagsProvider
GameEventGameEventTagsProvider
BiomeBiomeTagsProvider
FlatLevelGeneratorPresetFlatLevelGeneratorPresetTagsProvider
WorldPresetWorldPresetTagsProvider
StructureStructureTagsProvider
PoiTypePoiTypeTagsProvider
BannerPatternBannerPatternTagsProvider
CatVariantCatVariantTagsProvider
PaintingVariantPaintingVariantTagsProvider
InstrumentInstrumentTagsProvider
DamageTypeDamageTypeTagsProvider

* BlockTagsProvider is a Forge added TagsProvider.

ItemTagsProvider#copy

Blocks have item representations to obtain them in the inventory. As such, many of the block tags can also be an item tag. To easily generate item tags to have the same entries as block tags, the #copy method can be used which takes in the block tag to copy from and the item tag to copy to.

//In ItemTagsProvider#addTags
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 key to generate tags for.

public RecipeTypeTagsProvider(PackOutput output, CompletableFuture<HolderLookup.Provider> registries, ExistingFileHelper fileHelper) {
super(output, Registries.RECIPE_TYPE, registries, MOD_ID, fileHelper);
}

Intrinsic Holder Tags Providers

One special type of TagProviders are IntrinsicHolderTagsProviders. When creating a tag using this provider via #tag, the object itself can be used to add itself to the tag via #add. To do so, a function is provided within the constructor to turn an object into its ResourceKey.

// Subtype of `IntrinsicHolderTagsProvider`
public AttributeTagsProvider(PackOutput output, CompletableFuture<HolderLookup.Provider> registries, ExistingFileHelper fileHelper) {
super(
output,
ForgeRegistries.Keys.ATTRIBUTES,
registries,
attribute -> ForgeRegistries.ATTRIBUTES.getResourceKey(attribute).get(),
MOD_ID,
fileHelper
);
}