Skip to content

ModifyReceiver

LlamaLad7 edited this page Aug 28, 2023 · 3 revisions

Allows you to modify the receiver of a non-static method call or field get/set.

Your handler method receives the targeted instruction's arguments (optionally followed by the enclosing method's parameters), and should return the adjusted receiver for the operation.

Should be used in favour of Redirects when you are simply inspecting or conditionally modifying the receiver of an operation, as unlike Redirects, this chains when used by multiple people.

Handler Methods

Targeted operation Handler signature
Non-static method call private OwnerType yourHandlerMethod(OwnerType receiver, <arguments of the original call>)
super. method call private ThisClass yourHandlerMethod(ThisClass receiver, <arguments of the original call>)
Non-static field write private OwnerType yourHandlerMethod(OwnerType receiver, FieldType newValue)
Non-static field read private OwnerType yourHandlerMethod(OwnerType receiver)

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:

object1.setX(newXPosition);

you may wish to inspect or change the object being moved.

This could be done like so:

@ModifyReceiver(
	method = "targetMethod", 
	at = @At(value = "INVOKE", target = "Lsome/package/TargetClass;setX(I)V")
)
private TargetClass changeObject(TargetClass receiver, int newX) {
        if (newX == 10) {
	        return object2;
        }
        return receiver;
}

setX would then be called on object2 instead, if you so desired.

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

Code Diff

- object1.setX(10);
+ changeObject(object1, 10).setX(10);