Skip to content

How to react when something takes damage in a Minecraft mod

Before damage is applied the loader asks your mod. Fabric API’s ServerLivingEntityEvents.ALLOW_DAMAGE returns false to cancel it. NeoForge 1.21 and newer has LivingIncomingDamageEvent, which can be cancelled or change the amount; Forge and older NeoForge have LivingAttackEvent (cancel) and LivingHurtEvent (change the amount). On 1.19.4 and newer damage sources are grouped by damage type tags such as DamageTypeTags.IS_FALL, so you test the kind with source.is(tag); before that, ask the source with helpers such as isFall().

Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.19.2 to 26.3 on Fabric; Minecraft 1.17.1 to 1.20.1 on Forge. Each example was compiled against its loader and version before it shipped in Linen.

  1. Listen to the damage event when your mod starts.
  2. Check the entity and the damage source (source.is(DamageTypeTags.IS_FALL) on 1.19.4 and newer).
  3. Cancel the damage, or change its amount.

The example uses these values. Change them in the code.

Setting In the example What it does
damage IS_FALL The kind of damage players no longer take (a damage type tag).

The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.

Fabric 1.19.4 to 1.21.1, Fabric 1.21.2 to 26.3

Section titled “Fabric 1.19.4 to 1.21.1, Fabric 1.21.2 to 26.3”

Uses:

  • ServerLivingEntityEvents.ALLOW_DAMAGE
  • DamageSource.is
  • DamageTypeTags

src/main/java/com/example/examplemod/event/SafeLandingDamageEvents.java

package com.example.examplemod.event;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.minecraft.tags.DamageTypeTags;
import net.minecraft.world.entity.player.Player;
// Asked on the server before a living entity takes damage; false cancels it (Fabric API: ServerLivingEntityEvents.ALLOW_DAMAGE).
public final class SafeLandingDamageEvents {
private SafeLandingDamageEvents() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerLivingEntityEvents.ALLOW_DAMAGE.register((entity, source, amount) -> !(entity instanceof Player && source.is(DamageTypeTags.IS_FALL)));
}
}

In your main mod class, inside onInitialize:

SafeLandingDamageEvents.register();

NeoForge 1.21 to 1.21.1, NeoForge 1.21.2 to 26.3

Section titled “NeoForge 1.21 to 1.21.1, NeoForge 1.21.2 to 26.3”

Uses:

  • LivingIncomingDamageEvent
  • DamageSource.is
  • DamageTypeTags

src/main/java/com/example/examplemod/event/SafeLandingDamageEvents.java

package com.example.examplemod.event;
import net.minecraft.tags.DamageTypeTags;
import net.minecraft.world.entity.player.Player;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.living.LivingIncomingDamageEvent;
// Fired on the server before a living entity takes damage; cancel it or change the amount (NeoForge: LivingIncomingDamageEvent).
public final class SafeLandingDamageEvents {
private SafeLandingDamageEvents() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(SafeLandingDamageEvents::onIncomingDamage);
}
private static void onIncomingDamage(LivingIncomingDamageEvent event) {
if (event.getEntity() instanceof Player && event.getSource().is(DamageTypeTags.IS_FALL)) {
event.setCanceled(true);
}
}
}

In your main mod class, inside <init>:

SafeLandingDamageEvents.register();

Uses:

  • LivingHurtEvent
  • DamageSource.is
  • DamageTypeTags

src/main/java/com/example/examplemod/event/SafeLandingDamageEvents.java

package com.example.examplemod.event;
import net.minecraft.tags.DamageTypeTags;
import net.minecraft.world.entity.player.Player;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.living.LivingHurtEvent;
// Fired on the server before a living entity takes damage; cancel it or change the amount (NeoForge: LivingHurtEvent).
public final class SafeLandingDamageEvents {
private SafeLandingDamageEvents() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(SafeLandingDamageEvents::onIncomingDamage);
}
private static void onIncomingDamage(LivingHurtEvent event) {
if (event.getEntity() instanceof Player && event.getSource().is(DamageTypeTags.IS_FALL)) {
event.setCanceled(true);
}
}
}

In your main mod class, inside <init>:

SafeLandingDamageEvents.register();

Uses:

  • LivingAttackEvent
  • DamageSource.isFall

src/main/java/com/example/examplemod/event/SafeLandingDamageEvents.java

