Skip to content

How to make players immune to a kind of damage (Minecraft mod)

A damage event fires before an entity takes the hit. Fabric API: ServerLivingEntityEvents.ALLOW_DAMAGE, where returning false cancels the hit. NeoForge 1.21 and newer: LivingIncomingDamageEvent, which you cancel or change with setAmount. Forge and older NeoForge: LivingAttackEvent or LivingHurtEvent, which you cancel. On 1.19.4 and newer DamageSource.is(DamageTypes.X) tells you the kind: FALL, ON_FIRE, DROWN, LAVA, MAGIC. Run it on the server; these events only fire there.

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.

  1. Register the damage listener for your loader.
  2. Test that the entity is a player and the source is your type.
  3. Cancel the hit (return false on Fabric, cancel the event on Forge and NeoForge).
  4. Fall from high up and check your health.

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

Setting In the example What it does
type FALL The damage that does nothing.

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
  • DamageTypes
  • DamageSource.is

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

package com.example.examplemod.event;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.minecraft.world.damagesource.DamageTypes;
import net.minecraft.world.entity.player.Player;
// Players take no FALL damage (Fabric API: ServerLivingEntityEvents.ALLOW_DAMAGE; false cancels the hit).
public final class SafePlayerImmunity {
private SafePlayerImmunity() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerLivingEntityEvents.ALLOW_DAMAGE.register((entity, source, amount) -> !(entity instanceof Player && source.is(DamageTypes.FALL)));
}
}

In your main mod class, inside onInitialize:

SafePlayerImmunity.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
  • DamageTypes
  • DamageSource.is

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

package com.example.examplemod.event;
import net.minecraft.world.damagesource.DamageTypes;
import net.minecraft.world.entity.player.Player;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.living.LivingIncomingDamageEvent;
// Players take no FALL damage (NeoForge: LivingIncomingDamageEvent; cancelling it stops the hit).
public final class SafePlayerImmunity {
private SafePlayerImmunity() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(SafePlayerImmunity::onDamage);
}
private static void onDamage(LivingIncomingDamageEvent event) {
if (event.getEntity() instanceof Player && event.getSource().is(DamageTypes.FALL)) {
event.setCanceled(true);
}
}
}

In your main mod class, inside <init>:

SafePlayerImmunity.register();

Uses:

  • LivingHurtEvent
  • DamageTypes
  • DamageSource.is

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

package com.example.examplemod.event;
import net.minecraft.world.damagesource.DamageTypes;
import net.minecraft.world.entity.player.Player;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.living.LivingHurtEvent;
// Players take no FALL damage (NeoForge: LivingHurtEvent; cancelling it stops the hit).
public final class SafePlayerImmunity {
private SafePlayerImmunity() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(SafePlayerImmunity::onDamage);
}
private static void onDamage(LivingHurtEvent event) {
if (event.getEntity() instanceof Player && event.getSource().is(DamageTypes.FALL)) {
event.setCanceled(true);
}
}
}

In your main mod class, inside <init>:

SafePlayerImmunity.register();

Uses:

  • LivingAttackEvent
  • DamageSource

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

package com.example.examplemod.event;
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;
// Players take no FALL damage (Forge: LivingAttackEvent; cancelling it stops the hit).
public final class SafePlayerImmunity {
private SafePlayerImmunity() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(SafePlayerImmunity::onDamage);
}
private static void onDamage(LivingAttackEvent event) {
if (event.getEntity() instanceof Player && event.getSource() == DamageSource.FALL) {
event.setCanceled(true);
}
}
}

In your main mod class, inside <init>:

SafePlayerImmunity.register();

Uses:

  • LivingAttackEvent
  • DamageSource.is
  • DamageTypes

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

package com.example.examplemod.event;
import net.minecraft.world.damagesource.DamageTypes;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
// Players take no FALL damage (Forge: LivingAttackEvent; cancelling it stops the hit).
public final class SafePlayerImmunity {
private SafePlayerImmunity() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(SafePlayerImmunity::onDamage);
}
private static void onDamage(LivingAttackEvent event) {
if (event.getEntity() instanceof Player && event.getSource().is(DamageTypes.FALL)) {
event.setCanceled(true);
}
}
}

In your main mod class, inside <init>:

SafePlayerImmunity.register();

Uses:

  • LivingAttackEvent
  • DamageSource.is
  • DamageTypes

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

package com.example.examplemod.event;
import net.minecraft.world.damagesource.DamageTypes;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
// Players take no FALL damage (NeoForge: LivingAttackEvent; cancelling it stops the hit).
public final class SafePlayerImmunity {
private SafePlayerImmunity() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(SafePlayerImmunity::onDamage);
}
private static void onDamage(LivingAttackEvent event) {
if (event.getEntity() instanceof Player && event.getSource().is(DamageTypes.FALL)) {
event.setCanceled(true);
}
}
}

In your main mod class, inside <init>:

SafePlayerImmunity.register();

Uses:

  • ServerLivingEntityEvents.ALLOW_DAMAGE
  • DamageSource
  • DamageSource.is

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

package com.example.examplemod.event;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.player.Player;
// Players take no FALL damage (Fabric API: ServerLivingEntityEvents.ALLOW_DAMAGE; false cancels the hit).
public final class SafePlayerImmunity {
private SafePlayerImmunity() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerLivingEntityEvents.ALLOW_DAMAGE.register((entity, source, amount) -> !(entity instanceof Player && source == DamageSource.FALL));
}
}

In your main mod class, inside onInitialize:

SafePlayerImmunity.register();

Uses:

  • LivingAttackEvent
  • DamageSource

Before 1.19.4 there are no damage type tags: ask the source with a helper such as isFall() or compare it with a DamageSource constant such as DamageSource.FALL.

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

package com.example.examplemod.event;
import net.minecraft.util.DamageSource;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
// Players take no FALL damage (Forge: LivingAttackEvent; cancelling it stops the hit).
public final class SafePlayerImmunity {
private SafePlayerImmunity() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(SafePlayerImmunity::onDamage);
}
private static void onDamage(LivingAttackEvent event) {
if (event.getEntity() instanceof PlayerEntity && event.getSource() == DamageSource.FALL) {
event.setCanceled(true);
}
}
}

In your main mod class, inside <init>:

SafePlayerImmunity.register();

Uses:

  • LivingAttackEvent
  • DamageSource

Before 1.19.4 there are no damage type tags: ask the source with a helper such as isFireDamage() or compare it with a DamageSource constant such as DamageSource.FALL.

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

package com.example.examplemod.event;
import com.example.examplemod.ExampleMod;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.DamageSource;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
// Players take no FALL damage (Forge: LivingAttackEvent; cancelling it stops the hit).
@Mod.EventBusSubscriber(modid = ExampleMod.MOD_ID)
public final class SafePlayerImmunity {
private SafePlayerImmunity() {
}
@SubscribeEvent
public static void onDamage(LivingAttackEvent event) {
if (event.getEntityLiving() instanceof EntityPlayer && event.getSource() == DamageSource.FALL) {
event.setCanceled(true);
}
}
}

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.