tinycodinglm / mc /mc-0011.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame Contribute Delete
2.82 kB
opencode trace
System 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.

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
)
private float modifyVolume(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
)
private int modifyTickSpeed(int original) {
    return Math.max(1, original / 2);
}

Bytecode - inserts at variable assignment point:

bytecode
// At HEAD, before any use of local variable 1 (ordinal 0)
ILOAD 1
INVOKESTATIC MyMixin.modifyTickSpeed(I)I
ISTORE 1

@Redirect - Replaces an entire method invocation:

java
@Redirect(
    method = "tick",
    at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;getHealth()F")
)
private float redirectGetHealth(LivingEntity entity) {
    return entity.getHealth() * this.healthMultiplier; // Custom health calculation
}

Bytecode - replaces the entire invoke instruction:

bytecode
// Before:
ALOAD 0
INVOKEVIRTUAL LivingEntity.getHealth()F

// After:
ALOAD 0  // target instance
INVOKESTATIC MyMixin.redirectGetHealth(LivingEntity;)F

Decision Guide:

  • @ModifyArg: Changing one argument of a method call (keep the call, change inputs)
  • @ModifyVariable: Changing a local variable value (affects all downstream uses)
  • @Redirect: Replacing the entire method call with custom logic (complete override of the call)

@Redirect is more powerful but can conflict with other mixins targeting the same call site. @ModifyArg is safer for simple argument tweaks.