How to add drops to Minecraft's loot
To change Minecraft’s own drops, don’t replace its loot table. Fabric API’s LootTableEvents.MODIFY lets you add a pool to a table as it loads (check source.isBuiltin() and the table’s key). Forge and NeoForge use a global loot modifier: a LootModifier class with a codec, registered as a serializer, a JSON file in data/<mod>/loot_modifiers/ with its conditions, and its id in global_loot_modifiers.json under the loader’s own data folder (data/neoforge/loot_modifiers or data/forge/loot_modifiers).
Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge. Each example was compiled against its loader and version before it shipped in Linen.
- Pick the loot table by its id (minecraft:entities/zombie).
- Fabric: add a pool with the item and a random chance in LootTableEvents.MODIFY.
- Forge and NeoForge: write the LootModifier, register its codec, and list its JSON in global_loot_modifiers.json.
What you can change
Section titled “What you can change”The example uses these values. Change them in the code.
| Setting | In the example | What it does |
|---|---|---|
| table | minecraft:entities/zombie |
The loot table to add to (minecraft:blocks/grass, minecraft:chests/simple_dungeon…). |
| item | minecraft:emerald |
The item it adds. |
| chance | 0.25 |
How often it is added, 0 to 1. |
For each loader and version
Section titled “For each loader and version”The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.
Fabric 1.19.3 to 1.20.4
Section titled “Fabric 1.19.3 to 1.20.4”Uses:
LootTableEvents.MODIFYLootTableSource.isBuiltinLootPool.lootPoolLootItem.lootTableItemLootItemRandomChanceCondition.randomChance
Fabric API’s loot v2 event gives the table’s ResourceLocation (loot tables are not ResourceKeys before 1.20.5) and the resource and loot managers first.
src/main/java/com/example/examplemod/event/ZombieEmeraldsLootEvents.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.loot.v2.LootTableEvents;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.level.storage.loot.LootPool;import net.minecraft.world.level.storage.loot.entries.LootItem;import net.minecraft.world.level.storage.loot.predicates.LootItemRandomChanceCondition;
// Adds minecraft:emerald to minecraft:entities/zombie as the game loads it (Fabric API: LootTableEvents.MODIFY), 0.25 of the time.public final class ZombieEmeraldsLootEvents { private static final ResourceLocation TABLE = new ResourceLocation("minecraft:entities/zombie");
private ZombieEmeraldsLootEvents() { }
// Called from the mod's onInitialize. public static void register() { LootTableEvents.MODIFY.register((resourceManager, lootManager, id, tableBuilder, source) -> { // isBuiltin: only Minecraft's own table, not one a data pack replaced. if (source.isBuiltin() && TABLE.equals(id)) { tableBuilder.withPool(LootPool.lootPool() .add(LootItem.lootTableItem(BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft:emerald")))) .when(LootItemRandomChanceCondition.randomChance(0.25F))); } }); }}In your main mod class, inside onInitialize:
ZombieEmeraldsLootEvents.register();Fabric 1.20.5 to 1.20.6
Section titled “Fabric 1.20.5 to 1.20.6”Uses:
LootTableEvents.MODIFYLootTableSource.isBuiltinLootPool.lootPoolLootItem.lootTableItemLootItemRandomChanceCondition.randomChance
src/main/java/com/example/examplemod/event/ZombieEmeraldsLootEvents.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.loot.v2.LootTableEvents;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.level.storage.loot.LootPool;import net.minecraft.world.level.storage.loot.LootTable;import net.minecraft.world.level.storage.loot.entries.LootItem;import net.minecraft.world.level.storage.loot.predicates.LootItemRandomChanceCondition;
// Adds minecraft:emerald to minecraft:entities/zombie as the game loads it (Fabric API: LootTableEvents.MODIFY), 0.25 of the time.public final class ZombieEmeraldsLootEvents { private static final ResourceKey<LootTable> TABLE = ResourceKey.create(Registries.LOOT_TABLE, new ResourceLocation("minecraft:entities/zombie"));
private ZombieEmeraldsLootEvents() { }
// Called from the mod's onInitialize. public static void register() { LootTableEvents.MODIFY.register((key, tableBuilder, source) -> { // isBuiltin: only Minecraft's own table, not one a data pack replaced. if (source.isBuiltin() && TABLE.equals(key)) { tableBuilder.withPool(LootPool.lootPool() .add(LootItem.lootTableItem(BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft:emerald")))) .when(LootItemRandomChanceCondition.randomChance(0.25F))); } }); }}In your main mod class, inside onInitialize:
ZombieEmeraldsLootEvents.register();Fabric 1.21 to 1.21.1
Section titled “Fabric 1.21 to 1.21.1”Uses:
LootTableEvents.MODIFYLootTableSource.isBuiltinLootPool.lootPoolLootItem.lootTableItemLootItemRandomChanceCondition.randomChance
src/main/java/com/example/examplemod/event/ZombieEmeraldsLootEvents.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.loot.v3.LootTableEvents;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.level.storage.loot.LootPool;import net.minecraft.world.level.storage.loot.LootTable;import net.minecraft.world.level.storage.loot.entries.LootItem;import net.minecraft.world.level.storage.loot.predicates.LootItemRandomChanceCondition;
// Adds minecraft:emerald to minecraft:entities/zombie as the game loads it (Fabric API: LootTableEvents.MODIFY), 0.25 of the time.public final class ZombieEmeraldsLootEvents { private static final ResourceKey<LootTable> TABLE = ResourceKey.create(Registries.LOOT_TABLE, ResourceLocation.parse("minecraft:entities/zombie"));
private ZombieEmeraldsLootEvents() { }
// Called from the mod's onInitialize. public static void register() { LootTableEvents.MODIFY.register((key, tableBuilder, source, registries) -> { // isBuiltin: only Minecraft's own table, not one a data pack replaced. if (source.isBuiltin() && TABLE.equals(key)) { tableBuilder.withPool(LootPool.lootPool() .add(LootItem.lootTableItem(BuiltInRegistries.ITEM.get(ResourceLocation.parse("minecraft:emerald")))) .when(LootItemRandomChanceCondition.randomChance(0.25F))); } }); }}In your main mod class, inside onInitialize:
ZombieEmeraldsLootEvents.register();NeoForge 1.20.5 to 1.21.1, NeoForge 1.21.2 to 26.1.1
Section titled “NeoForge 1.20.5 to 1.21.1, NeoForge 1.21.2 to 26.1.1”Uses:
LootModifierIGlobalLootModifier.codecNeoForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERSneoforge:loot_table_idglobal_loot_modifiers.json
src/main/java/com/example/examplemod/loot/ZombieEmeraldsLootModifier.java
package com.example.examplemod.loot;
import com.mojang.serialization.MapCodec;import com.mojang.serialization.codecs.RecordCodecBuilder;import it.unimi.dsi.fastutil.objects.ObjectArrayList;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.storage.loot.LootContext;import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;import net.neoforged.neoforge.common.loot.IGlobalLootModifier;import net.neoforged.neoforge.common.loot.LootModifier;
// A global loot modifier that adds one item; data/examplemod/loot_modifiers/zombie_emeralds.json says which item and when (its conditions).public class ZombieEmeraldsLootModifier extends LootModifier { public static final MapCodec<ZombieEmeraldsLootModifier> CODEC = RecordCodecBuilder.mapCodec(instance -> codecStart(instance) .and(BuiltInRegistries.ITEM.byNameCodec().fieldOf("item").forGetter(modifier -> modifier.item)) .apply(instance, ZombieEmeraldsLootModifier::new));
private final Item item;
public ZombieEmeraldsLootModifier(LootItemCondition[] conditions, Item item) { super(conditions); this.item = item; }
@Override protected ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot, LootContext context) { generatedLoot.add(new ItemStack(item)); return generatedLoot; }
@Override public MapCodec<? extends IGlobalLootModifier> codec() { return CODEC; }}src/main/java/com/example/examplemod/registry/ModLootModifiers.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.loot.ZombieEmeraldsLootModifier;import com.mojang.serialization.MapCodec;import net.neoforged.neoforge.common.loot.IGlobalLootModifier;import net.neoforged.neoforge.registries.DeferredHolder;import net.neoforged.neoforge.registries.DeferredRegister;import net.neoforged.neoforge.registries.NeoForgeRegistries;
public final class ModLootModifiers { // Registered on the mod event bus in ExampleMod's constructor. public static final DeferredRegister<MapCodec<? extends IGlobalLootModifier>> LOOT_MODIFIERS = DeferredRegister.create( NeoForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERS, ExampleMod.MOD_ID);
public static final DeferredHolder<MapCodec<? extends IGlobalLootModifier>, MapCodec<ZombieEmeraldsLootModifier>> ZOMBIE_EMERALDS = LOOT_MODIFIERS.register( "zombie_emeralds", () -> ZombieEmeraldsLootModifier.CODEC);
private ModLootModifiers() { }}src/main/resources/data/examplemod/loot_modifiers/zombie_emeralds.json
{ "type": "examplemod:zombie_emeralds", "conditions": [ {"condition": "neoforge:loot_table_id", "loot_table_id": "minecraft:entities/zombie"}, {"condition": "minecraft:random_chance", "chance": 0.25} ], "item": "minecraft:emerald"}In your main mod class, inside <init>:
ModLootModifiers.LOOT_MODIFIERS.register(modEventBus);In src/main/resources/data/neoforge/loot_modifiers/global_loot_modifiers.json, add this entry to the top level:
"replace": falseIn src/main/resources/data/neoforge/loot_modifiers/global_loot_modifiers.json, add this to the list at /entries:
"examplemod:zombie_emeralds"NeoForge 1.20.2 to 1.20.4
Section titled “NeoForge 1.20.2 to 1.20.4”Uses:
LootModifierIGlobalLootModifier.codecNeoForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERSneoforge:loot_table_idglobal_loot_modifiers.json
src/main/java/com/example/examplemod/loot/ZombieEmeraldsLootModifier.java
package com.example.examplemod.loot;
import com.mojang.serialization.Codec;import com.mojang.serialization.codecs.RecordCodecBuilder;import it.unimi.dsi.fastutil.objects.ObjectArrayList;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.storage.loot.LootContext;import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;import net.neoforged.neoforge.common.loot.IGlobalLootModifier;import net.neoforged.neoforge.common.loot.LootModifier;
// A global loot modifier that adds one item; data/examplemod/loot_modifiers/zombie_emeralds.json says which item and when (its conditions).public class ZombieEmeraldsLootModifier extends LootModifier { public static final Codec<ZombieEmeraldsLootModifier> CODEC = RecordCodecBuilder.create(instance -> codecStart(instance) .and(BuiltInRegistries.ITEM.byNameCodec().fieldOf("item").forGetter(modifier -> modifier.item)) .apply(instance, ZombieEmeraldsLootModifier::new));
private final Item item;
public ZombieEmeraldsLootModifier(LootItemCondition[] conditions, Item item) { super(conditions); this.item = item; }
@Override protected ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot, LootContext context) { generatedLoot.add(new ItemStack(item)); return generatedLoot; }
@Override public Codec<? extends IGlobalLootModifier> codec() { return CODEC; }}src/main/java/com/example/examplemod/registry/ModLootModifiers.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.loot.ZombieEmeraldsLootModifier;import com.mojang.serialization.Codec;import net.neoforged.neoforge.common.loot.IGlobalLootModifier;import net.neoforged.neoforge.registries.DeferredHolder;import net.neoforged.neoforge.registries.DeferredRegister;import net.neoforged.neoforge.registries.NeoForgeRegistries;
public final class ModLootModifiers { // Registered on the mod event bus in ExampleMod's constructor. public static final DeferredRegister<Codec<? extends IGlobalLootModifier>> LOOT_MODIFIERS = DeferredRegister.create( NeoForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERS, ExampleMod.MOD_ID);
public static final DeferredHolder<Codec<? extends IGlobalLootModifier>, Codec<ZombieEmeraldsLootModifier>> ZOMBIE_EMERALDS = LOOT_MODIFIERS.register( "zombie_emeralds", () -> ZombieEmeraldsLootModifier.CODEC);
private ModLootModifiers() { }}src/main/resources/data/examplemod/loot_modifiers/zombie_emeralds.json
{ "type": "examplemod:zombie_emeralds", "conditions": [ {"condition": "neoforge:loot_table_id", "loot_table_id": "minecraft:entities/zombie"}, {"condition": "minecraft:random_chance", "chance": 0.25} ], "item": "minecraft:emerald"}In your main mod class, inside <init>:
ModLootModifiers.LOOT_MODIFIERS.register(modEventBus);In src/main/resources/data/neoforge/loot_modifiers/global_loot_modifiers.json, add this entry to the top level:
"replace": falseIn src/main/resources/data/neoforge/loot_modifiers/global_loot_modifiers.json, add this to the list at /entries:
"examplemod:zombie_emeralds"Fabric 1.21.2 to 1.21.10
Section titled “Fabric 1.21.2 to 1.21.10”Uses:
LootTableEvents.MODIFYLootTableSource.isBuiltinLootPool.lootPoolLootItem.lootTableItemLootItemRandomChanceCondition.randomChance
src/main/java/com/example/examplemod/event/ZombieEmeraldsLootEvents.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.loot.v3.LootTableEvents;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.level.storage.loot.LootPool;import net.minecraft.world.level.storage.loot.LootTable;import net.minecraft.world.level.storage.loot.entries.LootItem;import net.minecraft.world.level.storage.loot.predicates.LootItemRandomChanceCondition;
// Adds minecraft:emerald to minecraft:entities/zombie as the game loads it (Fabric API: LootTableEvents.MODIFY), 0.25 of the time.public final class ZombieEmeraldsLootEvents { private static final ResourceKey<LootTable> TABLE = ResourceKey.create(Registries.LOOT_TABLE, ResourceLocation.parse("minecraft:entities/zombie"));
private ZombieEmeraldsLootEvents() { }
// Called from the mod's onInitialize. public static void register() { LootTableEvents.MODIFY.register((key, tableBuilder, source, registries) -> { // isBuiltin: only Minecraft's own table, not one a data pack replaced. if (source.isBuiltin() && TABLE.equals(key)) { tableBuilder.withPool(LootPool.lootPool() .add(LootItem.lootTableItem(BuiltInRegistries.ITEM.getValue(ResourceLocation.parse("minecraft:emerald")))) .when(LootItemRandomChanceCondition.randomChance(0.25F))); } }); }}In your main mod class, inside onInitialize:
ZombieEmeraldsLootEvents.register();Fabric 1.21.11 to 26.3
Section titled “Fabric 1.21.11 to 26.3”Uses:
LootTableEvents.MODIFYLootTableSource.isBuiltinLootPool.lootPoolLootItem.lootTableItemLootItemRandomChanceCondition.randomChance
src/main/java/com/example/examplemod/event/ZombieEmeraldsLootEvents.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.loot.v3.LootTableEvents;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.resources.Identifier;import net.minecraft.world.level.storage.loot.LootPool;import net.minecraft.world.level.storage.loot.LootTable;import net.minecraft.world.level.storage.loot.entries.LootItem;import net.minecraft.world.level.storage.loot.predicates.LootItemRandomChanceCondition;
// Adds minecraft:emerald to minecraft:entities/zombie as the game loads it (Fabric API: LootTableEvents.MODIFY), 0.25 of the time.public final class ZombieEmeraldsLootEvents { private static final ResourceKey<LootTable> TABLE = ResourceKey.create(Registries.LOOT_TABLE, Identifier.parse("minecraft:entities/zombie"));
private ZombieEmeraldsLootEvents() { }
// Called from the mod's onInitialize. public static void register() { LootTableEvents.MODIFY.register((key, tableBuilder, source, registries) -> { // isBuiltin: only Minecraft's own table, not one a data pack replaced. if (source.isBuiltin() && TABLE.equals(key)) { tableBuilder.withPool(LootPool.lootPool() .add(LootItem.lootTableItem(BuiltInRegistries.ITEM.getValue(Identifier.parse("minecraft:emerald")))) .when(LootItemRandomChanceCondition.randomChance(0.25F))); } }); }}In your main mod class, inside onInitialize:
ZombieEmeraldsLootEvents.register();NeoForge 26.1.2 to 26.2
Section titled “NeoForge 26.1.2 to 26.2”Uses:
LootModifierIGlobalLootModifier.codecNeoForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERSneoforge:loot_table_idglobal_loot_modifiers.json
src/main/java/com/example/examplemod/loot/ZombieEmeraldsLootModifier.java
package com.example.examplemod.loot;
import com.mojang.serialization.MapCodec;import com.mojang.serialization.codecs.RecordCodecBuilder;import it.unimi.dsi.fastutil.objects.ObjectArrayList;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.storage.loot.LootContext;import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;import net.neoforged.neoforge.common.loot.IGlobalLootModifier;import net.neoforged.neoforge.common.loot.LootModifier;
// A global loot modifier that adds one item; data/examplemod/loot_modifiers/zombie_emeralds.json says which item and when (its conditions).public class ZombieEmeraldsLootModifier extends LootModifier { public static final MapCodec<ZombieEmeraldsLootModifier> CODEC = RecordCodecBuilder.mapCodec(instance -> codecStart(instance) .and(BuiltInRegistries.ITEM.byNameCodec().fieldOf("item").forGetter(modifier -> modifier.item)) .apply(instance, ZombieEmeraldsLootModifier::new));
private final Item item;
public ZombieEmeraldsLootModifier(LootItemCondition[] conditions, int priority, Item item) { super(conditions, priority); this.item = item; }
@Override protected ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot, LootContext context) { generatedLoot.add(new ItemStack(item)); return generatedLoot; }
@Override public MapCodec<? extends IGlobalLootModifier> codec() { return CODEC; }}src/main/java/com/example/examplemod/registry/ModLootModifiers.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.loot.ZombieEmeraldsLootModifier;import com.mojang.serialization.MapCodec;import net.neoforged.neoforge.common.loot.IGlobalLootModifier;import net.neoforged.neoforge.registries.DeferredHolder;import net.neoforged.neoforge.registries.DeferredRegister;import net.neoforged.neoforge.registries.NeoForgeRegistries;
public final class ModLootModifiers { // Registered on the mod event bus in ExampleMod's constructor. public static final DeferredRegister<MapCodec<? extends IGlobalLootModifier>> LOOT_MODIFIERS = DeferredRegister.create( NeoForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERS, ExampleMod.MOD_ID);
public static final DeferredHolder<MapCodec<? extends IGlobalLootModifier>, MapCodec<ZombieEmeraldsLootModifier>> ZOMBIE_EMERALDS = LOOT_MODIFIERS.register( "zombie_emeralds", () -> ZombieEmeraldsLootModifier.CODEC);
private ModLootModifiers() { }}src/main/resources/data/examplemod/loot_modifiers/zombie_emeralds.json
{ "type": "examplemod:zombie_emeralds", "conditions": [ {"condition": "neoforge:loot_table_id", "loot_table_id": "minecraft:entities/zombie"}, {"condition": "minecraft:random_chance", "chance": 0.25} ], "item": "minecraft:emerald"}In your main mod class, inside <init>:
ModLootModifiers.LOOT_MODIFIERS.register(modEventBus);In src/main/resources/data/neoforge/loot_modifiers/global_loot_modifiers.json, add this entry to the top level:
"replace": falseIn src/main/resources/data/neoforge/loot_modifiers/global_loot_modifiers.json, add this to the list at /entries:
"examplemod:zombie_emeralds"NeoForge 26.3
Section titled “NeoForge 26.3”Uses:
LootModifierIGlobalLootModifier.codecNeoForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERSneoforge:loot_table_idglobal_loot_modifiers.json
Since 26.3 a loot modifier has one optional condition (use minecraft:all_of to combine several) instead of an array.
src/main/java/com/example/examplemod/loot/ZombieEmeraldsLootModifier.java
package com.example.examplemod.loot;
import com.mojang.serialization.MapCodec;import com.mojang.serialization.codecs.RecordCodecBuilder;import java.util.Optional;import it.unimi.dsi.fastutil.objects.ObjectArrayList;import net.minecraft.core.Holder;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.storage.loot.LootContext;import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;import net.neoforged.neoforge.common.loot.IGlobalLootModifier;import net.neoforged.neoforge.common.loot.LootModifier;
// A global loot modifier that adds one item; data/examplemod/loot_modifiers/zombie_emeralds.json says which item and when (its conditions).public class ZombieEmeraldsLootModifier extends LootModifier { public static final MapCodec<ZombieEmeraldsLootModifier> CODEC = RecordCodecBuilder.mapCodec(instance -> codecStart(instance) .and(BuiltInRegistries.ITEM.byNameCodec().fieldOf("item").forGetter(modifier -> modifier.item)) .apply(instance, ZombieEmeraldsLootModifier::new));
private final Item item;
public ZombieEmeraldsLootModifier(Optional<Holder<LootItemCondition>> conditions, int priority, Item item) { super(conditions, priority); this.item = item; }
@Override protected ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot, LootContext context) { generatedLoot.add(new ItemStack(item)); return generatedLoot; }
@Override public MapCodec<? extends IGlobalLootModifier> codec() { return CODEC; }}src/main/java/com/example/examplemod/registry/ModLootModifiers.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.loot.ZombieEmeraldsLootModifier;import com.mojang.serialization.MapCodec;import net.neoforged.neoforge.common.loot.IGlobalLootModifier;import net.neoforged.neoforge.registries.DeferredHolder;import net.neoforged.neoforge.registries.DeferredRegister;import net.neoforged.neoforge.registries.NeoForgeRegistries;
public final class ModLootModifiers { // Registered on the mod event bus in ExampleMod's constructor. public static final DeferredRegister<MapCodec<? extends IGlobalLootModifier>> LOOT_MODIFIERS = DeferredRegister.create( NeoForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERS, ExampleMod.MOD_ID);
public static final DeferredHolder<MapCodec<? extends IGlobalLootModifier>, MapCodec<ZombieEmeraldsLootModifier>> ZOMBIE_EMERALDS = LOOT_MODIFIERS.register( "zombie_emeralds", () -> ZombieEmeraldsLootModifier.CODEC);
private ModLootModifiers() { }}src/main/resources/data/examplemod/loot_modifiers/zombie_emeralds.json
{ "type": "examplemod:zombie_emeralds", "conditions": { "condition": "minecraft:all_of", "terms": [ {"condition": "neoforge:loot_table_id", "loot_table_id": "minecraft:entities/zombie"}, {"condition": "minecraft:random_chance", "chance": 0.25} ] }, "item": "minecraft:emerald"}In your main mod class, inside <init>:
ModLootModifiers.LOOT_MODIFIERS.register(modEventBus);In src/main/resources/data/neoforge/loot_modifiers/global_loot_modifiers.json, add this entry to the top level:
"replace": falseIn src/main/resources/data/neoforge/loot_modifiers/global_loot_modifiers.json, add this to the list at /entries:
"examplemod:zombie_emeralds"Forge 1.17.1
Section titled “Forge 1.17.1”Uses:
LootModifierIGlobalLootModifierForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERS
src/main/java/com/example/examplemod/loot/ZombieEmeraldsLootModifier.java
package com.example.examplemod.loot;
import com.google.gson.JsonObject;import java.util.List;import net.minecraft.resources.ResourceLocation;import net.minecraft.util.GsonHelper;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.storage.loot.LootContext;import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;import net.minecraftforge.common.loot.GlobalLootModifierSerializer;import net.minecraftforge.common.loot.LootModifier;import net.minecraftforge.registries.ForgeRegistries;
// A global loot modifier that adds one item; data/examplemod/loot_modifiers/zombie_emeralds.json says which item and when (its conditions).public class ZombieEmeraldsLootModifier extends LootModifier { private final Item item;
public ZombieEmeraldsLootModifier(LootItemCondition[] conditions, Item item) { super(conditions); this.item = item; }
@Override protected List<ItemStack> doApply(List<ItemStack> generatedLoot, LootContext context) { generatedLoot.add(new ItemStack(item)); return generatedLoot; }
// Reads the modifier from its JSON file (1.18.2 has serializer classes, not codecs). public static class Serializer extends GlobalLootModifierSerializer<ZombieEmeraldsLootModifier> { @Override public ZombieEmeraldsLootModifier read(ResourceLocation name, JsonObject json, LootItemCondition[] conditions) { Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(GsonHelper.getAsString(json, "item"))); return new ZombieEmeraldsLootModifier(conditions, item); }
@Override public JsonObject write(ZombieEmeraldsLootModifier modifier) { JsonObject json = makeConditions(modifier.conditions); json.addProperty("item", ForgeRegistries.ITEMS.getKey(modifier.item).toString()); return json; } }}src/main/java/com/example/examplemod/registry/ModLootModifiers.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.loot.ZombieEmeraldsLootModifier;import net.minecraftforge.common.loot.GlobalLootModifierSerializer;import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;import net.minecraftforge.registries.DeferredRegister;import net.minecraftforge.registries.ForgeRegistries;import net.minecraftforge.fmllegacy.RegistryObject;
public final class ModLootModifiers { public static final DeferredRegister<GlobalLootModifierSerializer<?>> LOOT_MODIFIERS = DeferredRegister.create( ForgeRegistries.LOOT_MODIFIER_SERIALIZERS, ExampleMod.MOD_ID);
public static final RegistryObject<GlobalLootModifierSerializer<ZombieEmeraldsLootModifier>> ZOMBIE_EMERALDS = LOOT_MODIFIERS.register( "zombie_emeralds", ZombieEmeraldsLootModifier.Serializer::new);
private ModLootModifiers() { }
// Called from the ExampleMod constructor: puts the loot modifier serializers on the mod event bus. public static void register() { LOOT_MODIFIERS.register(FMLJavaModLoadingContext.get().getModEventBus()); }}src/main/resources/data/examplemod/loot_modifiers/zombie_emeralds.json
{ "type": "examplemod:zombie_emeralds", "conditions": [ {"condition": "forge:loot_table_id", "loot_table_id": "minecraft:entities/zombie"}, {"condition": "minecraft:random_chance", "chance": 0.25} ], "item": "minecraft:emerald"}In your main mod class, inside <init>:
ModLootModifiers.register();In src/main/resources/data/forge/loot_modifiers/global_loot_modifiers.json, add this entry to the top level:
"replace": falseIn src/main/resources/data/forge/loot_modifiers/global_loot_modifiers.json, add this to the list at /entries:
"examplemod:zombie_emeralds"Forge 1.18.2
Section titled “Forge 1.18.2”Uses:
LootModifierIGlobalLootModifierForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERS
src/main/java/com/example/examplemod/loot/ZombieEmeraldsLootModifier.java
package com.example.examplemod.loot;
import com.google.gson.JsonObject;import java.util.List;import net.minecraft.resources.ResourceLocation;import net.minecraft.util.GsonHelper;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.storage.loot.LootContext;import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;import net.minecraftforge.common.loot.GlobalLootModifierSerializer;import net.minecraftforge.common.loot.LootModifier;import net.minecraftforge.registries.ForgeRegistries;
// A global loot modifier that adds one item; data/examplemod/loot_modifiers/zombie_emeralds.json says which item and when (its conditions).public class ZombieEmeraldsLootModifier extends LootModifier { private final Item item;
public ZombieEmeraldsLootModifier(LootItemCondition[] conditions, Item item) { super(conditions); this.item = item; }
@Override protected List<ItemStack> doApply(List<ItemStack> generatedLoot, LootContext context) { generatedLoot.add(new ItemStack(item)); return generatedLoot; }
// Reads the modifier from its JSON file (1.18.2 has serializer classes, not codecs). public static class Serializer extends GlobalLootModifierSerializer<ZombieEmeraldsLootModifier> { @Override public ZombieEmeraldsLootModifier read(ResourceLocation name, JsonObject json, LootItemCondition[] conditions) { Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(GsonHelper.getAsString(json, "item"))); return new ZombieEmeraldsLootModifier(conditions, item); }
@Override public JsonObject write(ZombieEmeraldsLootModifier modifier) { JsonObject json = makeConditions(modifier.conditions); json.addProperty("item", ForgeRegistries.ITEMS.getKey(modifier.item).toString()); return json; } }}src/main/java/com/example/examplemod/registry/ModLootModifiers.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.loot.ZombieEmeraldsLootModifier;import net.minecraftforge.common.loot.GlobalLootModifierSerializer;import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;import net.minecraftforge.registries.DeferredRegister;import net.minecraftforge.registries.ForgeRegistries;import net.minecraftforge.registries.RegistryObject;
public final class ModLootModifiers { public static final DeferredRegister<GlobalLootModifierSerializer<?>> LOOT_MODIFIERS = DeferredRegister.create( ForgeRegistries.Keys.LOOT_MODIFIER_SERIALIZERS, ExampleMod.MOD_ID);
public static final RegistryObject<GlobalLootModifierSerializer<ZombieEmeraldsLootModifier>> ZOMBIE_EMERALDS = LOOT_MODIFIERS.register( "zombie_emeralds", ZombieEmeraldsLootModifier.Serializer::new);
private ModLootModifiers() { }
// Called from the ExampleMod constructor: puts the loot modifier serializers on the mod event bus. public static void register() { LOOT_MODIFIERS.register(FMLJavaModLoadingContext.get().getModEventBus()); }}src/main/resources/data/examplemod/loot_modifiers/zombie_emeralds.json
{ "type": "examplemod:zombie_emeralds", "conditions": [ {"condition": "forge:loot_table_id", "loot_table_id": "minecraft:entities/zombie"}, {"condition": "minecraft:random_chance", "chance": 0.25} ], "item": "minecraft:emerald"}In your main mod class, inside <init>:
ModLootModifiers.register();In src/main/resources/data/forge/loot_modifiers/global_loot_modifiers.json, add this entry to the top level:
"replace": falseIn src/main/resources/data/forge/loot_modifiers/global_loot_modifiers.json, add this to the list at /entries:
"examplemod:zombie_emeralds"Forge 1.19 to 1.20.1, NeoForge 1.20.1
Section titled “Forge 1.19 to 1.20.1, NeoForge 1.20.1”Uses:
LootModifierIGlobalLootModifierForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERS
src/main/java/com/example/examplemod/loot/ZombieEmeraldsLootModifier.java
package com.example.examplemod.loot;
import com.google.common.base.Suppliers;import com.mojang.serialization.Codec;import com.mojang.serialization.codecs.RecordCodecBuilder;import it.unimi.dsi.fastutil.objects.ObjectArrayList;import java.util.function.Supplier;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.storage.loot.LootContext;import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;import net.minecraftforge.common.loot.IGlobalLootModifier;import net.minecraftforge.common.loot.LootModifier;import net.minecraftforge.registries.ForgeRegistries;
// A global loot modifier that adds one item; data/examplemod/loot_modifiers/zombie_emeralds.json says which item and when (its conditions).public class ZombieEmeraldsLootModifier extends LootModifier { public static final Supplier<Codec<ZombieEmeraldsLootModifier>> CODEC = Suppliers.memoize(() -> RecordCodecBuilder.create(instance -> codecStart(instance) .and(ForgeRegistries.ITEMS.getCodec().fieldOf("item").forGetter(modifier -> modifier.item)) .apply(instance, ZombieEmeraldsLootModifier::new)));
private final Item item;
public ZombieEmeraldsLootModifier(LootItemCondition[] conditions, Item item) { super(conditions); this.item = item; }
@Override protected ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot, LootContext context) { generatedLoot.add(new ItemStack(item)); return generatedLoot; }
@Override public Codec<? extends IGlobalLootModifier> codec() { return CODEC.get(); }}src/main/java/com/example/examplemod/registry/ModLootModifiers.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.loot.ZombieEmeraldsLootModifier;import com.mojang.serialization.Codec;import net.minecraftforge.common.loot.IGlobalLootModifier;import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;import net.minecraftforge.registries.DeferredRegister;import net.minecraftforge.registries.ForgeRegistries;import net.minecraftforge.registries.RegistryObject;
public final class ModLootModifiers { public static final DeferredRegister<Codec<? extends IGlobalLootModifier>> LOOT_MODIFIERS = DeferredRegister.create( ForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERS, ExampleMod.MOD_ID);
public static final RegistryObject<Codec<ZombieEmeraldsLootModifier>> ZOMBIE_EMERALDS = LOOT_MODIFIERS.register( "zombie_emeralds", ZombieEmeraldsLootModifier.CODEC);
private ModLootModifiers() { }
// Called from the ExampleMod constructor: puts the loot modifier serializers on the mod event bus. public static void register() { LOOT_MODIFIERS.register(FMLJavaModLoadingContext.get().getModEventBus()); }}src/main/resources/data/examplemod/loot_modifiers/zombie_emeralds.json
{ "type": "examplemod:zombie_emeralds", "conditions": [ {"condition": "forge:loot_table_id", "loot_table_id": "minecraft:entities/zombie"}, {"condition": "minecraft:random_chance", "chance": 0.25} ], "item": "minecraft:emerald"}In your main mod class, inside <init>:
ModLootModifiers.register();In src/main/resources/data/forge/loot_modifiers/global_loot_modifiers.json, add this entry to the top level:
"replace": falseIn src/main/resources/data/forge/loot_modifiers/global_loot_modifiers.json, add this to the list at /entries:
"examplemod:zombie_emeralds"Fabric 1.18.2 to 1.19.2
Section titled “Fabric 1.18.2 to 1.19.2”Uses:
LootTableEvents.MODIFYLootTableSource.isBuiltinLootPool.lootPoolLootItem.lootTableItemLootItemRandomChanceCondition.randomChance
src/main/java/com/example/examplemod/event/ZombieEmeraldsLootEvents.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.loot.v2.LootTableEvents;import net.minecraft.core.Registry;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.level.storage.loot.LootPool;import net.minecraft.world.level.storage.loot.entries.LootItem;import net.minecraft.world.level.storage.loot.predicates.LootItemRandomChanceCondition;
// Adds minecraft:emerald to minecraft:entities/zombie as the game loads it (Fabric API: LootTableEvents.MODIFY), 0.25 of the time.public final class ZombieEmeraldsLootEvents { private static final ResourceLocation TABLE = new ResourceLocation("minecraft:entities/zombie");
private ZombieEmeraldsLootEvents() { }
// Called from the mod's onInitialize. public static void register() { LootTableEvents.MODIFY.register((resourceManager, lootManager, id, tableBuilder, source) -> { // isBuiltin: only Minecraft's own table, not one a data pack replaced. if (source.isBuiltin() && TABLE.equals(id)) { tableBuilder.withPool(LootPool.lootPool() .add(LootItem.lootTableItem(Registry.ITEM.get(new ResourceLocation("minecraft:emerald")))) .when(LootItemRandomChanceCondition.randomChance(0.25F))); } }); }}In your main mod class, inside onInitialize:
ZombieEmeraldsLootEvents.register();Fabric 1.16.5 to 1.18.1
Section titled “Fabric 1.16.5 to 1.18.1”Uses:
LootTableEvents.MODIFYLootTableSource.isBuiltinLootPool.lootPoolLootItem.lootTableItemLootItemRandomChanceCondition.randomChance
src/main/java/com/example/examplemod/event/ZombieEmeraldsLootEvents.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.loot.v1.event.LootTableLoadingCallback;import net.minecraft.core.Registry;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.level.storage.loot.LootPool;import net.minecraft.world.level.storage.loot.entries.LootItem;import net.minecraft.world.level.storage.loot.predicates.LootItemRandomChanceCondition;
// Adds minecraft:emerald to minecraft:entities/zombie as the game loads it (Fabric API: LootTableLoadingCallback), 0.25 of the time.public final class ZombieEmeraldsLootEvents { private static final ResourceLocation TABLE = new ResourceLocation("minecraft:entities/zombie");
private ZombieEmeraldsLootEvents() { }
// Called from the mod's onInitialize. public static void register() { LootTableLoadingCallback.EVENT.register((resourceManager, lootManager, id, supplier, setter) -> { if (TABLE.equals(id)) { supplier.pool(LootPool.lootPool() .add(LootItem.lootTableItem(Registry.ITEM.get(new ResourceLocation("minecraft:emerald")))) .when(LootItemRandomChanceCondition.randomChance(0.25F))); } }); }}In your main mod class, inside onInitialize:
ZombieEmeraldsLootEvents.register();Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
LootModifier.doApplyGlobalLootModifierSerializerForgeRegistries.LOOT_MODIFIER_SERIALIZERS
In 1.16.5 the modifier’s JSON file has no type: the serializer registered under the same name as the file reads it.
src/main/java/com/example/examplemod/loot/ZombieEmeraldsLootModifier.java
package com.example.examplemod.loot;
import com.google.gson.JsonObject;import java.util.List;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.loot.LootContext;import net.minecraft.loot.conditions.ILootCondition;import net.minecraft.util.JSONUtils;import net.minecraft.util.ResourceLocation;import net.minecraftforge.common.loot.GlobalLootModifierSerializer;import net.minecraftforge.common.loot.LootModifier;import net.minecraftforge.registries.ForgeRegistries;
// A global loot modifier that adds one item; data/examplemod/loot_modifiers/zombie_emeralds.json says which item and when (its conditions).public class ZombieEmeraldsLootModifier extends LootModifier { private final Item item;
public ZombieEmeraldsLootModifier(ILootCondition[] conditions, Item item) { super(conditions); this.item = item; }
@Override protected List<ItemStack> doApply(List<ItemStack> generatedLoot, LootContext context) { generatedLoot.add(new ItemStack(item)); return generatedLoot; }
// Reads the modifier's JSON file and writes it back (1.16.5 has no codecs: each modifier brings a serializer). public static class Serializer extends GlobalLootModifierSerializer<ZombieEmeraldsLootModifier> { @Override public ZombieEmeraldsLootModifier read(ResourceLocation name, JsonObject json, ILootCondition[] conditions) { Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(JSONUtils.getAsString(json, "item"))); return new ZombieEmeraldsLootModifier(conditions, item); }
@Override public JsonObject write(ZombieEmeraldsLootModifier modifier) { JsonObject json = makeConditions(modifier.conditions); json.addProperty("item", modifier.item.getRegistryName().toString()); return json; } }}src/main/java/com/example/examplemod/registry/ModLootModifiers.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.loot.ZombieEmeraldsLootModifier;import net.minecraftforge.common.loot.GlobalLootModifierSerializer;import net.minecraftforge.fml.RegistryObject;import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;import net.minecraftforge.registries.DeferredRegister;import net.minecraftforge.registries.ForgeRegistries;
public final class ModLootModifiers { public static final DeferredRegister<GlobalLootModifierSerializer<?>> LOOT_MODIFIERS = DeferredRegister.create( ForgeRegistries.LOOT_MODIFIER_SERIALIZERS, ExampleMod.MOD_ID);
// The serializer's name is the name of the modifier's JSON file. public static final RegistryObject<ZombieEmeraldsLootModifier.Serializer> ZOMBIE_EMERALDS = LOOT_MODIFIERS.register( "zombie_emeralds", ZombieEmeraldsLootModifier.Serializer::new);
private ModLootModifiers() { }
// Called from the ExampleMod constructor: puts the loot modifier serializers on the mod event bus. public static void register() { LOOT_MODIFIERS.register(FMLJavaModLoadingContext.get().getModEventBus()); }}src/main/resources/data/examplemod/loot_modifiers/zombie_emeralds.json
{ "conditions": [ {"condition": "forge:loot_table_id", "loot_table_id": "minecraft:entities/zombie"}, {"condition": "minecraft:random_chance", "chance": 0.25} ], "item": "minecraft:emerald"}In your main mod class, inside <init>:
ModLootModifiers.register();In src/main/resources/data/forge/loot_modifiers/global_loot_modifiers.json, add this entry to the top level:
"replace": falseIn src/main/resources/data/forge/loot_modifiers/global_loot_modifiers.json, add this to the list at /entries:
"examplemod:zombie_emeralds"Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.1/develop/events
- https://docs.neoforged.net/docs/1.21.1/resources/server/loottables/glm
- https://docs.neoforged.net/docs/1.21.1/resources/server/loottables/lootconditions
Related how-tos
Section titled “Related how-tos”- A recipe type of your own
- A block entity that ticks
- A block that does something when clicked
- A block that gives off particles
- A mob that drops an item
In Linen
Section titled “In Linen”Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.