Skip to content

WrapWithCondition

LlamaLad7 edited this page Jan 12, 2022 · 3 revisions

Allows you to wrap a field write or void method call with a conditional check.

Your handler method receives the targeted instruction's arguments (optionally followed by the enclosing method's parameters), and should return a boolean indicating whether the operation should go ahead.

Should be used in favour of Redirects when you are simply wrapping the original call with a check, as unlike Redirects, this chains when used by multiple people.

Handler Methods

Targeted operation Handler signature
Non-static method call private boolean yourHandlerMethod(OwnerType instance, <arguments of the original call>)
Static method call private boolean yourHandlerMethod(<arguments of the original call>)
Non-static field write private boolean yourHandlerMethod(OwnerType instance, FieldType newValue)
Static field write private boolean yourHandlerMethod(FieldType newValue)

In all of these cases, you can optionally add the enclosing method's parameters to the end, should you require them for additional context.

Example

When targeting code such as the following:

this.render(this.tickDelta);

you may wish to wrap the call in a conditional check, e.g. if (YourMod.shouldRender())

This could be done like so:

@WrapWithCondition(
	method = "targetMethod", 
	at = @At(value = "INVOKE", target = "Lsome/package/TargetClass;render(F)V")
)
private boolean onlyRenderIfAllowed(TargetClass instance, float tickDelta) {
	return YourMod.shouldRender();
}

render would then only be called if your handler method returns true.

Multiple mods can do this at the same time, and all their conditions will be applied.

Code Diff

+ if (onlyRenderIfAllowed(this, this.tickDelta)) {
    this.render(this.tickDelta);
+ }
Clone this wiki locally