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:37] – 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() { |
- | public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) { | + | |
- | | + | } |
} | } | ||
</ | </ | ||
- | 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 '' | + | And then, add this data generation in the entrypoint (we use '' |
+ | <code java ExampleModDataGenerator.java> | ||
+ | public | ||
+ | @Override | ||
+ | public void onInitializeDataGenerator(FabricDataGenerator generator) { | ||
+ | FabricDataGenerator.Pack pack = generator.createPack(); | ||
- | <code java> | + | pack.addProvider(TutorialBlockLootTableProvider:: |
- | public static final Block TEST_ORE = Registry.register(Registry.BLOCK, | + | } |
- | + | ||
- | 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 ==== | + | |
- | <code java> | + | |
- | private static class MyBlockLootTables extends SimpleFabricLootTableProvider { | + | |
- | public MyBlockLootTables(FabricDataGenerator dataGenerator) { | + | |
- | | + | |
- | } | + | |
- | + | ||
- | @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. | + | > If you're using versions pre-1.20, please replace |
- | ==== ADDING CHEST LOOT ==== | + | 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: |
+ | <code java TutorialBlockLootTableProvider.java> | ||
+ | @Override | ||
+ | public void generate() { | ||
+ | addDrop(TutorialBlocks.EXAMPLE_BLOCK); | ||
+ | } | ||
+ | </ | ||
- | Firstly, we need an identifier. This identifier points to a json file that contains your chest 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> | ||
- | // In Tutorial class | + | |
- | public static final Identifier TEST_CHEST = new Identifier(" | + | |
+ | |||
+ | | ||
+ | addDrop(TutorialBlocks.EXAMPLE_BLOCK, | ||
+ | |||
+ | // drops itself only when with tools with Silk Touch, otherwise drops nothing | ||
+ | addDropWithSilkTouch(TutorialBlocks.EXAMPLE_BLOCK); | ||
+ | |||
+ | // drops itself only when with tools with Silk Touch, otherwise drops a dirt 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)))); | ||
</ | </ | ||
- | Let's create a chest loot table generator and register it like so. | + | You can also generate |
<code java> | <code java> | ||
+ | addDrop(TutorialBlocks.CUSTOM_CROP, | ||
+ | </ | ||
+ | |||
+ | ===== Simple Loot Table ===== | ||
+ | |||
+ | 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.1663346273.txt.gz · Last modified: 2022/09/16 16:37 by nexus-dino