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.
- Write a @Mixin(LivingEntity.class) class.
- Add @Inject(method = “hurt”, at = @At(“HEAD”), cancellable = true) (use the method name of your version).
- Check the DamageSource and call setReturnValue(false).
- List the class in the mixins json and fall from a height.
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 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.setReturnValueLivingEntity.hurtDamageTypeTags.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"Fabric and NeoForge 1.21.2 to 26.3
Section titled “Fabric and NeoForge 1.21.2 to 26.3”Uses:
@Inject@At("HEAD")CallbackInfoReturnable.setReturnValueLivingEntity.hurtServerDamageTypeTags.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"Forge 1.17.1 to 1.19.3
Section titled “Forge 1.17.1 to 1.19.3”Uses:
@Inject@At("HEAD")CallbackInfoReturnable.setReturnValueLivingEntity.hurtDamageSource.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"Fabric 1.18 to 1.19.3
Section titled “Fabric 1.18 to 1.19.3”Uses:
@Inject@At("HEAD")CallbackInfoReturnable.setReturnValueLivingEntity.hurtDamageTypeTags.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"Fabric 1.16.5 to 1.17.1
Section titled “Fabric 1.16.5 to 1.17.1”Uses:
@Inject@At("HEAD")CallbackInfoReturnable.setReturnValueLivingEntity.hurtDamageTypeTags.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"Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
@Inject@At("HEAD")CallbackInfoReturnable.setReturnValueLivingEntity.hurtDamageSource.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"Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
@Inject@At("HEAD")CallbackInfoReturnable.setReturnValueEntityLivingBase.hurtDamageSource.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"Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.1/develop/events
- https://docs.neoforged.net/docs/1.21.1/gettingstarted/modfiles
Related how-tos
Section titled “Related how-tos”- Change what a method returns with a mixin
- See what my mixin did to the game’s code
- Change Minecraft’s code with a mixin
- Read a private field with a mixin accessor
- A mob that drops an item
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.