tutorial:datagen_loot

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:datagen_loot [2022/09/16 16:26] – [ADDING BLOCK LOOT] nexus-dinotutorial:datagen_loot [2025/04/01 13:47] (current) solidblock
Line 1: Line 1:
-Before reading this, make sure you have a class that implements ''DataGenerationEntrypoint''+====== Loot Table Generation ======
  
-To get startedmake a class (or a few, you need one for blockschests and entities) that extends ''SimpleFabricLootTableProvider'' and register it like so:+In this tutorialyou 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 ''DataGenerationEntrypoint''.
  
-==== Setting Up ==== +===== Block loot tables ===== 
-<code java> +To add a data generation for block loot tables, just create a class which extends ''FabricBlockLootTableProvider''.
-private static class MyBlockLootTables extends SimpleFabricLootTableProvider { +
-    public MyBlockLootTables(FabricDataGenerator dataGenerator) { +
-         super(dataGenerator, LootContextTypes.BLOCK); +
-    } +
-     +
-    @Override +
-    public void accept(BiConsumer<Identifier, LootTable.Builder> biConsumer) { +
-        // ... +
-    } +
-}+
  
-// ...+<code java TutorialBlockLootTableProvider.java> 
 +public class TutorialBlockLootTableProvider extends FabricBlockLootTableProvider { 
 +  protected TutorialBlockLootTableProvider(FabricDataOutput dataOutput, CompletableFuture<RegistryWrapper.WrapperLookup> registryLookup) { 
 +    super(dataOutput, registryLookup); 
 +  }
  
 +  @Override
 +  public void generate() {
 +    // ...
 +  }
 +}
 +</code>
 +
 +And then, add this data generation in the entrypoint (we use ''ExampleModDataGenerator'' as an example):
 +<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);+
 } }
 </code> </code>
  
-Let's just create a simple ore block and an item to drop from it for a block loot tableAdd this to your block init or ''Tutorial'' class in this case.+> If you're using versions pre-1.20, please replace ''pack.addProvider(TutorialBlockLootTableProvider::new);'' with ''fabricDataGenerator.addProvider(TutorialBlockLootTableProvider::new);'' and remove ''FabricDataGenerator.Pack myPack = fabricDataGenerator.createPack();''
  
-<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 static final Block TEST_ORE = Registry.register(Registry.BLOCK, new Identifier("tutorial", "test_ore"), new Block(...));+<code java TutorialBlockLootTableProvider.java> 
 +  @Override 
 +  public void generate() 
 +    addDrop(TutorialBlocks.EXAMPLE_BLOCK); 
 +  } 
 +</code>
  
-public static final Item TEST_ITEM = Registry.register(Registry.ITEMnew Identifier("tutorial""test_item", new Item(...)); +This is the most common loot table for a blockWhen 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'a block item 😅+ 
 +You can also make it drop a little more complexfor example (the code is just to show an exmaple; please do not add loot tables for one block multiple times): 
 +<code java> 
 +    // drops nothing 
 +    addDrop(TutorialBlocks.EXAMPLE_BLOCKdropsNothing()); 
 +     
 +    // drops a dirt block 
 +    addDrop(TutorialBlocks.EXAMPLE_BLOCK, Blocks.DIRT); 
 +     
 +    // 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 dirt block 
 +    addDropWithSilkTouch(TutorialBlocks.EXAMPLE_BLOCK, Blocks.DIRT); 
 +     
 +    // drops itself only when with shears or tools with Silk Touch, otherwise drops nothing 
 +    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))));
 </code> </code>
  
-==== 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, cropDrops(TutorialBlocks.CUSTOM_CROPTutorialItems.CUSTOM_ITEMTutorialItems.CUSTOM_SEEDS, BlockStatePropertyLootCondition.builder(TutorialBlocks.CUSTOM_CROP).properties(StatePredicate.Builder.create().exactMatch(CropBlock.AGE7))));
-    public MyBlockLootTables(FabricDataGenerator dataGenerator) { +
-         super(dataGeneratorLootContextTypes.BLOCK); +
-    } +
-  +
-    @Override +
-    public void accept(BiConsumer<IdentifierLootTable.Builder> biConsumer+
-    // The BlockLootTableGenerator class contains a behemoth of utility methodsJust take some time and go through the methods available to override. +
-        biConsumer.accept(new Identifier("tutorial", "test_block"), BlockLootTableGenerator.drops(Tutorial.TEST_BLOCKTutorial.TEST_ITEM, ConstantLootNumberProvider.create(9.0F))); +
-    } +
-}+
 </code> </code>
  
-Now that we successfully adding a block. Now let's add chest loot.+===== Simple Loot Table =====
  
-==== ADDING CHEST LOOT ====+To add a simple loot table, which can also be used in many occasions, such as chests, we just extend ''SimpleFabricLootTableProvider''
 +<code java TutorialChestLootTableProvider.java> 
 +public class TutorialChestLootTableProvider extends SimpleFabricLootTableProvider { 
 +  public TutorialChestLootTableProvider(FabricDataOutput output, CompletableFuture<RegistryWrapper.WrapperLookup> registryLookup) { 
 +    super(output, registryLookup, LootContextTypes.CHEST); 
 +  }
  
-Firstlywe need an identifier.+  public static final RegistryKey<LootTable> TEST_CHEST = RegistryKey.of(RegistryKeys.LOOT_TABLEIdentifier.of("tutorial", "test_chest"));
  
-<code java+  @Override 
-// In Tutorial class +  public void accept(BiConsumer<RegistryKey<LootTable>, LootTable.Builder> lootTableBiConsumer) { 
-public static final Identifier TEST_CHEST = new Identifier("tutorial""chests/test_loot");+    lootTableBiConsumer.accept(TEST_CHESTLootTable.builder() 
 +        .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))) 
 +    ); 
 +  } 
 +}
 </code> </code>
  
-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::new); 
- +  }
-public class MyChestLootGenerator extends ChestLootTableGenerator { +
-    @Override +
-    public void accept(BiConsumer<Identifier, LootTable.Builder> biConsumer) { +
-        biConsumer.accept(Tutorial.TEST_CHEST, LootTable.builder() +
-                  .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, 39.0F)))) +
-        ); +
-    }+
 } }
- 
 </code> </code>
 +
 +> If you're using versions pre-1.20, please replace ''pack.addProvider(TutorialChestLootTableProvider::new);'' with ''fabricDataGenerator.addProvider(TutorialChestLootTableProvider::new);'', and remove ''FabricDataGenerator.Pack myPack = fabricDataGenerator.createPack();''.
 +
 +Now run the data generator, then start Minecraft and run the command ''/loot give @s loot tutorial:test_chest'' multiple times. See what happens! You can also try running the command ''/setblock ~ ~ ~ chest{LootTable:"tutorial:test_chest"}'' and open the chest.
tutorial/datagen_loot.1663345616.txt.gz · Last modified: 2022/09/16 16:26 by nexus-dino