Skip to content

ModifyReturnValue

LlamaLad7 edited this page Nov 12, 2023 · 2 revisions

NB: Not to be confused with ModifyExpressionValue

Allows you to tweak the value being returned from a method.

Your handler method receives the value about to be returned (optionally followed by the enclosing method's parameters), and should return the adjusted value.

Should be used in favour of Injects with the cir.setReturnValue(modify(cir.getReturnValue())) pattern, as that pattern does not chain when multiple people do it, whereas this does.

Example

When targeting code such as the following:

return this.speed * 10f - 0.5f;

you may wish to change the value being returned, e.g. by dividing it by 2.

This could be done like so:

@ModifyReturnValue(
	method = "targetMethod", 
	at = @At("RETURN")
)
private float halveSpeed(float original) {
	return original / 2f;
}

Your handler method would then be called with the result of the existing calculation, and you could change the value as it is returned.

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

Code Diff

- return this.speed * 10f - 0.5f;
+ return halveSpeed(this.speed * 10f - 0.5f);
Clone this wiki locally