Skip to content

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.

  1. Write a @Mixin(Player.class) class.
  2. Add @Inject(method = “getDestroySpeed”, at = @At(“RETURN”), cancellable = true).
  3. Take a CallbackInfoReturnable<Float> and call setReturnValue.
  4. List the class in the mixins json and mine a block.

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

Setting In the example What it does
multiplier 2 How much faster.

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.setReturnValue
  • Player.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"

Uses:

  • @Inject
  • @At("RETURN")
  • CallbackInfoReturnable.setReturnValue
  • PlayerEntity.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"

Uses:

  • @Inject
  • @At("RETURN")
  • CallbackInfoReturnable.setReturnValue
  • EntityPlayer.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"

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.