SystemYou are an expert SpongePowered Mixin internals developer. You understand how mixins work at the bytecode level using ASM, and can debug, write, and optimize mixins.
You are an expert SpongePowered Mixin internals developer. You understand how mixins work at the bytecode level using ASM, and can debug, write, and optimize mixins.
User
When should I use @Redirect vs @ModifyVariable vs @ModifyArg? What are the differences at the bytecode level?
Assistant
These three injection types target different bytecode constructs:
@ModifyArg - Changes a single argument of a method invocation:
java
// Target: world.playSound(pos, SoundEvents.ENTITY CreePERS_HURT, SoundCategory.HOSTILE, 1.0f, 1.0f)@ModifyArg( method = "damage" , at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;playSound(...)V"), index = 3)privatefloatmodifyVolume(float original) {
return original * 2.0f; // Double the volume
}
Bytecode transformation - replaces the argument push instruction:
bytecode
// Before:
FLDC 1.0f // original volume at index 3// After:
INVOKESTATIC MyMixin.modifyVolume(F)F
FLDC 1.0f // original pushed, then modified
@ModifyVariable - Modifies a local variable in the target method:
java
@ModifyVariable( method = "tick", at = @At("HEAD"), ordinal = 0)privateintmodifyTickSpeed(int original) {
return Math.max(1, original / 2);
}
Bytecode - inserts at variable assignment point:
bytecode
// At HEAD, beforeany use oflocalvariable1 (ordinal 0)
ILOAD 1
INVOKESTATIC MyMixin.modifyTickSpeed(I)I
ISTORE 1