Difference between revisions of "Datageneration"

From Forge Community Wiki
(Categorize with Category:Data Generation by SizableShrimp#0755)
(37.0.91 fix, advancement providers now work)
 
(2 intermediate revisions by the same user not shown)
Line 27: Line 27:
  
 
== Data Providers ==
 
== Data Providers ==
'''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.
+
'''Data providers''' are the classes that define the resources data to be generated. All data providers implement <code>DataProvider</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>.
Line 38: Line 38:
 
*** <code>BlockStateProvider</code> - for blockstates and their block and item models; override <code>#registerStatesAndModels</code>
 
*** <code>BlockStateProvider</code> - for blockstates and their block and item models; override <code>#registerStatesAndModels</code>
 
*** <code>BlockModelProvider</code> -  for block models; override <code>#registerModels</code>
 
*** <code>BlockModelProvider</code> -  for block models; override <code>#registerModels</code>
 +
* <code>SoundDefinitionsProvider</code> - for the <code>sounds.json</code> file; override <code>#registerSounds</code>
  
 
=== Server Data ===
 
=== Server Data ===
 
* ''These classes are under the <code>net.minecraft.data</code> package''
 
* ''These classes are under the <code>net.minecraft.data</code> package''
 
* <code>LootTableProvider</code> - for loot tables; override <code>#getTables</code>
 
* <code>LootTableProvider</code> - for loot tables; override <code>#getTables</code>
* <code>RecipeProvider</code> - for recipes and their unlocking advancements; override <code>#registerRecipes</code>
+
* <code>RecipeProvider</code> - for recipes and their unlocking advancements; override <code>#buildCraftingRecipes</code>
* <code>TagsProvider</code> - for tags; override <code>#registerTags</code>
+
* <code>TagsProvider</code> - for tags; override <code>#addTags</code>
 
+
* <code>GlobalLootModifierProvider</code> - for global loot modifiers; override <code>#start</code>
{{Tip|An <code>AdvancementProvider</code> class does exists, however it is hardcoded for only the vanilla advancements.}}
+
* <code>AdvancementProvider</code> - for advancements; override <code>#registerAdvancements</code>
  
  
 
[[Category:Data Generation]]
 
[[Category:Data Generation]]

Latest revision as of 18:20, 15 October 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 DataProvider. 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
  • SoundDefinitionsProvider - for the sounds.json file; override #registerSounds

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 #buildCraftingRecipes
  • TagsProvider - for tags; override #addTags
  • GlobalLootModifierProvider - for global loot modifiers; override #start
  • AdvancementProvider - for advancements; override #registerAdvancements