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 [2023/10/12 06:17] – Add note to double casting example daomephstatutorial:mixin_examples [2025/10/12 09:21] (current) – Mark page as having a planned rewrite gauntrecluse
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.//
 +
 +
 +:!: //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 ======
 This is a collection of frequently used mixins. This is a collection of frequently used mixins.
Line 14: Line 19:
  
 ===== Access the this instance of the class your mixin is targeting ===== ===== Access the this instance of the class your mixin is targeting =====
-Note: Double casting ''this'' should be avoided when possible. +Note: Double casting ''this'' should be avoided when possible. If you intend to use a method or field from the target class, use ''@Shadow''If the method or field is from a parent of the target class, have your mixin extend the direct parent of the target class.
-If the method or field is from the target class, use `@Shadow+
-If the method or field is from a parent of the target class, have your mixin extend the direct parent of the target class.+
  
 Mixin: Mixin:
Line 24: Line 27:
   @Inject(method = "foo()V", at = @At("HEAD"))   @Inject(method = "foo()V", at = @At("HEAD"))
   private void injected(CallbackInfo ci) {   private void injected(CallbackInfo ci) {
-    ((TargetClass)(Object)this).methodOfTheTargetClass();+    TargetClass thisObject = (TargetClass)(Object)this;
   }   }
 } }
Line 149: Line 152:
 </code> </code>
  
-===== Injecting into the point before a method call with shift amount =====+===== Injecting into the point with shift amount =====
 Mixin: Mixin:
 <code java> <code java>
Line 250: Line 253:
  
 ===== Capturing local values ===== ===== Capturing local values =====
 +==== Capture locals without MixinExtras ====
  
 Mixin: Mixin:
Line 272: Line 276:
 </code> </code>
  
 +==== Capture locals with MixinExtras ====
 +:!: See the oficial MixinExtra's [[https://github.com/LlamaLad7/MixinExtras/wiki/Local|Wiki]].
 +
 +:!: MixinExtras required Fabric Loader 0.15 or above, or you have to manually specify it in ''build.gradle''.
 +
 +:!: If there are multiple locals with that type, you have to specify ''ordinal'' or it will throw an error.
 +
 +:!: the use of ''@Local'' is recommended over ''LocalCapture''
 +
 +Mixin:
 +<code java>
 +@Inject(method = "foo()V", at = @At(value = "TAIL"))
 +private void injected(CallbackInfo ci, @Local TypeArg2 arg2) {
 +  arg2.doSomething4();
 +}
 +</code>
 +
 +Result:
 +<code diff>
 +  public void foo() {
 +    TypeArg1 arg1 = getArg1();
 +    arg1.doSomething1();
 +    arg1.doSomething2();
 +    TypeArg2 arg2 = getArg2();
 +    arg2.doSomething3();
 ++   injected(new CallbackInfo("foo", false), arg2);
 +  }
 +</code>
 +
 +==== Capturing one of multiple locals of a type ====
 +Mixin:
 +<code java>
 +@Inject(method = "foo()V", at = @At(value = "TAIL"))
 +private void injected(CallbackInfo ci, @Local(ordinal = 2) TypeArg arg) {
 +  arg.doSomething4();
 +}
 +</code>
 +
 +Result:
 +<code diff>
 +  public void foo() {
 +    TypeArg arg1 = getArg1();
 +    TypeArg arg2 = getArg2();
 +    TypeArg arg3 = getArg3();
 +    TypeArg arg4 = getArg4();
 +    doSomething();
 ++   injected(new CallbackInfo("foo", false), arg3);
 +  }
 +</code>
 +
 +===== Modifying locals =====
 +This requires MixinExtras.
 +
 +Mixin:
 +<code java>
 +  @Inject(method = "foo()V", at = @At(value = "INVOKE", target = "doSomething()V", shift = At.Shift.AFTER))
 +  private static void injected(CallbackInfo ci, @Local LocalRef<String> localRef) {
 +    localRef.set(localRef.get() + " - modified")
 +  }
 +</code>
 +
 +Result:
 +<code diff>
 +  public void foo() {
 +    String s = "example string";
 +    doSomething();
 ++   s = s + " - modified";
 +    doSomething2(s);
 +  }
 +</code>
 ===== Modifying a return value ===== ===== Modifying a return value =====
 Mixin: Mixin:
tutorial/mixin_examples.1697091431.txt.gz · Last modified: 2023/10/12 06:17 by daomephsta