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
Last revisionBoth sides next revision
tutorial:mixin_examples [2023/12/18 02:09] – [Access the this instance of the class your mixin is targeting] solidblocktutorial:mixin_examples [2024/01/12 23:26] arkosammy12
Line 147: Line 147:
 </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 248: Line 248:
  
 ===== Capturing local values ===== ===== Capturing local values =====
 +==== Capture locals without MixinExtras ====
  
 Mixin: Mixin:
Line 270: Line 271:
 </code> </code>
  
 +==== Capture locals with MixinExtras ====
 +:!: 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.
 +
 +:!: More information about MixinExtra's ''@Local'' annotation can be found in its [[https://github.com/LlamaLad7/MixinExtras/wiki/Local|Wiki]].
 +
 +Mixin:
 +<code java>
 +@Inject(method = "foo()V", at = @At(value = "TAIL"))
 +private void injected(CallbackInfo ci, @Local TypeArg2 arg2) {
 +  arg1.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) {
 +  arg1.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.txt · Last modified: 2024/01/13 15:02 by arkosammy12