-
Notifications
You must be signed in to change notification settings - Fork 18
Cancellable
Allows you to receive a cancellable CallbackInfo
or CallbackInfoReturnable
as appropriate from any kind of injector. This allows you to optionally cancel the target method without being forced to use @Inject
.
Possible parameters could look like:
-
@Cancellable CallbackInfo ci
(for avoid
method) -
@Cancellable CallbackInfoReturnable<Integer> cir
(for aint
orInteger
method)
When targeting code such as the following:
public void renderHeart(MatrixStack matrices, HeartType type) {
Identifier texture = type.getTexture();
// ... Draw logic using `texture`
}
you may wish to conditionally skip the rendering based on the texture.
This could be done like so:
@ModifyExpressionValue(method = "renderHeart", at = @At(value = "INVOKE", target = "Lsome/package/HeartType;getTexture()Lsome/package/Identifier;"))
private Identifier dontRenderPoisonHearts(Identifier texture, @Cancellable CallbackInfo ci) {
if (texture.equals(Textures.POSION_HEART)) {
ci.cancel();
}
return texture;
}
The target method would then return early if the texture matches.
public void targetMethod() {
+ CallbackInfo ci = new CallbackInfo(...);
- Identifier texture = type.getTexture();
+ Identifier texture = dontRenderPoisonHearts(type.getTexture(), ci);
+ if (ci.isCancelled()) {
+ return;
+ }
// ... Draw logic using `texture`
}
The same CallbackInfo(Returnable)
s will be passed to every handler method in a chain of @WrapOperation
s (i.e. any number of @WrapOperation
s and at most one inner @Redirect
/ @ModifyConstant
). This means you can choose to use the CallbackInfo#isCancelled()
and CallbackInfoReturnable#getReturnValue()
methods to see if the wrapped handler cancelled, so you can respond accordingly.