tutorial:mixin_examples
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:mixin_examples [2020/09/14 05:28] – minor improvement siglong | tutorial:mixin_examples [2024/01/13 15:02] (current) – arkosammy12 | ||
---|---|---|---|
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: | ||
+ | |||
+ | ===== Mixing into a private inner class ===== | ||
+ | Use the targets parameter and a '' | ||
+ | <code java> | ||
+ | @Mixin(targets = " | ||
+ | public class AmbientOcclusionCalculatorMixin { | ||
+ | // do your stuff here | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Access the this instance of the class your mixin is targeting ===== | ||
+ | Note: Double casting '' | ||
+ | |||
+ | Mixin: | ||
+ | <code java> | ||
+ | @Mixin(TargetClass.class) | ||
+ | public class MyMixin extends EveryThingThatTargetClassExtends implements EverythingThatTargetClassImplements { | ||
+ | @Inject(method = " | ||
+ | private void injected(CallbackInfo ci) { | ||
+ | TargetClass thisObject = (TargetClass)(Object)this; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Injecting into the head of a static block ===== | ||
+ | Mixin: | ||
+ | <code java> | ||
+ | @Inject(method = "< | ||
+ | private void injected(CallbackInfo ci) { | ||
+ | doSomething3(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Result: | ||
+ | <code diff> | ||
+ | static { | ||
+ | + | ||
+ | doSomething1(); | ||
+ | doSomething2(); | ||
+ | } | ||
+ | </ | ||
===== Injecting into the head of a method ===== | ===== Injecting into the head of a method ===== | ||
Line 64: | Line 107: | ||
</ | </ | ||
- | ===== Injecting into before method call ===== | + | ===== Injecting into the point before |
Mixin: | Mixin: | ||
<code java> | <code java> | ||
Line 84: | Line 127: | ||
</ | </ | ||
- | ===== Injecting into after method call ===== | + | ===== Injecting into the point after a method call ===== |
Mixin: | Mixin: | ||
<code java> | <code java> | ||
Line 101: | Line 144: | ||
+ | + | ||
doSomething2(); | doSomething2(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Injecting into the point with shift amount ===== | ||
+ | Mixin: | ||
+ | <code java> | ||
+ | @Inject(method = " | ||
+ | private void injected(CallbackInfo ci) { | ||
+ | doSomething3(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Result: | ||
+ | <code diff> | ||
+ | public void foo() { | ||
+ | doSomething1(); | ||
+ | Something something = new Something(); | ||
+ | something.doSomething(); | ||
+ | doSomething2(); | ||
+ | + | ||
} | } | ||
</ | </ | ||
Line 111: | Line 174: | ||
at = @At( | at = @At( | ||
value = " | value = " | ||
- | target = " | + | target = " |
- | slice = @Slice( | + | |
- | from = @At(value = " | + | slice = @Slice( |
- | to = @At(value = " | + | from = @At(value = " |
+ | to = @At(value = " | ||
+ | | ||
+ | ) | ||
private void injected(CallbackInfo ci) { | private void injected(CallbackInfo ci) { | ||
doSomething5(); | doSomething5(); | ||
Line 181: | Line 247: | ||
</ | </ | ||
+ | ===== Capturing local values ===== | ||
+ | ==== Capture locals without MixinExtras ==== | ||
+ | |||
+ | Mixin: | ||
+ | <code java> | ||
+ | @Inject(method = " | ||
+ | private void injected(CallbackInfo ci, TypeArg1 arg1) { | ||
+ | // | ||
+ | arg1.doSomething4(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Result: | ||
+ | <code diff> | ||
+ | public void foo() { | ||
+ | TypeArg1 arg1 = getArg1(); | ||
+ | arg1.doSomething1(); | ||
+ | arg1.doSomething2(); | ||
+ | TypeArg2 arg2 = getArg2(); | ||
+ | arg2.doSomething3(); | ||
+ | + | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Capture locals with MixinExtras ==== | ||
+ | :!: See the oficial MixinExtra' | ||
+ | |||
+ | :!: MixinExtras required Fabric Loader 0.15 or above, or you have to manually specify it in '' | ||
+ | |||
+ | :!: If there are multiple locals with that type, you have to specify '' | ||
+ | |||
+ | Mixin: | ||
+ | <code java> | ||
+ | @Inject(method = " | ||
+ | private void injected(CallbackInfo ci, @Local TypeArg2 arg2) { | ||
+ | arg1.doSomething4(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Result: | ||
+ | <code diff> | ||
+ | public void foo() { | ||
+ | TypeArg1 arg1 = getArg1(); | ||
+ | arg1.doSomething1(); | ||
+ | arg1.doSomething2(); | ||
+ | TypeArg2 arg2 = getArg2(); | ||
+ | arg2.doSomething3(); | ||
+ | + | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Capturing one of multiple locals of a type ==== | ||
+ | Mixin: | ||
+ | <code java> | ||
+ | @Inject(method = " | ||
+ | private void injected(CallbackInfo ci, @Local(ordinal = 2) TypeArg arg) { | ||
+ | arg1.doSomething4(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Result: | ||
+ | <code diff> | ||
+ | public void foo() { | ||
+ | TypeArg arg1 = getArg1(); | ||
+ | TypeArg arg2 = getArg2(); | ||
+ | TypeArg arg3 = getArg3(); | ||
+ | TypeArg arg4 = getArg4(); | ||
+ | doSomething(); | ||
+ | + | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Modifying locals ===== | ||
+ | This requires MixinExtras. | ||
+ | |||
+ | Mixin: | ||
+ | <code java> | ||
+ | @Inject(method = " | ||
+ | private static void injected(CallbackInfo ci, @Local LocalRef< | ||
+ | localRef.set(localRef.get() + " - modified" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Result: | ||
+ | <code diff> | ||
+ | public void foo() { | ||
+ | String s = " | ||
+ | doSomething(); | ||
+ | + s = s + " - modified"; | ||
+ | doSomething2(s); | ||
+ | } | ||
+ | </ | ||
===== Modifying a return value ===== | ===== Modifying a return value ===== | ||
Mixin: | Mixin: | ||
Line 356: | Line 514: | ||
+ | + | ||
double d2 = doSomething4(); | double d2 = doSomething4(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Modifying a constant ===== | ||
+ | Mixin: | ||
+ | <code java> | ||
+ | @ModifyConstant(method = " | ||
+ | private int injected(int value) { | ||
+ | return ++value; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Result: | ||
+ | <code diff> | ||
+ | public void foo() { | ||
+ | - for (int i = 0; i < 4; i++) { | ||
+ | + for (int i = 0; i < injected(4); | ||
+ | doSomething(i); | ||
+ | } | ||
} | } | ||
</ | </ |
tutorial/mixin_examples.1600061294.txt.gz · Last modified: 2020/09/14 05:28 by siglong