Skip to content

How to stop a method from running with a mixin (Minecraft mod)

@Inject with @At(“HEAD”) runs before the method. Set cancellable = true and call cir.setReturnValue(…) for a method that returns a value, or ci.cancel() for a void method: the rest of the method then never runs. This example makes the entity damage method (LivingEntity#hurt, hurtServer from 1.21.2, attackEntityFrom on 1.12.2) return false for fall damage, so the player takes none. Prefer an event when the loader has one; use a mixin when it does not.

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; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.

  1. Write a @Mixin(LivingEntity.class) class.
  2. Add @Inject(method = “hurt”, at = @At(“HEAD”), cancellable = true) (use the method name of your version).
  3. Check the DamageSource and call setReturnValue(false).
  4. List the class in the mixins json and fall from a height.

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

Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Forge 1.19.4 to 1.20.1, Fabric 1.19.4 to 1.20.3

Section titled “Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Forge 1.19.4 to 1.20.1, Fabric 1.19.4 to 1.20.3”

Uses:

  • @Inject
  • @At("HEAD")
  • CallbackInfoReturnable.setReturnValue
  • LivingEntity.hurt
  • DamageTypeTags.IS_FALL

src/main/java/com/example/examplemod/mixin/NoFallDamageMixin.java

package com.example.examplemod.mixin;
import net.minecraft.tags.DamageTypeTags;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
// No Fall Damage: players take no fall damage. hurt returns false at the head, so the damage code never runs.
@Mixin(LivingEntity.class)
public abstract class NoFallDamageMixin {
@Inject(method = "hurt", at = @At("HEAD"), cancellable = true)
private void examplemod$noFall(DamageSource source, float amount, CallbackInfoReturnable<Boolean> info) {
if ((Object) this instanceof Player && source.is(DamageTypeTags.IS_FALL)) {
info.setReturnValue(false);
}
}
}

In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:

"NoFallDamageMixin"

Uses:

  • @Inject
  • @At("HEAD")
  • CallbackInfoReturnable.setReturnValue
  • LivingEntity.hurtServer
  • DamageTypeTags.IS_FALL

Since 1.21.2 the method to inject into is hurtServer(ServerLevel, DamageSource, float), so its callback takes the level as well.

src/main/java/com/example/examplemod/mixin/NoFallDamageMixin.java

package com.example.examplemod.mixin;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.DamageTypeTags;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
// No Fall Damage: players take no fall damage. hurtServer returns false at the head, so the damage code never runs.
@Mixin(LivingEntity.class)
public abstract class NoFallDamageMixin {
@Inject(method = "hurtServer", at = @At("HEAD"), cancellable = true)
private void examplemod$noFall(ServerLevel level, DamageSource source, float amount, CallbackInfoReturnable<Boolean> info) {
if ((Object) this instanceof Player && source.is(DamageTypeTags.IS_FALL)) {
info.setReturnValue(false);
}
}
}

In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:

"NoFallDamageMixin"

Uses:

  • @Inject
  • @At("HEAD")
  • CallbackInfoReturnable.setReturnValue
  • LivingEntity.hurt
  • DamageSource.isFall

src/main/java/com/example/examplemod/mixin/NoFallDamageMixin.java

package com.example.examplemod.mixin;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
// No Fall Damage: players take no fall damage. hurt returns false at the head, so the damage code never runs.
@Mixin(LivingEntity.class)
public abstract class NoFallDamageMixin {
@Inject(method = "hurt", at = @At("HEAD"), cancellable = true)
private void examplemod$noFall(DamageSource source, float amount, CallbackInfoReturnable<Boolean> info) {
if ((Object) this instanceof Player && source.isFall()) {
info.setReturnValue(false);
}
}
}

In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:

"NoFallDamageMixin"

Uses:

  • @Inject
  • @At("HEAD")
  • CallbackInfoReturnable.setReturnValue
  • LivingEntity.hurt
  • DamageTypeTags.IS_FALL

src/main/java/com/example/examplemod/mixin/NoFallDamageMixin.java

package com.example.examplemod.mixin;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
// No Fall Damage: players take no fall damage. hurt returns false at the head, so the damage code never runs.
@Mixin(LivingEntity.class)
public abstract class NoFallDamageMixin {
@Inject(method = "hurt", at = @At("HEAD"), cancellable = true)
private void examplemod$noFall(DamageSource source, float amount, CallbackInfoReturnable<Boolean> info) {
if ((Object) this instanceof Player && source.isFall()) {
info.setReturnValue(false);
}
}
}

In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:

"NoFallDamageMixin"

Uses:

  • @Inject
  • @At("HEAD")
  • CallbackInfoReturnable.setReturnValue
  • LivingEntity.hurt
  • DamageTypeTags.IS_FALL

src/main/java/com/example/examplemod/mixin/NoFallDamageMixin.java

package com.example.examplemod.mixin;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
// No Fall Damage: players take no fall damage. hurt returns false at the head, so the damage code never runs.
@Mixin(LivingEntity.class)
public abstract class NoFallDamageMixin {
@Inject(method = "hurt", at = @At("HEAD"), cancellable = true)
private void examplemod$noFall(DamageSource source, float amount, CallbackInfoReturnable<Boolean> info) {
if ((Object) this instanceof Player && source == DamageSource.FALL) {
info.setReturnValue(false);
}
}
}

In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:

"NoFallDamageMixin"

Uses:

  • @Inject
  • @At("HEAD")
  • CallbackInfoReturnable.setReturnValue
  • LivingEntity.hurt
  • DamageSource.isFall

src/main/java/com/example/examplemod/mixin/NoFallDamageMixin.java

package com.example.examplemod.mixin;
import net.minecraft.util.DamageSource;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
// No Fall Damage: players take no fall damage. hurt returns false at the head, so the damage code never runs.
@Mixin(LivingEntity.class)
public abstract class NoFallDamageMixin {
@Inject(method = "hurt", at = @At("HEAD"), cancellable = true)
private void examplemod$noFall(DamageSource source, float amount, CallbackInfoReturnable<Boolean> info) {
if ((Object) this instanceof PlayerEntity && source == DamageSource.FALL) {
info.setReturnValue(false);
}
}
}

In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:

"NoFallDamageMixin"

Uses:

  • @Inject
  • @At("HEAD")
  • CallbackInfoReturnable.setReturnValue
  • EntityLivingBase.hurt
  • DamageSource.isFall

In 1.12.2 the method is EntityLivingBase#attackEntityFrom(DamageSource, float), so the @Inject names “attackEntityFrom”.

src/main/java/com/example/examplemod/mixin/NoFallDamageMixin.java

package com.example.examplemod.mixin;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.DamageSource;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
// No Fall Damage: players take no fall damage. attackEntityFrom returns false at the head, so the damage code never runs.
@Mixin(EntityLivingBase.class)
public abstract class NoFallDamageMixin {
@Inject(method = "attackEntityFrom", at = @At("HEAD"), cancellable = true)
private void examplemod$noFall(DamageSource source, float amount, CallbackInfoReturnable<Boolean> info) {
if ((Object) this instanceof EntityPlayer && source == DamageSource.FALL) {
info.setReturnValue(false);
}
}
}

In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:

"NoFallDamageMixin"

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.