User Tools

Site Tools


tutorial:mixin_examples

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
tutorial:mixin_examples [2025/10/12 09:21] – Mark page as having a planned rewrite gauntreclusetutorial:mixin_examples [2026/04/08 11:56] (current) – [Modifying a return value] Use ModifyReturnValue instead of the getReturnValue + setReturnValue pattern. gauntrecluse
Line 349: Line 349:
 Mixin: Mixin:
 <code java> <code java>
-@Inject(method = "foo()I;", at = @At("RETURN"), cancellable = true+@ModifyReturnValue(method = "foo", at = @At("RETURN")) 
-private void injected(CallbackInfoReturnable<Integer> cir) { +private int exampleModifyReturnValue(int original) { 
-  cir.setReturnValue(cir.getReturnValue() * 3);+    return original * 3;
 } }
 </code> </code>
Line 357: Line 357:
 Result: Result:
 <code diff> <code diff>
-  public int foo() {+public int foo() {
     doSomething1();     doSomething1();
     doSomething2();     doSomething2();
 -   return doSomething3() + 7; -   return doSomething3() + 7;
-+   int i = doSomething3() + 7++   return this.exampleModifyReturnValue(doSomething3() + 7); 
-+   CallbackInfoReturnable<Integer> cir = new CallbackInfoReturnable<Integer>("foo", true, i); +}
-+   injected(cir); +
-+   if (cir.isCancelled()) return cir.getReturnValue(); +
-+   return i; +
-  }+
 </code> </code>
  
tutorial/mixin_examples.txt · Last modified: 2026/04/08 11:56 by gauntrecluse