tutorial:datagen_loot
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:datagen_loot [2022/12/21 06:58] – Fixed header jmanc3 | tutorial:datagen_loot [2025/04/01 13:47] (current) – solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Loot Table Generation ====== | ====== Loot Table Generation ====== | ||
- | Before reading this, make sure you've read [[datagen_setup|Getting started with Data Generation]], | + | In this tutorial, you will learn how to generate loot tables for blocks and other contents. With data generation, you do not need to write JSONs for each block anymore. |
- | To begin, make a class (or a few, you need one for blocks, chests and entities) that extends '' | + | ===== Block loot tables ===== |
+ | To add a data generation for block loot tables, just create | ||
- | ==== Setting Up ==== | + | <code java TutorialBlockLootTableProvider.java> |
- | To get started with block loot, create a block loot table generator | + | public class TutorialBlockLootTableProvider extends FabricBlockLootTableProvider { |
+ | protected TutorialBlockLootTableProvider(FabricDataOutput dataOutput, CompletableFuture< | ||
+ | super(dataOutput, | ||
+ | } | ||
- | <code java> | + | |
- | private static class MyBlockLootTables extends SimpleFabricLootTableProvider { | + | public void generate() { |
- | public MyBlockLootTables(FabricDataGenerator dataGenerator) { | + | // ... |
- | | + | } |
- | } | + | |
- | + | ||
- | | + | |
- | public void accept(BiConsumer< | + | |
- | // ... | + | |
- | } | + | |
} | } | ||
+ | </ | ||
- | // ... | + | And then, add this data generation in the entrypoint (we use '' |
+ | <code java ExampleModDataGenerator.java> | ||
+ | public class ExampleModDataGenerator implements DataGeneratorEntrypoint { | ||
+ | @Override | ||
+ | public void onInitializeDataGenerator(FabricDataGenerator generator) { | ||
+ | FabricDataGenerator.Pack pack = generator.createPack(); | ||
- | + | pack.addProvider(TutorialBlockLootTableProvider::new); | |
- | @Override | + | } |
- | public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) { | + | |
- | fabricDataGenerator.addProvider(MyBlockLootTables::new); | + | |
} | } | ||
</ | </ | ||
- | Let's just create a simple ore block and an item to drop from it for a block loot table. Add this to your block init or '' | + | > If you're using versions pre-1.20, please replace |
- | <code java> | + | In the previous [[blocks]] tutorial, we created an //example block//. Now let's use the data generator to generate its loot table. It's as simple as: |
- | public | + | < |
- | + | | |
- | public static final Item TEST_ITEM = Registry.register(Registry.ITEM, | + | |
- | // Let's just ignore the fact that there isn't a block item 😅 | + | |
+ | } | ||
</ | </ | ||
- | ==== Adding Block Loot ==== | + | This is the most common loot table for a block. When mined, drops one. When exploded in a explosion with decay (such as creeper and nether bed), it may be destroyed totally and drop nothing. |
+ | |||
+ | You can also make it drop a little more complex, for example (the code is just to show an exmaple; please do not add loot tables for one block multiple times): | ||
<code java> | <code java> | ||
- | private static class MyBlockLootTables extends SimpleFabricLootTableProvider { | + | // drops nothing |
- | | + | |
- | super(dataGenerator, LootContextTypes.BLOCK); | + | |
- | | + | // drops a dirt block |
- | + | addDrop(TutorialBlocks.EXAMPLE_BLOCK, Blocks.DIRT); | |
- | | + | |
- | | + | |
- | // The BlockLootTableGenerator class contains | + | |
- | | + | |
- | } | + | // drops itself only when with tools with Silk Touch, otherwise drops a dirt block |
- | } | + | |
+ | |||
+ | // drops itself only when with shears or tools with Silk Touch, otherwise | ||
+ | addDrop(TutorialBlocks.EXAMPLE_BLOCK, dropsWithSilkTouchOrShears(TutorialBlocks.EXAMPLE_BLOCK)); | ||
+ | |||
+ | // drops five blocks | ||
+ | addDrop(TutorialBlocks.EXAMPLE_BLOCK, drops(TutorialBlocks.EXAMPLE_BLOCK) | ||
+ | .apply(SetCountLootFunction.builder(ConstantLootNumberProvider.create(5)))); | ||
</ | </ | ||
- | Now that we successfully adding a block. Now let's add chest loot. | + | You can also generate |
- | + | ||
- | ==== Adding Chest Loot ==== | + | |
- | + | ||
- | Firstly, | + | |
<code java> | <code java> | ||
- | // In Tutorial class | + | addDrop(TutorialBlocks.CUSTOM_CROP, cropDrops(TutorialBlocks.CUSTOM_CROP, |
- | public static final Identifier TEST_CHEST = new Identifier(" | + | |
</ | </ | ||
- | Let's create a chest loot table generator and register it like so. | + | ===== Simple Loot Table ===== |
- | <code java> | + | To add a simple loot table, which can also be used in many occasions, such as chests, we just extend '' |
+ | <code java TutorialChestLootTableProvider.java> | ||
+ | public class TutorialChestLootTableProvider extends SimpleFabricLootTableProvider { | ||
+ | public TutorialChestLootTableProvider(FabricDataOutput output, CompletableFuture< | ||
+ | super(output, | ||
+ | } | ||
- | private static class MyChestLootTables extends SimpleFabricLootTableProvider { | + | |
- | | + | |
- | super(dataGenerator, LootContextTypes.CHEST); | + | |
- | } | + | |
- | | + | |
- | public void accept(BiConsumer< | + | public void accept(BiConsumer< |
- | | + | |
- | .pool(LootPool.builder().rolls(ConstantLootNumberProvider.create(1.0F)) | + | .pool(LootPool.builder().rolls(ConstantLootNumberProvider.create(1.0F)) |
- | .with(ItemEntry.builder(Items.DIAMOND) | + | .with(ItemEntry.builder(Items.DIAMOND) |
- | .apply(SetCountLootFunction.builder(ConstantLootNumberProvider.create(1.0F))) | + | .apply(SetCountLootFunction.builder(ConstantLootNumberProvider.create(1.0F)))) |
- | .with(ItemEntry.builder(Items.DIAMOND_SWORD)).apply(EnchantWithLevelsLootFunction.create(UniformLootNumberProvider.create(20.0F, | + | .with(ItemEntry.builder(Items.DIAMOND_SWORD))) |
- | ); | + | ); |
- | } | + | } |
} | } | ||
+ | </ | ||
- | // ... | + | And then in your entry point: |
+ | <code java ExampleModDataGenerator.java> | ||
+ | public class ExampleModDataGenerator implements DataGeneratorEntrypoint { | ||
+ | @Override | ||
+ | public void onInitializeDataGenerator(FabricDataGenerator generator) { | ||
+ | | ||
- | @Override | + | pack.addProvider(TutorialChestLootTableProvider::new); |
- | public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) { | + | } |
- | fabricDataGenerator.addProvider(MyChestLootTables::new); | + | |
} | } | ||
</ | </ | ||
+ | |||
+ | > If you're using versions pre-1.20, please replace '' | ||
+ | |||
+ | Now run the data generator, then start Minecraft and run the command ''/ |
tutorial/datagen_loot.1671605896.txt.gz · Last modified: 2022/12/21 06:58 by jmanc3