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/09/16 16:26] – [ADDING BLOCK LOOT] nexus-dino | tutorial:datagen_loot [2025/04/01 13:47] (current) – solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | Before reading this, make sure you have a class that implements '' | + | ====== Loot Table Generation ====== |
- | To get started, make a class (or a few, you need one for blocks, chests | + | 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. Before reading this, make sure you've read [[datagen_setup|Getting started with Data Generation]], and have a class that implements |
- | ==== Setting Up ==== | + | ===== Block loot tables ===== |
- | <code java> | + | To add a data generation for block loot tables, just create a class which extends |
- | private static | + | |
- | public MyBlockLootTables(FabricDataGenerator dataGenerator) { | + | |
- | | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public void accept(BiConsumer< | + | |
- | // ... | + | |
- | } | + | |
- | } | + | |
- | // ... | + | <code java TutorialBlockLootTableProvider.java> |
+ | public class TutorialBlockLootTableProvider extends FabricBlockLootTableProvider { | ||
+ | protected TutorialBlockLootTableProvider(FabricDataOutput dataOutput, CompletableFuture< | ||
+ | super(dataOutput, | ||
+ | } | ||
+ | @Override | ||
+ | public void generate() { | ||
+ | // ... | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 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(); | ||
- | @Override | + | pack.addProvider(TutorialBlockLootTableProvider::new); |
- | 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 | + | < |
+ | | ||
+ | | ||
+ | addDrop(TutorialBlocks.EXAMPLE_BLOCK); | ||
+ | } | ||
+ | </ | ||
- | public static final Item TEST_ITEM = Registry.register(Registry.ITEM, new Identifier(" | + | 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. |
- | // Let's just ignore the fact that there isn' | + | |
+ | You can also make it drop a little more complex, for example | ||
+ | <code java> | ||
+ | // drops nothing | ||
+ | addDrop(TutorialBlocks.EXAMPLE_BLOCK, dropsNothing()); | ||
+ | |||
+ | // drops a dirt block | ||
+ | addDrop(TutorialBlocks.EXAMPLE_BLOCK, | ||
+ | |||
+ | // drops itself only when with tools with Silk Touch, otherwise drops nothing | ||
+ | addDropWithSilkTouch(TutorialBlocks.EXAMPLE_BLOCK); | ||
+ | |||
+ | | ||
+ | addDropWithSilkTouch(TutorialBlocks.EXAMPLE_BLOCK, | ||
+ | |||
+ | // drops itself only when with shears or tools with Silk Touch, otherwise drops nothing | ||
+ | addDrop(TutorialBlocks.EXAMPLE_BLOCK, | ||
+ | |||
+ | // drops five blocks | ||
+ | addDrop(TutorialBlocks.EXAMPLE_BLOCK, | ||
+ | .apply(SetCountLootFunction.builder(ConstantLootNumberProvider.create(5)))); | ||
</ | </ | ||
- | ==== ADDING BLOCK LOOT ==== | + | You can also generate loot tables for the [[crops]] we introduced before: |
<code java> | <code java> | ||
- | private static class MyBlockLootTables extends SimpleFabricLootTableProvider { | + | addDrop(TutorialBlocks.CUSTOM_CROP, |
- | public MyBlockLootTables(FabricDataGenerator dataGenerator) { | + | |
- | super(dataGenerator, LootContextTypes.BLOCK); | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public void accept(BiConsumer< | + | |
- | // The BlockLootTableGenerator class contains a behemoth of utility methods. Just take some time and go through the methods available to override. | + | |
- | biConsumer.accept(new Identifier(" | + | |
- | } | + | |
- | } | + | |
</ | </ | ||
- | Now that we successfully adding a block. Now let's add chest loot. | + | ===== Simple Loot Table ===== |
- | ==== ADDING | + | 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, | ||
+ | } | ||
- | Firstly, we need an identifier. | + | public static final RegistryKey< |
- | <code java> | + | @Override |
- | // In Tutorial class | + | public void accept(BiConsumer< |
- | public static final Identifier TEST_CHEST = new Identifier(" | + | |
+ | .pool(LootPool.builder().rolls(ConstantLootNumberProvider.create(1.0F)) | ||
+ | .with(ItemEntry.builder(Items.DIAMOND) | ||
+ | .apply(SetCountLootFunction.builder(ConstantLootNumberProvider.create(1.0F)))) | ||
+ | .with(ItemEntry.builder(Items.DIAMOND_SWORD))) | ||
+ | | ||
+ | } | ||
+ | } | ||
</ | </ | ||
- | Let's create a chest loot table now. | + | And then in your entry point: |
+ | <code java ExampleModDataGenerator.java> | ||
+ | public class ExampleModDataGenerator implements DataGeneratorEntrypoint { | ||
+ | @Override | ||
+ | public void onInitializeDataGenerator(FabricDataGenerator generator) { | ||
+ | // ... | ||
- | <code java> | + | pack.addProvider(TutorialChestLootTableProvider:: |
- | + | } | |
- | public class MyChestLootGenerator extends ChestLootTableGenerator { | + | |
- | @Override | + | |
- | public void accept(BiConsumer< | + | |
- | biConsumer.accept(Tutorial.TEST_CHEST, | + | |
- | .pool(LootPool.builder().rolls(ConstantLootNumberProvider.create(1.0F)) | + | |
- | .with(ItemEntry.builder(Items.DIAMOND) | + | |
- | .apply(SetCountLootFunction.builder(ConstantLootNumberProvider.create(1.0F))) | + | |
- | .with(ItemEntry.builder(Items.DIAMOND_SWORD)).apply(EnchantWithLevelsLootFunction.create(UniformLootNumberProvider.create(20.0F, | + | |
- | | + | |
- | } | + | |
} | } | ||
- | |||
</ | </ | ||
+ | |||
+ | > 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.1663345616.txt.gz · Last modified: 2022/09/16 16:26 by nexus-dino