How to make a mob that drops an item in a Minecraft mod
A mob’s drops come from its loot table, but a mod can add one more item without touching the table: listen for the mob’s death. Fabric API has ServerLivingEntityEvents.AFTER_DEATH and calls entity.spawnAtLocation. Forge and NeoForge have LivingDropsEvent and you add an item entity to its drops list. Both run on the server only. For your own mob, write a loot table in data/<modid>/loot_table/entities/<mob>.json instead (loot_tables before 1.21, assets/<modid>/loot_tables on 1.12.2).
Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.19.2 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.
- Pick the mob and the item by their ids, like minecraft:zombie and minecraft:diamond.
- Listen for the death event on your loader.
- Roll the chance and add the item to the drops.
- Kill the mob in survival a few times to see the drop.
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 |
|---|---|---|
| mob | minecraft:zombie |
The id of the mob that drops it. |
| item | minecraft:diamond |
The id of the item it drops. |
| chance | 0.5 |
The chance, from 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.6
Section titled “Fabric 1.19.3 to 1.20.6”Uses:
ServerLivingEntityEvents.AFTER_DEATHEntity.spawnAtLocationBuiltInRegistries.ITEM
src/main/java/com/example/examplemod/event/ZombieLootDrops.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.EntityType;import net.minecraft.world.item.ItemStack;
// A minecraft:zombie drops minecraft:diamond on death with chance 0.5F (Fabric API: ServerLivingEntityEvents.AFTER_DEATH).public final class ZombieLootDrops { private ZombieLootDrops() { }
// Called from the mod's onInitialize. public static void register() { EntityType<?> mob = BuiltInRegistries.ENTITY_TYPE.get(new ResourceLocation("minecraft:zombie")); ServerLivingEntityEvents.AFTER_DEATH.register((entity, source) -> { if (entity.getType() == mob && entity.getRandom().nextFloat() < 0.5F) { entity.spawnAtLocation(new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft:diamond")))); } }); }}In your main mod class, inside onInitialize:
ZombieLootDrops.register();Fabric 1.21 to 1.21.1
Section titled “Fabric 1.21 to 1.21.1”Uses:
ServerLivingEntityEvents.AFTER_DEATHEntity.spawnAtLocationBuiltInRegistries.ITEM
src/main/java/com/example/examplemod/event/ZombieLootDrops.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.EntityType;import net.minecraft.world.item.ItemStack;
// A minecraft:zombie drops minecraft:diamond on death with chance 0.5F (Fabric API: ServerLivingEntityEvents.AFTER_DEATH).public final class ZombieLootDrops { private ZombieLootDrops() { }
// Called from the mod's onInitialize. public static void register() { EntityType<?> mob = BuiltInRegistries.ENTITY_TYPE.get(ResourceLocation.parse("minecraft:zombie")); ServerLivingEntityEvents.AFTER_DEATH.register((entity, source) -> { if (entity.getType() == mob && entity.getRandom().nextFloat() < 0.5F) { entity.spawnAtLocation(new ItemStack(BuiltInRegistries.ITEM.get(ResourceLocation.parse("minecraft:diamond")))); } }); }}In your main mod class, inside onInitialize:
ZombieLootDrops.register();NeoForge 1.21 to 1.21.1
Section titled “NeoForge 1.21 to 1.21.1”Uses:
LivingDropsEvent.getDropsItemEntityBuiltInRegistries.ITEM
src/main/java/com/example/examplemod/event/ZombieLootDrops.java
package com.example.examplemod.event;
import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.item.ItemEntity;import net.minecraft.world.item.ItemStack;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.entity.living.LivingDropsEvent;
// A minecraft:zombie drops minecraft:diamond on death with chance 0.5F (NeoForge: LivingDropsEvent adds to the drops list).public final class ZombieLootDrops { private ZombieLootDrops() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(ZombieLootDrops::onDrops); }
private static void onDrops(LivingDropsEvent event) { EntityType<?> mob = BuiltInRegistries.ENTITY_TYPE.get(ResourceLocation.parse("minecraft:zombie")); if (event.getEntity().getType() == mob && event.getEntity().getRandom().nextFloat() < 0.5F) { ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.get(ResourceLocation.parse("minecraft:diamond"))); event.getDrops().add(new ItemEntity(event.getEntity().level(), event.getEntity().getX(), event.getEntity().getY(), event.getEntity().getZ(), stack)); } }}In your main mod class, inside <init>:
ZombieLootDrops.register();NeoForge 1.20.2 to 1.20.6
Section titled “NeoForge 1.20.2 to 1.20.6”Uses:
LivingDropsEvent.getDropsItemEntityBuiltInRegistries.ITEM
src/main/java/com/example/examplemod/event/ZombieLootDrops.java
package com.example.examplemod.event;
import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.item.ItemEntity;import net.minecraft.world.item.ItemStack;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.entity.living.LivingDropsEvent;
// A minecraft:zombie drops minecraft:diamond on death with chance 0.5F (NeoForge: LivingDropsEvent adds to the drops list).public final class ZombieLootDrops { private ZombieLootDrops() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(ZombieLootDrops::onDrops); }
private static void onDrops(LivingDropsEvent event) { EntityType<?> mob = BuiltInRegistries.ENTITY_TYPE.get(new ResourceLocation("minecraft:zombie")); if (event.getEntity().getType() == mob && event.getEntity().getRandom().nextFloat() < 0.5F) { ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft:diamond"))); event.getDrops().add(new ItemEntity(event.getEntity().level(), event.getEntity().getX(), event.getEntity().getY(), event.getEntity().getZ(), stack)); } }}In your main mod class, inside <init>:
ZombieLootDrops.register();Fabric 1.21.2 to 1.21.10
Section titled “Fabric 1.21.2 to 1.21.10”Uses:
ServerLivingEntityEvents.AFTER_DEATHEntity.spawnAtLocationBuiltInRegistries.ITEM
src/main/java/com/example/examplemod/event/ZombieLootDrops.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.resources.ResourceLocation;import net.minecraft.server.level.ServerLevel;import net.minecraft.world.entity.EntityType;import net.minecraft.world.item.ItemStack;
// A minecraft:zombie drops minecraft:diamond on death with chance 0.5F (Fabric API: ServerLivingEntityEvents.AFTER_DEATH).public final class ZombieLootDrops { private ZombieLootDrops() { }
// Called from the mod's onInitialize. public static void register() { EntityType<?> mob = BuiltInRegistries.ENTITY_TYPE.getValue(ResourceLocation.parse("minecraft:zombie")); ServerLivingEntityEvents.AFTER_DEATH.register((entity, source) -> { if (entity.level() instanceof ServerLevel level && entity.getType() == mob && entity.getRandom().nextFloat() < 0.5F) { entity.spawnAtLocation(level, new ItemStack(BuiltInRegistries.ITEM.getValue(ResourceLocation.parse("minecraft:diamond")))); } }); }}In your main mod class, inside onInitialize:
ZombieLootDrops.register();NeoForge 1.21.2 to 1.21.10
Section titled “NeoForge 1.21.2 to 1.21.10”Uses:
LivingDropsEvent.getDropsItemEntityBuiltInRegistries.ITEM
src/main/java/com/example/examplemod/event/ZombieLootDrops.java
package com.example.examplemod.event;
import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.item.ItemEntity;import net.minecraft.world.item.ItemStack;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.entity.living.LivingDropsEvent;
// A minecraft:zombie drops minecraft:diamond on death with chance 0.5F (NeoForge: LivingDropsEvent adds to the drops list).public final class ZombieLootDrops { private ZombieLootDrops() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(ZombieLootDrops::onDrops); }
private static void onDrops(LivingDropsEvent event) { EntityType<?> mob = BuiltInRegistries.ENTITY_TYPE.getValue(ResourceLocation.parse("minecraft:zombie")); if (event.getEntity().getType() == mob && event.getEntity().getRandom().nextFloat() < 0.5F) { ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.getValue(ResourceLocation.parse("minecraft:diamond"))); event.getDrops().add(new ItemEntity(event.getEntity().level(), event.getEntity().getX(), event.getEntity().getY(), event.getEntity().getZ(), stack)); } }}In your main mod class, inside <init>:
ZombieLootDrops.register();NeoForge 1.21.11 to 26.3
Section titled “NeoForge 1.21.11 to 26.3”Uses:
LivingDropsEvent.getDropsItemEntityBuiltInRegistries.ITEM
src/main/java/com/example/examplemod/event/ZombieLootDrops.java
package com.example.examplemod.event;
import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.resources.Identifier;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.item.ItemEntity;import net.minecraft.world.item.ItemStack;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.entity.living.LivingDropsEvent;
// A minecraft:zombie drops minecraft:diamond on death with chance 0.5F (NeoForge: LivingDropsEvent adds to the drops list).public final class ZombieLootDrops { private ZombieLootDrops() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(ZombieLootDrops::onDrops); }
private static void onDrops(LivingDropsEvent event) { EntityType<?> mob = BuiltInRegistries.ENTITY_TYPE.getValue(Identifier.parse("minecraft:zombie")); if (event.getEntity().getType() == mob && event.getEntity().getRandom().nextFloat() < 0.5F) { ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.getValue(Identifier.parse("minecraft:diamond"))); event.getDrops().add(new ItemEntity(event.getEntity().level(), event.getEntity().getX(), event.getEntity().getY(), event.getEntity().getZ(), stack)); } }}In your main mod class, inside <init>:
ZombieLootDrops.register();Fabric 1.21.11 to 26.3
Section titled “Fabric 1.21.11 to 26.3”Uses:
ServerLivingEntityEvents.AFTER_DEATHEntity.spawnAtLocationBuiltInRegistries.ITEM
src/main/java/com/example/examplemod/event/ZombieLootDrops.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.resources.Identifier;import net.minecraft.server.level.ServerLevel;import net.minecraft.world.entity.EntityType;import net.minecraft.world.item.ItemStack;
// A minecraft:zombie drops minecraft:diamond on death with chance 0.5F (Fabric API: ServerLivingEntityEvents.AFTER_DEATH).public final class ZombieLootDrops { private ZombieLootDrops() { }
// Called from the mod's onInitialize. public static void register() { EntityType<?> mob = BuiltInRegistries.ENTITY_TYPE.getValue(Identifier.parse("minecraft:zombie")); ServerLivingEntityEvents.AFTER_DEATH.register((entity, source) -> { if (entity.level() instanceof ServerLevel level && entity.getType() == mob && entity.getRandom().nextFloat() < 0.5F) { entity.spawnAtLocation(level, new ItemStack(BuiltInRegistries.ITEM.getValue(Identifier.parse("minecraft:diamond")))); } }); }}In your main mod class, inside onInitialize:
ZombieLootDrops.register();Forge 1.17.1 to 1.18.2
Section titled “Forge 1.17.1 to 1.18.2”Uses:
LivingDropsEvent.getDropsItemEntityBuiltInRegistries.ITEM
src/main/java/com/example/examplemod/event/ZombieLootDrops.java
package com.example.examplemod.event;
import net.minecraft.core.Registry;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.item.ItemEntity;import net.minecraft.world.item.ItemStack;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.living.LivingDropsEvent;
// A minecraft:zombie drops minecraft:diamond on death with chance 0.5F (Forge: LivingDropsEvent adds to the drops list).public final class ZombieLootDrops { private ZombieLootDrops() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(ZombieLootDrops::onDrops); }
private static void onDrops(LivingDropsEvent event) { EntityType<?> mob = Registry.ENTITY_TYPE.get(new ResourceLocation("minecraft:zombie")); if (event.getEntityLiving().getType() == mob && event.getEntityLiving().getRandom().nextFloat() < 0.5F) { ItemStack stack = new ItemStack(Registry.ITEM.get(new ResourceLocation("minecraft:diamond"))); event.getDrops().add(new ItemEntity(event.getEntityLiving().level, event.getEntityLiving().getX(), event.getEntityLiving().getY(), event.getEntityLiving().getZ(), stack)); } }}In your main mod class, inside <init>:
ZombieLootDrops.register();Forge 1.19 to 1.19.2
Section titled “Forge 1.19 to 1.19.2”Uses:
LivingDropsEvent.getDropsItemEntityBuiltInRegistries.ITEM
src/main/java/com/example/examplemod/event/ZombieLootDrops.java
package com.example.examplemod.event;
import net.minecraft.core.Registry;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.item.ItemEntity;import net.minecraft.world.item.ItemStack;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.living.LivingDropsEvent;
// A minecraft:zombie drops minecraft:diamond on death with chance 0.5F (Forge: LivingDropsEvent adds to the drops list).public final class ZombieLootDrops { private ZombieLootDrops() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(ZombieLootDrops::onDrops); }
private static void onDrops(LivingDropsEvent event) { EntityType<?> mob = Registry.ENTITY_TYPE.get(new ResourceLocation("minecraft:zombie")); if (event.getEntity().getType() == mob && event.getEntity().getRandom().nextFloat() < 0.5F) { ItemStack stack = new ItemStack(Registry.ITEM.get(new ResourceLocation("minecraft:diamond"))); event.getDrops().add(new ItemEntity(event.getEntity().level, event.getEntity().getX(), event.getEntity().getY(), event.getEntity().getZ(), stack)); } }}In your main mod class, inside <init>:
ZombieLootDrops.register();Forge 1.19.3 to 1.19.4
Section titled “Forge 1.19.3 to 1.19.4”Uses:
LivingDropsEvent.getDropsItemEntityBuiltInRegistries.ITEM
src/main/java/com/example/examplemod/event/ZombieLootDrops.java
package com.example.examplemod.event;
import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.item.ItemEntity;import net.minecraft.world.item.ItemStack;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.living.LivingDropsEvent;
// A minecraft:zombie drops minecraft:diamond on death with chance 0.5F (Forge: LivingDropsEvent adds to the drops list).public final class ZombieLootDrops { private ZombieLootDrops() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(ZombieLootDrops::onDrops); }
private static void onDrops(LivingDropsEvent event) { EntityType<?> mob = BuiltInRegistries.ENTITY_TYPE.get(new ResourceLocation("minecraft:zombie")); if (event.getEntity().getType() == mob && event.getEntity().getRandom().nextFloat() < 0.5F) { ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft:diamond"))); event.getDrops().add(new ItemEntity(event.getEntity().level, event.getEntity().getX(), event.getEntity().getY(), event.getEntity().getZ(), stack)); } }}In your main mod class, inside <init>:
ZombieLootDrops.register();Forge 1.20 to 1.20.1
Section titled “Forge 1.20 to 1.20.1”Uses:
LivingDropsEvent.getDropsItemEntityBuiltInRegistries.ITEM
src/main/java/com/example/examplemod/event/ZombieLootDrops.java
package com.example.examplemod.event;
import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.item.ItemEntity;import net.minecraft.world.item.ItemStack;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.living.LivingDropsEvent;
// A minecraft:zombie drops minecraft:diamond on death with chance 0.5F (Forge: LivingDropsEvent adds to the drops list).public final class ZombieLootDrops { private ZombieLootDrops() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(ZombieLootDrops::onDrops); }
private static void onDrops(LivingDropsEvent event) { EntityType<?> mob = BuiltInRegistries.ENTITY_TYPE.get(new ResourceLocation("minecraft:zombie")); if (event.getEntity().getType() == mob && event.getEntity().getRandom().nextFloat() < 0.5F) { ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft:diamond"))); event.getDrops().add(new ItemEntity(event.getEntity().level(), event.getEntity().getX(), event.getEntity().getY(), event.getEntity().getZ(), stack)); } }}In your main mod class, inside <init>:
ZombieLootDrops.register();NeoForge 1.20.1
Section titled “NeoForge 1.20.1”Uses:
LivingDropsEvent.getDropsItemEntityBuiltInRegistries.ITEM
src/main/java/com/example/examplemod/event/ZombieLootDrops.java
package com.example.examplemod.event;
import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.item.ItemEntity;import net.minecraft.world.item.ItemStack;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.living.LivingDropsEvent;
// A minecraft:zombie drops minecraft:diamond on death with chance 0.5F (NeoForge: LivingDropsEvent adds to the drops list).public final class ZombieLootDrops { private ZombieLootDrops() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(ZombieLootDrops::onDrops); }
private static void onDrops(LivingDropsEvent event) { EntityType<?> mob = BuiltInRegistries.ENTITY_TYPE.get(new ResourceLocation("minecraft:zombie")); if (event.getEntity().getType() == mob && event.getEntity().getRandom().nextFloat() < 0.5F) { ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft:diamond"))); event.getDrops().add(new ItemEntity(event.getEntity().level(), event.getEntity().getX(), event.getEntity().getY(), event.getEntity().getZ(), stack)); } }}In your main mod class, inside <init>:
ZombieLootDrops.register();Fabric 1.19.2
Section titled “Fabric 1.19.2”Uses:
ServerLivingEntityEvents.AFTER_DEATHEntity.spawnAtLocationRegistry.ITEM
src/main/java/com/example/examplemod/event/ZombieLootDrops.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;import net.minecraft.core.Registry;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.EntityType;import net.minecraft.world.item.ItemStack;
// A minecraft:zombie drops minecraft:diamond on death with chance 0.5F (Fabric API: ServerLivingEntityEvents.AFTER_DEATH).public final class ZombieLootDrops { private ZombieLootDrops() { }
// Called from the mod's onInitialize. public static void register() { EntityType<?> mob = Registry.ENTITY_TYPE.get(new ResourceLocation("minecraft:zombie")); ServerLivingEntityEvents.AFTER_DEATH.register((entity, source) -> { if (entity.getType() == mob && entity.getRandom().nextFloat() < 0.5F) { entity.spawnAtLocation(new ItemStack(Registry.ITEM.get(new ResourceLocation("minecraft:diamond")))); } }); }}In your main mod class, inside onInitialize:
ZombieLootDrops.register();Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
LivingDropsEvent.getDropsItemEntityForgeRegistries.ITEMS
src/main/java/com/example/examplemod/event/ZombieLootDrops.java
package com.example.examplemod.event;
import java.util.Random;import net.minecraftforge.registries.ForgeRegistries;import net.minecraft.util.ResourceLocation;import net.minecraft.entity.EntityType;import net.minecraft.entity.item.ItemEntity;import net.minecraft.item.ItemStack;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.living.LivingDropsEvent;
// A minecraft:zombie drops minecraft:diamond on death with chance 0.5F (Forge: LivingDropsEvent adds to the drops list).public final class ZombieLootDrops { private ZombieLootDrops() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(ZombieLootDrops::onDrops); }
private static void onDrops(LivingDropsEvent event) { EntityType<?> mob = ForgeRegistries.ENTITIES.getValue(new ResourceLocation("minecraft:zombie")); if (event.getEntity().getType() == mob && new Random().nextFloat() < 0.5F) { ItemStack stack = new ItemStack(ForgeRegistries.ITEMS.getValue(new ResourceLocation("minecraft:diamond"))); event.getDrops().add(new ItemEntity(event.getEntity().level, event.getEntity().getX(), event.getEntity().getY(), event.getEntity().getZ(), stack)); } }}In your main mod class, inside <init>:
ZombieLootDrops.register();Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
LivingDropsEvent.getDropsEntityItemForgeRegistries.ITEMS
src/main/java/com/example/examplemod/event/ZombieLootDrops.java
package com.example.examplemod.event;
import com.example.examplemod.ExampleMod;import net.minecraft.entity.EntityLivingBase;import net.minecraft.entity.EntityList;import net.minecraft.entity.item.EntityItem;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.util.ResourceLocation;import net.minecraftforge.event.entity.living.LivingDropsEvent;import net.minecraftforge.fml.common.Mod;import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;import net.minecraftforge.fml.common.registry.ForgeRegistries;
// A minecraft:zombie drops minecraft:diamond on death with chance 0.5F (Forge: LivingDropsEvent adds to the drops list).@Mod.EventBusSubscriber(modid = ExampleMod.MOD_ID)public final class ZombieLootDrops { private ZombieLootDrops() { }
@SubscribeEvent public static void onDrops(LivingDropsEvent event) { EntityLivingBase mob = event.getEntityLiving(); if (new ResourceLocation("minecraft:zombie").equals(EntityList.getKey(mob)) && mob.getRNG().nextFloat() < 0.5F) { Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation("minecraft:diamond")); event.getDrops().add(new EntityItem(mob.world, mob.posX, mob.posY, mob.posZ, new ItemStack(item))); } }}Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.1/develop/events
- https://docs.neoforged.net/docs/1.21.1/entities/livingentity
- https://maven.fabricmc.net/docs/fabric-api-0.116.17+1.21.1/net/fabricmc/fabric/api/entity/event/v1/ServerLivingEntityEvents.html
Related how-tos
Section titled “Related how-tos”- An enchantment that heals you (lifesteal)
- Make players immune to a kind of damage
- A sword that sets targets on fire
- Add drops to Minecraft’s loot
- Share code between Fabric and NeoForge
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.