Skip to content

Instantly share code, notes, and snippets.

@TelepathicGrunt
Last active June 10, 2021 14:05
Show Gist options
  • Save TelepathicGrunt/59f5ae53cf2b336ddfa0a37032e5e5a3 to your computer and use it in GitHub Desktop.
Save TelepathicGrunt/59f5ae53cf2b336ddfa0a37032e5e5a3 to your computer and use it in GitHub Desktop.
How-to-get outer class instance from inner class with mixin

When you mixin into a non-static inner class, the inner class has a hidden field that stores the instance of the outer class. It is a bit tricky to get it but it can be done.

Forge:

Step one, go to the Forge jar and find the class in question. Open it in Intellij

https://i.imgur.com/Dnrv3Kv.png

https://i.imgur.com/F1bPHRG.png

Next, locate the srg name of the field that holds the outer class. Here, for Wandering Goal in bee Entity, the field is field_226508_a_

https://i.imgur.com/Yy3Ynfa.png

Now use Intellij's bytecode viewer on the inner class in your development environment and find the in-dev name of the field that holds the outer class instance. Here, it is called this$0.

https://i.imgur.com/pmBoDyZ.png

Now make your mixin shadow that field using the aliases to the srg name. The name of the field should be the bytecode viewer in-dev name.

    @Final
    @Shadow(aliases = "field_226508_a_")
    private BeeEntity this$0;

This should now work to grab the field in both dev and prod environments and you can use it for whatever you need it for.

 

Fabric:

Step one, use Intellij's bytecode viewer on the inner class and find the intermediary name of the field that holds the outer class instance. Here, for Bee Wander Around Goal in bee Entity, the field is field_20380

https://i.imgur.com/lvjjOMU.png

Now make your mixin shadow that field using the aliases to the intermediary name. The name of the field should be called the same name. You need the aliases to get the compilier to stop complaining about the field not existing when it does.

    @Final
    @Shadow(aliases = "field_20380")
    private BeeEntity field_20380;

 

 


 

How to mixin gist:

https://gist.github.com/TelepathicGrunt/3784f8a8b317bac11039474012de5fb4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment