User Tools

Site Tools


Sidebar

← Go back to the homepage

Fabric Tutorials

Setup

These pages will help you setup a productive development environment and semi-automated releases.

Basics

These pages are essential must-reads when modding with Fabric, and modding Minecraft in general, if you are new to modding, it is recommended you read the following.

Items

Blocks and Block Entities

Data Generation

World Generation

Commands

These pages will guide you through Mojang's Brigadier library which allows you to create commands with complex arguments and actions.

Events

These pages will guide you through using the many events included in Fabric API, and how to create your own events for you or other mods to use.

Entities

Fluids

Mixins & ASM

These pages will guide you through the usage of SpongePowered's Mixin library, which is a highly complex topic. We recommend you read these pages thoroughly.

Miscellaneous

Yarn

Contribute to Fabric

tutorial:mixin_tips

FIXME This page is currently being written, and may change at any time.

Mixin Tips (WIP)

This is a collection of different tips some might find useful. It's recommended to read the previous articles to understand the tips.

Why make a class abstract?

1. Prevent instantiation

It's fair to say that you should never instantiate a mixin class, mainly because the class doesn't make sense to java if it isn't used in a mixin environment, and there are other ways to access methods declared in a mixin.

Declaring a mixin class abstract doesn't affect it's function, and it becomes protected against accidental instantiation.

MixinClass foo = new MixinClass(); // can't do that with an abstract class

2. Make more elegant shadow methods

If you want to access a unaccessible method or field from your target class into your mixin class, you need to use @Shadow to make that method/field visible.

You can perfectly do this in normal classes by using a dummy body:

@Shadow
protected void hiddenMethod() {/*dummy body*/}

But it's much more elegant to use an abstract method (and hence an abstract class):

@Shadow
protected abstract void hiddenMethod(); // no dummy body necessary

Note: this doesn't work with private methods, since you can't have private abstract methods, and hence you need to use the normal one.

3. Access the this instance more easily

In mixin, if you want to access the “this” instance, you have to do a cast inside your mixin class:

((TargetClass)(Object)this).field/method();

But that only works if your mixin class extends/implements everything that your target class does, which can be a pain because if one of those classes/interfaces has a method you need to implement, you'll be in for some trouble.

Luckily, this can all be avoided by using an abstract class, in which case you don't have to implement methods and all problems are avoided.

How to mixin inner classes

1. Normal inaccessible inner classes

Since this you can't directly access (and hence mention) these classes from outside, you need to use the “targets” field of the mixin annotation to specify the name.

You do this by using the complete name of the outer class, then a $ and finally the name of the inner class, as shown below:

Class:

package some.random.package;
 
public class Outer {
     private class Inner {
         public void someRandomMethod() {}
     }
}

Mixin with injection:

@Mixin(targets = "some.random.package.Outer$Inner")
public class MyMixin {
    @Inject(method = "someRandomMethod()V", at = @At("HEAD")
    private void injected(CallbackInfo ci) {
        // your code here
    }
}

The only caveat is that if you want to mixin into the inner class constructor, the first parameter must be of the type of the outer class (this is implicitly added by the compiler to allow the inner class to access instance methods of the outer class):

@Inject(method = "<init>(Lsome/random/package/Outer;)V", at = @At("HEAD")
private void injected(CallbackInfo ci) {
    // your code here
}

2. Static inaccessible inner classes

These are the same as above, the only difference is that the constructor doesn't have the outer class first parameter (because in static inner classes only private static methods can be accessed from the inner class, and hence that parameter isn't needed).

3. Anonymous inner classes

These are the same as the static inaccessible inner classes, the only difference is that since they don't have a name, they are declared by appearance order, for example: the anonymous inner class if declared in our previous example class first would be named Outer$1, a second one would be named Outer$2, a third one Outer$3 and so on (the declaration order is on source level).

UNDER CONSTRUCTION

tutorial/mixin_tips.txt · Last modified: 2022/08/17 23:33 by clomclem