Skip to content
Bawnorton edited this page Aug 22, 2023 · 26 revisions

Target Handler

@TargetHandler allows any injector to target a handler that was added to the mixin'd class.

The handler takes in 3 arguments:

  • mixin: The fully qualified class name for the mixin the handler originates from.
  • name: The name of the handler method.
    • This must be the exact name of the handler in the target mixin, if the handler is prefixed with the modid, then the name argument must also.
  • prefix: The prefix added to the front of handler methods by Mixin
    • Optional, will match all prefixes if not specifed
    • See the table below for the prefixes

When targetting a mixin'd handler with an injector, set the method parameter to @MixinSquared:Handler to delegate the targetting to the dynamic selector.

Example:

Let's say the following mixin exists:

@Mixin(TargetClass.class)
public abstract class TargetClassMixin {
    @Redirect(
        method = "targetMethod", 
        at = @At(
            value = "INVOKE", 
            target = "Ltarget/Class;damage(Lpackage/to/class/DamageSource;F)Z"
        )
    )
    private boolean handleDamage(TargetClass instance, DamageSource source, float amount) {
        if(!AnotherMod.handleDamage(source, amount) {
            LOGGER.warn("Could not handle damage");
        }
    }
}

You may want to stop this mixin from always giving a warning when the mod doesn't handle the damage, and instead reduce the level to debug.

This can be done like so:

@Mixin(value = TargetClass.class, priority = 1500) // priority is higher than the target mixin
public abstract class TargetClassMixinSquared {
    @TargetHandler(
        mixin = "com.bawnorton.mixin.TargetClassMixin", 
        name = "handleDamage"
    )
    @Redirect(
        method = "@MixinSquared:Handler", 
        at = @At(
            value = "INVOKE", 
            target = "org/slf4j/Logger.warn(Ljava/lang/String;)V"
        )
    )
    private void reduceLogLevel(Logger instance, String message) {
        instance.debug(message);
    }
}

The target handler can target other mod's mixin squared mixins as long as the priority is higher.

Prefixes:

Annotation Prefix
@Inject handler
@ModifyArg modify
@ModifyArgs args
@ModifyConstant constant
@ModifyVariable localvar
@Redirect redirect
Mixin Extras
@ModifyExpressionValue modifyExpressionValue
@ModifyReciever modifyReciever
@ModifyReturnValue modifyReturnValue
@WrapWithCondition wrapWithCondition
@WrapOperation wrapOperation
Clone this wiki locally