User Tools

Site Tools


tutorial:introduction

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
tutorial:introduction [2019/11/03 14:20] – [Mixins] layltutorial:introduction [2019/11/03 14:49] – [Mixins] layl
Line 55: Line 55:
 ===== Mixins ===== ===== Mixins =====
  
-Finally, if all other methods have failed, you can use mixins. +Finally, you can use mixins. 
-These are not to be taken lightly, they can make debugging lot harder and increase the risk of conflicts+Mixins are a powerful feature that let you change any of Minecraft's own code
-Butif you need to affect Minecraft's core functionality in a way no other previously mentioned method lets you, this is the way to do it.+Some mixins can cause conflictbut used responsibly these are key to adding unique behavior to your mod.
  
-Mixins allow you to add any code you want to existing Minecraft classes and methods. +Mixins come in a variety of flavors, in rough order of preference:
-The example project already comes with an example mixin that injects itself into the "MinecraftClient#init" method.+
  
-If you can, prefer adding interfaces to classes over injecting into existing methods. +  * Adding Interfaces 
-This will let you access the class in ways not previously allowed by casting an instance of it to your interface. +  * Callback Injectors 
-By preferring adding interfaces, you minimize the risk of conflicts by purely adding something to the class rather than changing it.+  * Redirect Injectors 
 +  * Overwrites, you should never use these
  
-If you do have to use method injections**strongly** prefer additions over "@Overwrite"-ing methods entirely+ 
-Overwrites are the most likely to conflict with other mods and in most situations you do not need them. +==== Adding Interfaces ==== 
-You can inject code into any part of a methodincluding wrapping inner method calls and canceling them.+ 
 +Probably the safest way to use mixins, new interface implementations can be added to any Minecraft class. 
 +You can then access the interface by casting the class to it. 
 +This doesn't change anything about the class, it only adds new things, and is therefore very unlikely to conflict. 
 + 
 + 
 +==== Callback Injectors ==== 
 + 
 +Callback injectors let you add callback hooks to existing methods, as well as on specific method calls within that method. 
 +They also let you intercept and change the return value of a method, and return early. 
 +Callback injects can stack, and are therefore unlikely to cause conflicts between mods. 
 + 
 + 
 +==== Redirect Injectors ==== 
 + 
 +Redirects let you wrap method calls or variable access within a target method with your own code. 
 +Use these very sparingly, a target call or access can only be redirected once between all mods. 
 +If two mods redirect the same value, that will cause a conflict. 
 +Consider callback injects first. 
 + 
 + 
 +==== Overwrite ==== 
 + 
 +Never use overwrites
 +Overwrites replace a method entirely. 
 +They are extremely likely to conflict not just with other mods, but also with changes to Minecraft itself. 
 +You most likely do not need an overwrite to do what you want to doplease use something else.
tutorial/introduction.txt · Last modified: 2023/09/13 21:20 by 2001:8a0:f4d2:c700:98c:bb27:6ad8:1dab