After the previous tutorial your blocks render correctly in-game, but breaking them drops nothing. Minecraft requires a loot table for each block to know what items to drop. In this tutorial we generate those loot tables automatically using datagen.
GatherDataEvent listener alongside the client-side one we already have.Block Loot Provider
In your NeoForge data package, create a class called ExampleBlockLootTableProvider extending BlockLootSubProvider:
The generate() method is where you define what each block drops. Right now we just call dropSelf so the block drops itself when broken with the correct tool.
getKnownBlocks() tells the generator which blocks this provider is responsible for. Using BlockRegistry.BLOCKS.getEntries() means every block you register will be validated automatically. If you forget to add a loot table for a block, datagen will throw an error rather than silently producing an incomplete jar.
Registering the Provider
Loot tables are server-side resources, so they are registered through GatherDataEvent rather than GatherDataEvent.Client. Add a second listener in your NeoForge mod class constructor:
Then add the static handler method. The loot provider is wrapped inside LootTableProvider, which handles the file writing and validation:
The CompletableFuture<HolderLookup.Provider> obtained from the event is what the loot provider needs to resolve datapack objects at generation time.
generate(). Common helpers include:dropSelf(block): drops the block itselfcreateOreDrop(block, item): drops an ore's yield, affected by FortunecreateSlabItemTable(block): correctly doubles the drop count for slabs
Running Datagen
Run the NeoForge Data configuration. After it finishes, check the common/src/generated/resources/data/examplemod/loot_table/blocks/ folder. You should see a new_dirt.json file that looks like this:
loot_table (singular), not loot_tables. This is the correct path for 26.1.Testing In-Game
Launch the client and place a New Dirt block in survival mode. Break it; it should now drop itself. Try breaking it with a Silk Touch pickaxe to confirm the survives_explosion condition does not affect normal drops.
You can find the source for this tutorial here:
View Source on GitHubData Generation: Crafting Recipes (MultiLoader 26.1+)
Create a RecipeProvider to generate shaped crafting recipes, shapeless recipes, and smelting and blasting recipes via datagen, hooking everything into GatherDataEvent alongside your existing providers.
Continue →