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
Next revision
Previous revision
tutorial:mixin_examples [2025/09/24 11:36] – Add FIXME warning at head of page on the currently unreliable state of the page for learning Mixin gauntreclusetutorial:mixin_examples [2026/05/16 08:25] (current) – Clarify that using shift by is discouraged. earthcomputer
Line 1: Line 1:
 FIXME //The Fabric Wiki's Mixin segments is under heavy reviews, pages on the topic are subject to major edits or rewrites. This page in particular is noted as being unreliable to learn Mixin as a tool from. Learning by example should be used sparingly, as it may lead to a lack of understanding. Prioritize, if you are unable to learn through documentation, presenting your intention in detail in a Mixin support channel on the Fabric or SpongePowered Discord servers and discussing with experienced devs there.// FIXME //The Fabric Wiki's Mixin segments is under heavy reviews, pages on the topic are subject to major edits or rewrites. This page in particular is noted as being unreliable to learn Mixin as a tool from. Learning by example should be used sparingly, as it may lead to a lack of understanding. Prioritize, if you are unable to learn through documentation, presenting your intention in detail in a Mixin support channel on the Fabric or SpongePowered Discord servers and discussing with experienced devs there.//
 +
 +
 +:!: //A rewrite of this page is planned and will be drafted in the foreseeable future, whilst some quicker changes and improvements may be applied to this page in the process, more fundamental structural changes will happen as part of a broader rewrite and replacement by a new version of the page//
  
 ====== Mixin Examples ====== ====== Mixin Examples ======
Line 150: Line 153:
  
 ===== Injecting into the point with shift amount ===== ===== Injecting into the point with shift amount =====
 +:!: Using a shift amount is heavily discouraged, instead you should find a more descriptive/precise injection point.
 +
 Mixin: Mixin:
 <code java> <code java>
Line 346: Line 351:
 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 354: Line 359:
 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.1758713807.txt.gz · Last modified: 2025/09/24 11:36 by gauntrecluse