User Tools

Site Tools


tutorial:datagen_loot

Differences

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

Link to this comparison view

Next revision
Previous revision
tutorial:datagen_loot [2022/09/14 20:23] – created nexus-dinotutorial:datagen_loot [2023/06/16 17:29] (current) – 1.20+ addProvider change slainlight
Line 1: Line 1:
-Before reading this, make sure you have a class that implements ''DataGenerationEntrypoint''+====== Loot Table Generation ======
  
-To get started, make a class (or a few, you need one for blocks, chests and entities) that extends ''SimpleFabricLootTableProvider'' and register it like so:+Before reading this, make sure you've read [[datagen_setup|Getting started with Data Generation]], and have a class that implements ''DataGenerationEntrypoint'' 
 + 
 +To begin, make a class (or a few, you need one for blocks, chests and entities) that extends ''SimpleFabricLootTableProvider'' and register it like so:
  
 ==== Setting Up ==== ==== Setting Up ====
 +To get started with block loot, create a block loot table generator
 +
 <code java> <code java>
-private static class MyBlockLootTables extends SimpleFabricLootTableProvider +private static class MyBlockLootTables extends FabricBlockLootTableProvider 
-    public MyBlockLootTables(FabricDataGenerator dataGenerator) { +    public MyBlockLootTables(FabricDataOutput dataOutput) { 
-         super(dataGenerator, LootContextTypes.BLOCK);+         super(dataOutput);
     }     }
          
     @Override     @Override
-    public void accept(BiConsumer<Identifier, LootTable.Builder> biConsumer) {+    public void generate() {
         // ...         // ...
     }     }
Line 21: Line 25:
 @Override @Override
 public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) { public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) {
-    fabricDataGenerator.addProvider(MyBlockLootTables::new);+    // ... 
 +    FabricDataGenerator.Pack myPack = fabricDataGenerator.createPack(); 
 +    myPack.addProvider(MyBlockLootTables::new); 
 +    // ...
 } }
 </code> </code>
 +
 +If you're using versions pre-1.20, please replace ''myPack.addProvider(MyBlockLootTables::new);'' with ''fabricDataGenerator.addProvider(MyBlockLootTables::new);''。You can also remove ''FabricDataGenerator.Pack myPack = fabricDataGenerator.createPack();''
  
 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 ''Tutorial'' class in this case. 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 ''Tutorial'' class in this case.
Line 34: Line 43:
 </code> </code>
  
-==== ADDING BLOCK LOOT ====+==== Adding Block Loot ====
 <code java> <code java>
-private static class MyBlockLootTables extends SimpleFabricLootTableProvider +private static class MyBlockLootTables extends FabricBlockLootTableProvider 
-    public MyBlockLootTables(FabricDataGenerator dataGenerator) { +    public MyBlockLootTables(FabricDataOutput dataOutput) { 
-         super(dataGenerator, LootContextTypes.BLOCK);+         super(dataOutput);
     }     }
- +    
     @Override     @Override
-    public void accept(BiConsumer<Identifier, LootTable.Builder> biConsumer) { +    public void generate() { 
-    // The BlockLootTableGenerator class contains a behemoth of utility methods. Just take some time and go through the methods available to override. +        addDrop(Tutorial.TEST_BLOCK, drops(Tutorial.TEST_ITEM));
-        biConsumer.accept(new Identifier("tutorial", "test_block"), BlockLootTableGenerator.drops(Tutorial.TEST_BLOCK, Tutorial.TEST_ITEM, ConstantLootNumberProvider.create(9.0F)));+
     }     }
 } }
Line 51: Line 59:
 Now that we successfully adding a block. Now let's add chest loot. Now that we successfully adding a block. Now let's add chest loot.
  
-Firstly, we need an identifier.+==== Adding Chest Loot ==== 
 + 
 +Firstly, we need an identifier. This identifier points to a json file that contains your chest loot.
  
 <code java> <code java>
Line 58: Line 68:
 </code> </code>
  
-TODO+Let's create a chest loot table generator and register it like so. 
 + 
 +<code java> 
 + 
 +private static class MyChestLootTables extends SimpleFabricLootTableProvider { 
 +    public MyChestLootGenerator(FabricDataOutput dataGenerator) { 
 + super(dataGenerator, LootContextTypes.CHEST); 
 +    } 
 + 
 +    @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)))) 
 +        ); 
 +    } 
 +
 + 
 +// ... 
 + 
 +@Override 
 +public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) { 
 +    FabricDataGenerator.Pack myPack = fabricDataGenerator.createPack(); 
 +    myPack.addProvider(MyChestLootTables::new); 
 +
 +</code> 
 + 
 +If you're using versions pre-1.20, please replace ''myPack.addProvider(MyBlockLootTables::new);'' with ''fabricDataGenerator.addProvider(MyBlockLootTables::new);''。You can also remove ''FabricDataGenerator.Pack myPack = fabricDataGenerator.createPack();''
tutorial/datagen_loot.1663186993.txt.gz · Last modified: 2022/09/14 20:23 by nexus-dino