Manually writing JSON model and blockstate files is tedious and error-prone. NeoForge's data generation system can produce these files automatically. In this tutorial we configure it to output directly into the common project so Fabric picks them up too.
NEW_DIRT block and IRON_STICK item from those tutorials.Configuring Common Output
By default NeoForge datagen writes into a generated folder inside the NeoForge subproject. We want the output in common instead, so both loaders can reference the same files.
Open the common subproject's build.gradle and make two changes. First, add the generated resources directory as a source set before the dependencies block:
Second, update the artifacts block so the common jar includes all resource directories (not just the first one):
Updating NeoForge Data Config
Open the neoforge subproject's build.gradle and update the data run configuration block so it outputs to common and uses your existing resources to resolve conflicts:
The --output flag routes generated files into common, and --existing tells the generator to resolve any file conflicts against your manually authored resources.
Hit the Gradle refresh button after saving both files.
BlockState Provider
In your NeoForge subproject, create a data package and add a class called ExampleBlockStateProvider extending BlockStateProvider:
simpleBlockWithItem generates three things at once: the blockstate JSON, the block model JSON, and the item model JSON that inherits the block model. The cubeAll call creates a cube_all model using the block's registry name as the texture path.
name() and key() helpers, as they come in handy when you need to build custom model paths for more complex blocks later.Item Model Provider
In the same data package, create ExampleItemModelProvider extending ItemModelProvider:
basicItem generates a minecraft:item/generated model JSON for flat 2D items. Add one basicItem call per item that is not a BlockItem (block items are already handled by the BlockState provider above).
Registering the Providers
In your NeoForge mod main class constructor, register a listener for GatherDataEvent:
Then add the static handler method:
Running Datagen
Before running the generator, delete the existing hand-authored blockstates/ and models/ directories from your common project's resources (but keep your textures, as these are not generated).
Open the Run Configurations dropdown and select NeoForge Data. After it completes successfully you will see a generated (or generated (main)) folder appear in your common project containing the blockstate, block model and item model files:
In the next tutorial we will use the same datagen setup to generate block loot tables so your blocks actually drop items when broken.
You can find the source for this tutorial here:
View Source on GitHubData Generation: Block Loot Tables (MultiLoader 1.21+)
Write a BlockLootSubProvider that makes your custom blocks drop themselves, add it to a LootTableProvider, register everything through GatherDataEvent, and verify drops in-game.
Continue →