package com.example.examplemod.event;
import java.util.function.Predicate;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
// Fired on the server before a living entity takes damage; cancel it or change the amount (Forge: LivingAttackEvent; LivingHurtEvent changes the amount).
public final class SafeLandingDamageEvents {
private SafeLandingDamageEvents() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(SafeLandingDamageEvents::onAttack);
}
private static void onAttack(LivingAttackEvent event) {
if (event.getEntity() instanceof Player && DamageTypeTags.IS_FALL.test(event.getSource())) {
event.setCanceled(true);
}
}
// Damage type tags only came in 1.19.4. Before that a DamageSource is a constant or has an isXxx check, so this holds the same kinds by name.
private static final class DamageTypeTags {
static final Predicate<DamageSource> IS_FALL = DamageSource::isFall;
static final Predicate<DamageSource> IS_FIRE = DamageSource::isFire;
static final Predicate<DamageSource> IS_DROWNING = source -> source == DamageSource.DROWN;
static final Predicate<DamageSource> IS_EXPLOSION = DamageSource::isExplosion;
static final Predicate<DamageSource> IS_PROJECTILE = DamageSource::isProjectile;
static final Predicate<DamageSource> IS_FREEZING = source -> source == DamageSource.FREEZE;
static final Predicate<DamageSource> IS_LIGHTNING = source -> source == DamageSource.LIGHTNING_BOLT;
}
}

In your main mod class, inside <init>:

SafeLandingDamageEvents.register();

Uses:

  • LivingAttackEvent
  • LivingHurtEvent
  • DamageSource.is

src/main/java/com/example/examplemod/event/SafeLandingDamageEvents.java

package com.example.examplemod.event;
import net.minecraft.tags.DamageTypeTags;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
// Fired on the server before a living entity takes damage; cancel it or change the amount (Forge: LivingAttackEvent; LivingHurtEvent changes the amount).
public final class SafeLandingDamageEvents {
private SafeLandingDamageEvents() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(SafeLandingDamageEvents::onAttack);
}
private static void onAttack(LivingAttackEvent event) {
if (event.getEntity() instanceof Player && event.getSource().is(DamageTypeTags.IS_FALL)) {
event.setCanceled(true);
}
}
}

In your main mod class, inside <init>:

SafeLandingDamageEvents.register();

Uses:

  • LivingAttackEvent
  • LivingHurtEvent
  • DamageSource.is

src/main/java/com/example/examplemod/event/SafeLandingDamageEvents.java

package com.example.examplemod.event;
import net.minecraft.tags.DamageTypeTags;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
// Fired on the server before a living entity takes damage; cancel it or change the amount (NeoForge: LivingAttackEvent; LivingHurtEvent changes the amount).
public final class SafeLandingDamageEvents {
private SafeLandingDamageEvents() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(SafeLandingDamageEvents::onAttack);
}
private static void onAttack(LivingAttackEvent event) {
if (event.getEntity() instanceof Player && event.getSource().is(DamageTypeTags.IS_FALL)) {
event.setCanceled(true);
}
}
}

In your main mod class, inside <init>:

SafeLandingDamageEvents.register();

Uses:

  • ServerLivingEntityEvents.ALLOW_DAMAGE
  • DamageSource.is
  • DamageTypeTags

src/main/java/com/example/examplemod/event/SafeLandingDamageEvents.java

package com.example.examplemod.event;
import java.util.function.Predicate;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.player.Player;
// Asked on the server before a living entity takes damage; false cancels it (Fabric API: ServerLivingEntityEvents.ALLOW_DAMAGE).
public final class SafeLandingDamageEvents {
private SafeLandingDamageEvents() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerLivingEntityEvents.ALLOW_DAMAGE.register((entity, source, amount) -> !(entity instanceof Player && DamageTypeTags.IS_FALL.test(source)));
}
// Damage type tags only came in 1.19.4. Before that a DamageSource is a constant or has an isXxx check, so this holds the same kinds by name.
private static final class DamageTypeTags {
static final Predicate<DamageSource> IS_FALL = DamageSource::isFall;
static final Predicate<DamageSource> IS_FIRE = DamageSource::isFire;
static final Predicate<DamageSource> IS_DROWNING = source -> source == DamageSource.DROWN;
static final Predicate<DamageSource> IS_EXPLOSION = DamageSource::isExplosion;
static final Predicate<DamageSource> IS_PROJECTILE = DamageSource::isProjectile;
static final Predicate<DamageSource> IS_FREEZING = source -> source == DamageSource.FREEZE;
static final Predicate<DamageSource> IS_LIGHTNING = source -> source == DamageSource.LIGHTNING_BOLT;
}
}

In your main mod class, inside onInitialize:

SafeLandingDamageEvents.register();

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.