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
Next revisionBoth sides next revision
tutorial:mixin_examples [2021/08/31 20:15] – [Access `this` instance of the class you're mixing into within a mixin class] lvanderzandetutorial:mixin_examples [2022/09/30 17:20] – [Capturing local values] salvopelux
Line 1: Line 1:
 ====== Mixin Examples ====== ====== Mixin Examples ======
 This is a collection of frequently used mixins. This is a collection of frequently used mixins.
 +This page is intended as a cheat sheet.
 +See [[tutorial:mixin_introduction|Mixin Introduction]] if you haven't already.
  
-===== Access `thisinstance of the class you're mixin is targeting =====+===== Mixing into a private inner class ===== 
 +Use the targets parameter and a ''$'' sign to get the inner class. 
 +<code java> 
 +@Mixin(targets = "net.minecraft.client.render.block.BlockModelRenderer$AmbientOcclusionCalculator"
 +public class AmbientOcclusionCalculatorMixin { 
 +    // do your stuff here 
 +
 +</code> 
 + 
 +===== Access the this instance of the class your mixin is targeting =====
 Mixin: Mixin:
 <code java> <code java>
-@Mixin(ClassMyMixinIsTargeting.class) +@Mixin(TargetClass.class) 
-public class MyMixin {+public class MyMixin extends EveryThingThatTargetClassExtends implements EverythingThatTargetClassImplements {
   @Inject(method = "foo()V", at = @At("HEAD"))   @Inject(method = "foo()V", at = @At("HEAD"))
   private void injected(CallbackInfo ci) {   private void injected(CallbackInfo ci) {
-    ((ClassMyMixinIsTargeting)(Object)this).functionOfTheTargetClass();+    ((TargetClass)(Object)this).methodOfTheTargetClass();
   }   }
 } }
 </code> </code>
  
 +===== Injecting into the head of a static block =====
 +Mixin:
 +<code java>
 +@Inject(method = "<clinit>", at = @At("HEAD"))
 +private void injected(CallbackInfo ci) {
 +    doSomething3();
 +}
 +</code>
 +
 +Result:
 +<code diff>
 +static {
 ++   injected(new CallbackInfo(“<clinit>”, false));
 +    doSomething1();
 +    doSomething2();
 +}
 +</code>
  
 ===== Injecting into the head of a method ===== ===== Injecting into the head of a method =====
Line 114: Line 142:
 +   injected(new CallbackInfo("foo", false)); +   injected(new CallbackInfo("foo", false));
     doSomething2();     doSomething2();
 +  }
 +</code>
 +
 +===== Injecting into the point before a method call with shift amount =====
 +Mixin:
 +<code java>
 +@Inject(method = "foo()V", at = @At(value = "INVOKE", target = "La/b/c/Something;doSomething()V", shift = At.Shift.BY, by = 2))
 +private void injected(CallbackInfo ci) {
 +  doSomething3();
 +}
 +</code>
 +
 +Result:
 +<code diff>
 +  public void foo() {
 +    doSomething1();
 +    Something something = new Something();
 +    something.doSomething();
 +    doSomething2();
 ++   injected(new CallbackInfo("foo", false));
   }   }
 </code> </code>
Line 194: Line 242:
     doSomething3();     doSomething3();
     return 10;     return 10;
 +  }
 +</code>
 +
 +===== Capturing local values =====
 +
 +Mixin:
 +<code java>
 +@Inject(method = "foo()V", at = @At(value = "TAIL"), locals = LocalCapture.CAPTURE_FAILHARD)
 +private void injected(CallbackInfo ci, TypeArg1 arg1) {
 +  //CAPTURE_FAILHARD: If the calculated locals are different from the expected values, throws an error.
 +  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), arg1);
   }   }
 </code> </code>
tutorial/mixin_examples.txt · Last modified: 2024/01/13 15:02 by arkosammy12