Difference between revisions of "Datageneration"

From Forge Community Wiki
(Intial Import dokuwiki)
 
(cleanup some style, reword and restructure the first paragraphs)
Line 1: Line 1:
Data generators are a way to programmatically generate the assets and data of mods. It allows the definition of the contents of these files in the code and their automatic generation, without worrying about the specifics.
+
'''Data generation''' (AKA '''datagen''') is a builtin system in Minecraft and Forge that allows mod developers to write code to programmatically generate the [[Using Resources|assets and data]] files of mods.  
  
The data generator system is loaded by the main class <code>net.minecraft.data.Main</code>. Different command-line arguments can be passed to customize which mods’ data are gathered, what existing files are considered, etc. The class responsible for data generation is <code>net.minecraft.data.DataGenerator</code>.
+
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 <code>net.minecraft.data.Main</code>. 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 <code>net.minecraft.data.DataGenerator</code>.
  
 
The default configurations in the MDK <code>build.gradle</code> adds the <code>runData</code> task for running the data generators.
 
The default configurations in the MDK <code>build.gradle</code> adds the <code>runData</code> task for running the data generators.
Line 7: Line 13:
 
== Generator Modes ==
 
== 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 <code><nowiki>GatherDataEvent#include()</nowiki></code> methods.
 
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 <code><nowiki>GatherDataEvent#include()</nowiki></code> methods.
* <code>Client Assets</code>
+
* '''Client Assets'''
** Generates client-only files in <code>assets</code>: <code>block</code>/<code>item</code> models, <code>blockstate JSON</code>s, language files, etc.
+
** Generates client-only files in <tt>assets</tt>: [[Introduction to Models|block/item models]], [[BlockState JSONs|blockstate JSONs]], [[Datageneration/I18n|language files]], etc.
 
** <code><nowiki>--client</nowiki></code>, <code><nowiki>includeClient()</nowiki></code>
 
** <code><nowiki>--client</nowiki></code>, <code><nowiki>includeClient()</nowiki></code>
* <code>Server Data</code>
+
* '''Server Data'''
** Generates server-only files in <code>data</code>: <code>recipes</code>, <code>advancements</code>, <code>tags</code>, etc.
+
** Generates server-only files in <tt>data</tt>: [[Datageneration/Recipes|recipes]], advancements, [[Datageneration/Tags|tags]], etc.
 
** <code><nowiki>--server</nowiki></code>, <code><nowiki>includeServer()</nowiki></code>
 
** <code><nowiki>--server</nowiki></code>, <code><nowiki>includeServer()</nowiki></code>
* <code>Development Tools</code>
+
* '''Development Tools'''
 
** Runs some development tools: converting SNBT to NBT and vice-versa, etc.
 
** Runs some development tools: converting SNBT to NBT and vice-versa, etc.
 
** <code><nowiki>--dev</nowiki></code>, <code>includeDev()</code>
 
** <code><nowiki>--dev</nowiki></code>, <code>includeDev()</code>
* <code>Reports</code>
+
* '''Reports'''
 
** Dumps all registered blocks, items, commands, etc.
 
** Dumps all registered blocks, items, commands, etc.
 
** <code><nowiki>--reports</nowiki></code>, <code>includeReports()</code>
 
** <code><nowiki>--reports</nowiki></code>, <code>includeReports()</code>
  
 
== Data Providers ==
 
== Data Providers ==
Data providers are the classes that actually define what data will be generated and provided. All data providers implement <code>IDataProvider</code>. Minecraft has abstract implementations for most assets and data, so modders need only to extend and override the specified method.
+
'''Data providers''' are the classes that define the resources data to be generated. All data providers implement <code>IDataProvider</code>. 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 <code>GatherDataEvent</code> is fired on the mod event bus when the data generator is being created, and the <code>DataGenerator</code> can be obtained from the event. Create and register data providers using <code><nowiki>DataGenerator#addProvider</nowiki></code>.
 
The <code>GatherDataEvent</code> is fired on the mod event bus when the data generator is being created, and the <code>DataGenerator</code> can be obtained from the event. Create and register data providers using <code><nowiki>DataGenerator#addProvider</nowiki></code>.
 +
 
=== Client Assets ===
 
=== Client Assets ===
 
* <code>net.minecraftforge.common.data.LanguageProvider</code> - for language strings; override <code><nowiki>#addTranslations</nowiki></code>
 
* <code>net.minecraftforge.common.data.LanguageProvider</code> - for language strings; override <code><nowiki>#addTranslations</nowiki></code>
Line 38: Line 45:
 
* <code>TagsProvider</code> - for tags; override <code>#registerTags</code>
 
* <code>TagsProvider</code> - for tags; override <code>#registerTags</code>
  
{{Colored box|title=Tip|content=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.}}

Revision as of 04:29, 2 January 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.