-
Notifications
You must be signed in to change notification settings - Fork 2
Home
Bawnorton edited this page Aug 22, 2023
·
26 revisions
The @TargetHandler
selector allows any injector to target a handler that was added to the mixin'd class.
The handler takes in 2 required and 2 optional arguments:
-
mixin
: The fully qualified class name for the mixin the handler originates from.- Required
-
name
: The name of the handler method.- Required
- 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 the first prefix if not specified.
- See the table below for the prefixes.
-
print
: Print all possible candidates for the selector- Optional
- Due to the limitations of selectors this will print until the first match is found.
When targetting a mixin'd handler with an injector, set the method parameter to @MixinSquared:Handler
to delegate the targeting to the dynamic selector.
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.
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 |