Skip to content

ModifyExpressionValue

LlamaLad7 edited this page May 19, 2022 · 4 revisions

Allows you to tweak the resultant value of a method call, field get, constant, or new object instantiation.

Your handler method receives the expression's resultant value (optionally followed by the enclosing method's parameters), and should return the adjusted value.

Should be used in favour of Redirects or ModifyConstants when you want to tweak an expression's existing value instead of replacing it entirely, as unlike Redirects and ModifyConstants, this chains when used by multiple people.

Example

When targeting code such as the following:

if (this.shouldFly()) {
	this.fly();
}

you may wish to add an extra condition, e.g. && YourMod.isFlyingAllowed()

This could be done like so:

@ModifyExpressionValue(
	method = "targetMethod", 
	at = @At(value = "INVOKE", target = "Ltarget/Class;shouldFly()Z")
)
private boolean onlyFlyIfAllowed(boolean original) {
	return original && YourMod.isFlyingAllowed();
}

Your handler method would then be called with the result of shouldFly, and you could prevent flying should you wish.

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

Code Diff

- if (this.shouldFly()) {
+ if (onlyFlyIfAllowed(this.shouldFly())) {