Difference between revisions of "Datageneration"

From Forge Community Wiki
(cleanup some style, reword and restructure the first paragraphs)
(Categorize with Category:Data Generation by SizableShrimp#0755)
Line 46: Line 46:
  
 
{{Tip|An <code>AdvancementProvider</code> class does exists, however it is hardcoded for only the vanilla advancements.}}
 
{{Tip|An <code>AdvancementProvider</code> class does exists, however it is hardcoded for only the vanilla advancements.}}
 +
 +
 +
[[Category:Data Generation]]

Revision as of 00:59, 19 May 2021

Data generation (AKA datagen) is a builtin system in Minecraft and Forge that allows mod developers to write code to programmatically generate the assets and data files of mods.

Using datageneration offers some notable benefits:

  • Asset and data files can be automatically generated by code without needing to manually type them out, saving effort and time.
  • The structure of the datagenerated files may be changed without affecting the code which generates them, allowing for files to be cleanly regenerated when the format is changed.
  • Mod developers can mix and match datagenerated resources with pre-existing or manually-made resources.
  • Similar and repeated assets and data files can be programmatically generated for any number of objects, with the least amount of effort needed to define in code.

The data generation system is loaded by the main class net.minecraft.data.Main. Different command-line arguments can be passed to customize which mods’ data are gathered, what existing files are considered, etc. The class responsible for managing and orchestrating data generation is net.minecraft.data.DataGenerator.

The default configurations in the MDK build.gradle adds the runData task for running the data generators.

Generator Modes

The data generator can be configured to run 4 different data generations, which are configured from the command-line parameters, and can be checked from GatherDataEvent#include() methods.

  • Client Assets
  • Server Data
    • Generates server-only files in data: recipes, advancements, tags, etc.
    • --server, includeServer()
  • Development Tools
    • Runs some development tools: converting SNBT to NBT and vice-versa, etc.
    • --dev, includeDev()
  • Reports
    • Dumps all registered blocks, items, commands, etc.
    • --reports, includeReports()

Data Providers

Data providers are the classes that define the resources data to be generated. All data providers implement IDataProvider. Minecraft has abstract implementations for most assets and data, so mod developers only need to extend those abstract classes and override the specified method.

The GatherDataEvent is fired on the mod event bus when the data generator is being created, and the DataGenerator can be obtained from the event. Create and register data providers using DataGenerator#addProvider.

Client Assets

  • net.minecraftforge.common.data.LanguageProvider - for language strings; override #addTranslations
  • ModelProvider<?> - base class for all model providers
    • These classes are under the net.minecraftforge.client.model.generators package
      • ItemModelProvider - for item models; override #registerModels
      • BlockStateProvider - for blockstates and their block and item models; override #registerStatesAndModels
      • BlockModelProvider - for block models; override #registerModels

Server Data

  • These classes are under the net.minecraft.data package
  • LootTableProvider - for loot tables; override #getTables
  • RecipeProvider - for recipes and their unlocking advancements; override #registerRecipes
  • TagsProvider - for tags; override #registerTags
An AdvancementProvider class does exists, however it is hardcoded for only the vanilla advancements.