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.
- Register the damage listener for your loader.
- Test that the entity is a player and the source is your type.
- Cancel the hit (return false on Fabric, cancel the event on Forge and NeoForge).
- Fall from high up and check your health.
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 |
|---|---|---|
| type | FALL |
The damage that does nothing. |
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.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_DAMAGEDamageTypesDamageSource.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:
LivingIncomingDamageEventDamageTypesDamageSource.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();NeoForge 1.20.2 to 1.20.6
Section titled “NeoForge 1.20.2 to 1.20.6”Uses:
LivingHurtEventDamageTypesDamageSource.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();Forge 1.17.1 to 1.19.3
Section titled “Forge 1.17.1 to 1.19.3”Uses:
LivingAttackEventDamageSource
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();Forge 1.19.4 to 1.20.1
Section titled “Forge 1.19.4 to 1.20.1”Uses:
LivingAttackEventDamageSource.isDamageTypes
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();NeoForge 1.20.1
Section titled “NeoForge 1.20.1”Uses:
LivingAttackEventDamageSource.isDamageTypes
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();Fabric 1.19.2 to 1.19.3
Section titled “Fabric 1.19.2 to 1.19.3”Uses:
ServerLivingEntityEvents.ALLOW_DAMAGEDamageSourceDamageSource.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();Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
LivingAttackEventDamageSource
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();Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
LivingAttackEventDamageSource
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); } }}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”- A mob that drops an item
- An enchantment that heals you (lifesteal)
- Define a damage type
- Do something when a player joins
- Give players items when they first join
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.