WrapMethod - LlamaLad7/MixinExtras GitHub Wiki

Allows you to wrap a whole method. To wrap individual operations within a method, including method calls, see WrapOperation.

Your handler method receives the target method's arguments and an Operation representing the method being wrapped. You should return the same type as the wrapped operation does.

When calling the original, you must pass everything before the original in your handler's parameters. You can optionally pass different values to change what the original uses.

This has 2 main use-cases:

  1. Wrapping a method in a try/catch, synchronization, async dispatch, etc.
  2. Running code at the start and end of a method even if someone else cancels it.

While this injector in general does not support Sugar, it does have special support for using Share to share values between wrappers and the target method.

Example

When targeting a method such as the following:

public int riskyCalculation(String someParam) throws SomeException {
	// Some logic using someParam
}

you may wish to wrap the method so it returns a sensible default instead of throwing.

This could be done like so:

@WrapMethod(method = "riskyCalculation")
private int guardRiskyCalculation(String someParam, Operation<Integer> original) {
	try {
		return original.call(someParam);
	} catch (SomeException ignored) {
		return 0;
	}
}

riskyCalculation would then always be wrapped in your logic, regardless of who calls it.

Multiple mods can do this at the same time, and the wrapping will chain. The first to be applied receives an Operation representing the vanilla method, if another is applied it receives an Operation representing the first one's wrapper, etc.

Code Diff

  public int riskyCalculation(String someParam) throws SomeException {
+	  return this.guardRiskyCalculation(someParam, args -> {
-		  // Some logic using someParam
+		  // Some logic using args[0]
+	  });
  }
⚠️ **GitHub.com Fallback** ⚠️