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 [2023/10/12 06:17] – Add note to double casting example daomephsta | tutorial:mixin_examples [2024/11/18 15:52] (current) – fixed some of the argument names in mixinextra section bemoty | ||
---|---|---|---|
Line 14: | Line 14: | ||
===== 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 '' | + | Note: Double casting '' |
- | 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 22: | ||
@Inject(method = " | @Inject(method = " | ||
private void injected(CallbackInfo ci) { | private void injected(CallbackInfo ci) { | ||
- | | + | |
} | } | ||
} | } | ||
Line 149: | Line 147: | ||
</ | </ | ||
- | ===== 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 248: | ||
===== Capturing local values ===== | ===== Capturing local values ===== | ||
+ | ==== Capture locals without MixinExtras ==== | ||
Mixin: | Mixin: | ||
Line 272: | Line 271: | ||
</ | </ | ||
+ | ==== 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) { | ||
+ | arg2.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) { | ||
+ | arg.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: |
tutorial/mixin_examples.1697091431.txt.gz · Last modified: 2023/10/12 06:17 by daomephsta