How to change what a method returns with a mixin (Minecraft mod)
@Inject with @At(“RETURN”) runs just before a method returns. For a method that returns a value the callback is a CallbackInfoReturnable: getReturnValue gives what the game computed, and setReturnValue replaces it. Set cancellable = true when you need to set it. This example doubles the mining speed, Player#getDestroySpeed (getDigSpeed on 1.12.2). @At(“HEAD”) runs before the method body instead.
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(Player.class) class.
- Add @Inject(method = “getDestroySpeed”, at = @At(“RETURN”), cancellable = true).
- Take a CallbackInfoReturnable<Float> and call setReturnValue.
- List the class in the mixins json and mine a block.
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 |
|---|---|---|
| multiplier | 2 |
How much faster. |
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, Fabric 1.16.5 to 1.20.3, Fabric and NeoForge 1.21.2 to 26.3, Forge 1.17.1 to 1.20.1
Section titled “Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Fabric 1.16.5 to 1.20.3, Fabric and NeoForge 1.21.2 to 26.3, Forge 1.17.1 to 1.20.1”Uses:
@Inject@At("RETURN")CallbackInfoReturnable.setReturnValuePlayer.getDestroySpeed
src/main/java/com/example/examplemod/mixin/FastMiningMixin.java
package com.example.examplemod.mixin;
import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.block.state.BlockState;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;
// Fast Mining: Player#getDestroySpeed returns the mining speed; this multiplies it by 2.0F before it is used.@Mixin(Player.class)public abstract class FastMiningMixin { @Inject(method = "getDestroySpeed", at = @At("RETURN"), cancellable = true) private void examplemod$faster(BlockState state, CallbackInfoReturnable<Float> info) { info.setReturnValue(info.getReturnValueF() * 2.0F); }}In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:
"FastMiningMixin"Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
@Inject@At("RETURN")CallbackInfoReturnable.setReturnValuePlayerEntity.getDestroySpeed
src/main/java/com/example/examplemod/mixin/FastMiningMixin.java
package com.example.examplemod.mixin;
import net.minecraft.entity.player.PlayerEntity;import net.minecraft.block.BlockState;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;
// Fast Mining: Player#getDestroySpeed returns the mining speed; this multiplies it by 2.0F before it is used.@Mixin(PlayerEntity.class)public abstract class FastMiningMixin { @Inject(method = "getDestroySpeed", at = @At("RETURN"), cancellable = true) private void examplemod$faster(BlockState state, CallbackInfoReturnable<Float> info) { info.setReturnValue(info.getReturnValueF() * 2.0F); }}In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:
"FastMiningMixin"Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
@Inject@At("RETURN")CallbackInfoReturnable.setReturnValueEntityPlayer.getDestroySpeed
In 1.12.2 the mining speed method is EntityPlayer#getDigSpeed, so the @Inject names “getDigSpeed”.
src/main/java/com/example/examplemod/mixin/FastMiningMixin.java
package com.example.examplemod.mixin;
import net.minecraft.block.state.IBlockState;import net.minecraft.entity.player.EntityPlayer;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;
// Fast Mining: EntityPlayer#getDigSpeed returns the mining speed; this multiplies it by 2.0F before it is used.@Mixin(EntityPlayer.class)public abstract class FastMiningMixin { @Inject(method = "getDigSpeed", at = @At("RETURN"), cancellable = true) private void examplemod$faster(IBlockState state, CallbackInfoReturnable<Float> info) { info.setReturnValue(info.getReturnValueF() * 2.0F); }}In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:
"FastMiningMixin"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”- See what my mixin did to the game’s code
- Stop a method from running with a mixin
- Read a private field with a mixin accessor
- Change Minecraft’s code with a mixin
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.