Views
Actions
Difference between revisions of "Datageneration/Tags"
(Update to 1.18.2) |
(Finish tag providers) |
||
Line 1: | Line 1: | ||
− | + | Tags can be generated for a mod by subclassing <code>TagsProvider</code> and implementing <code>#addTags</code>. After implementation, the provider must be added to the <code>DataGenerator</code>. | |
− | + | = TagsProvider = | |
+ | The tags provider typically generates tags via <code>#tag</code>. | ||
− | {{ | + | {{Tip|There is another method called <code>#getOrCreateRawBuilder</code>, but this is typically used only in cases when two registries represent the same object but in different forms (block and items).}} |
− | + | When <code>#tag</code> is called, a builder is created to which the following methods can be chained: | |
− | |||
− | |||
− | + | {| class="wikitable sortable" | |
− | + | ! Method !! Description | |
− | + | |- | |
− | tag | + | | <code>#add</code> || Adds an object to a tag. |
− | </ | + | |- |
+ | | <code>#addOptional</code> || Adds an object to a tag through its name. If the object is not present, then the object will be skipped when loading. | ||
+ | |- | ||
+ | | <code>#addTag</code> || Adds a tag to a tag. All elements within the inner tag are now a part of the outer tag. | ||
+ | |- | ||
+ | | <code>#addOptionalTag</code> || Adds a tag to a tag through its name. If the tag is not present, then the tag will be skipped when loading. | ||
+ | |- | ||
+ | | <code>#replace</code> || When <code>true</code>, 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. | ||
+ | |- | ||
+ | | <code>#remove</code> || Removes an object or all objects within a tag from a tag. If the object is not present in the tag, nothing happens. | ||
+ | |} | ||
+ | {{Tabs/Code Snippets| | ||
+ | java=// 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 | ||
+ | }} | ||
− | + | {{Tip/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. | ||
+ | |||
+ | {| class="wikitable sortable" | ||
+ | ! Registry Object Type !! Tag Provider | ||
+ | |- | ||
+ | | <code>Block</code> || <code>BlockTagsProvider</code> | ||
+ | |- | ||
+ | | <code>Item</code> || <code>ItemTagsProvider</code> | ||
+ | |- | ||
+ | | <code>EntityType</code> || <code>EntityTypeTagsProvider</code> | ||
+ | |- | ||
+ | | <code>Fluid</code> || <code>FluidTagsProvider</code> | ||
+ | |- | ||
+ | | <code>GameEvent</code> || <code>GameEventTagsProvider</code> | ||
+ | |- | ||
+ | | <code>Biome</code> || <code>BiomeTagsProvider</code> | ||
+ | |- | ||
+ | | <code>FlatLevelGeneratorPreset</code> || <code>FlatLevelGeneratorPresetTagsProvider</code> | ||
+ | |- | ||
+ | | <code>WorldPreset</code> || <code>WorldPresetTagsProvider</code> | ||
+ | |- | ||
+ | | <code>Structure</code> || <code>StructureTagsProvider</code> | ||
+ | |- | ||
+ | | <code>PoiType</code> || <code>PoiTypeTagsProvider</code> | ||
+ | |- | ||
+ | | <code>BannerPattern</code> || <code>BannerPatternTagsProvider</code> | ||
+ | |- | ||
+ | | <code>CatVariant</code> || <code>CatVariantTagsProvider</code> | ||
+ | |- | ||
+ | | <code>PaintingVariant</code> || <code>PaintingVariantTagsProvider</code> | ||
+ | |- | ||
+ | | <code>Instrument</code> || <code>InstrumentTagsProvider</code> | ||
+ | |} | ||
+ | |||
+ | {{Tabs/Code Snippets| | ||
+ | java=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 <code>#copy</code> method can be used. | ||
+ | |||
+ | {{Tabs/Code Snippets| | ||
+ | java=// 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 <code>TagsProvider</code> subclass which takes in the <code>Registry</code> to generate tags for. | ||
+ | |||
+ | {{Tabs/Code Snippets| | ||
+ | java=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 <code>ForgeRegistryTagsProvider</code> subclass instead: | ||
+ | |||
+ | {{Tabs/Code Snippets| | ||
+ | java=public AttributeTagsProvider(DataGenerator gen, String modId, ExistingFileHelper efh) { | ||
+ | super(gen, ForgeRegistries.ATTRIBUTES, modId, efh); | ||
+ | } | ||
+ | }} | ||
[[Category:Data Generation]] | [[Category:Data Generation]] |
Latest revision as of 22:12, 6 October 2022
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
.
#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
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); }