User Tools

Site Tools


tutorial:adding_to_loot_tables

Differences

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

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
tutorial:adding_to_loot_tables [2019/07/29 14:03] – created juuztutorial:adding_to_loot_tables [2022/03/27 07:20] stormyfabric
Line 1: Line 1:
-====== Adding items to loot tables ======+====== Adding items to existing loot tables ======
  
 ===== Introduction ===== ===== Introduction =====
  
-Sometimes you want to add items to loot tables, but replacing the loot table file can break other mods – what if they want to change them as well? We’ll take a look at how you can add items to loot tables without overriding them.+Sometimes you want to add items to [[https://minecraft.gamepedia.com/Loot_table|loot tables]], for example adding your own drops to a vanilla block or entity. The simplest solution, replacing the loot table filecan break other mods – what if they want to change them as well? We’ll take a look at how you can add items to loot tables without overriding the table.
  
 Our example will be adding eggs to the coal ore loot table. Our example will be adding eggs to the coal ore loot table.
  
-===== Listening to loot table events =====+===== Listening to loot table loading =====
  
-Fabric API has an event that’s fired when loot tables are loaded, ''%%LootTableLoadingCallback%%''. You can register an event listener for it in your initializer. Let’s also check that the current loot table is ''%%minecraft:blocks/coal_ore%%''.+Fabric API has an event that’s fired when loot tables are loaded, ''%%LootTableLoadingCallback%%''. You can register an event listener for it in your initializer. Let’s also check that the current loot table is the coal ore loot table.
  
-<code java> +<yarncode java> 
-LootTableLoadingCallback.EVENT.register((resourceManager, lootManager, id, supplier, setter) -> { +// No magic constants! 
-    if ("minecraft:blocks/coal_ore".equals(id.toString()) {+private static final class_2960 COAL_ORE_LOOT_TABLE_ID = class_2246.COAL_ORE.getLootTableId(); 
 + 
 +// Actual code 
 + 
 +LootTableLoadingCallback.EVENT.register((resourceManager, lootManager, id, table, setter) -> { 
 +    if (COAL_ORE_LOOT_TABLE_ID.equals(id)) {
         // Our code will go here         // Our code will go here
     }     }
 }); });
-</code+</yarncode> 
-In loot tables, item are stored in //loot entries,// and entries are stored in //loot pools//. To add an item, we’ll need to add a pool with an item entry to the loot table.+ 
 +===== Adding items to the table ===== 
 + 
 +In loot tables, items are stored in //loot entries,// and entries are stored in //loot pools//. To add an item, we’ll need to add a pool with an item entry to the loot table.
  
 We can make a pool with ''%%FabricLootPoolBuilder%%'', and add it to the loot table: We can make a pool with ''%%FabricLootPoolBuilder%%'', and add it to the loot table:
  
-<code java> +<yarncode java> 
-LootTableLoadingCallback.EVENT.register((resourceManager, lootManager, id, supplier, setter) -> { +LootTableLoadingCallback.EVENT.register((resourceManager, lootManager, id, table, setter) -> { 
-    if ("minecraft:blocks/coal_ore".equals(id.toString()) {+    if (COAL_ORE_LOOT_TABLE_ID.equals(id)) {
         FabricLootPoolBuilder poolBuilder = FabricLootPoolBuilder.builder()         FabricLootPoolBuilder poolBuilder = FabricLootPoolBuilder.builder()
-                .withRolls(ConstantLootTableRange.create(1)); // Same as "rolls": 1 in the loot table json+                .method_352(class_44.method_32448(1)); // Same as "rolls": 1 in the loot table json
  
-        supplier.withPool(poolBuilder);+        table.method_336(poolBuilder);
     }     }
 }); });
-</code>+</yarncode>
 Our pool doesn’t have any items yet, so we’ll make an item entry and add it to the pool, and we're done: Our pool doesn’t have any items yet, so we’ll make an item entry and add it to the pool, and we're done:
  
-<code java> +<yarncode java> 
-LootTableLoadingCallback.EVENT.register((resourceManager, lootManager, id, supplier, setter) -> { +LootTableLoadingCallback.EVENT.register((resourceManager, lootManager, id, table, setter) -> { 
-    if ("minecraft:blocks/coal_ore".equals(id.toString()) {+    if (COAL_ORE_LOOT_TABLE_ID.equals(id)) {
         FabricLootPoolBuilder poolBuilder = FabricLootPoolBuilder.builder()         FabricLootPoolBuilder poolBuilder = FabricLootPoolBuilder.builder()
-                .withRolls(ConstantLootTableRange.create(1)) +                .method_352(class_44.method_32448(1)); 
-                .withEntry(ItemEntry.builder(Items.EGG));+                .method_351(class_77.method_411(class_1802.field_8803));
  
-        supplier.withPool(poolBuilder);+        table.method_336(poolBuilder);
     }     }
 }); });
-</code>+</yarncode>
  
 {{:tutorial:coal_ore_egg.png?400|}} {{:tutorial:coal_ore_egg.png?400|}}
  
tutorial/adding_to_loot_tables.txt · Last modified: 2024/02/05 16:08 by mschae23