User Tools

Site Tools


tutorial:interface_injection

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:interface_injection [2025/10/20 00:29] – Make example mixin class package-private abstract gauntreclusetutorial:interface_injection [2025/11/10 03:38] (current) – Add info on how to inject generic interfaces earthcomputer
Line 1: Line 1:
 ======= Interface Injection ======= ======= Interface Injection =======
  
-This is a new technique introduced by Loom 0.11 to add methods into a specific existing class. +===== Overview ===== 
-More specifically, you can create an Interface, and then inject this interface into the class.+ 
 +Interface injection is a technique to add methods into a specific existing class. 
 +More specifically, you can create an interface, and then inject this interface into the class.
 As result the target class will acquire all the methods of the interface, as if it always had them. As result the target class will acquire all the methods of the interface, as if it always had them.
 Interface injection is a compile time only feature, this means that a Mixin should also be used to implement the interface into the target class. Interface injection is a compile time only feature, this means that a Mixin should also be used to implement the interface into the target class.
Line 10: Line 12:
 Fabric API takes advantage of this technique. For example, ''TagBuilder'' implements ''FabricTagBuilder'', ''BlockEntityType'' implements ''FabricBlockEntityType'', so that you can directly use the instance methods in the Fabric API on the vanilla objects. Fabric API takes advantage of this technique. For example, ''TagBuilder'' implements ''FabricTagBuilder'', ''BlockEntityType'' implements ''FabricBlockEntityType'', so that you can directly use the instance methods in the Fabric API on the vanilla objects.
  
-Let's explain better with an example: +===== Example Goal =====
 The scope of this example is to add the following method into ''<yarn net.minecraft.class_3609>'' to get the sound of the bucket when emptied. The scope of this example is to add the following method into ''<yarn net.minecraft.class_3609>'' to get the sound of the bucket when emptied.
 This, normally, is not possible because ''<yarn net.minecraft.class_3609>'' does not have a similar method. This, normally, is not possible because ''<yarn net.minecraft.class_3609>'' does not have a similar method.
Line 18: Line 19:
 Optional<class_3414> getBucketEmptySound$myMod() Optional<class_3414> getBucketEmptySound$myMod()
 </yarncode> </yarncode>
 +
 +===== Step 1: Create the Interface =====
  
 To add the method into the class, first of all you need to create an interface with it: To add the method into the class, first of all you need to create an interface with it:
Line 34: Line 37:
  
 ℹ️ It's highly recommended to add a dollar-character or underscore character with the mod name as the prefix or suffix of the method name, in order to avoid method name conflict with other mods. ℹ️ It's highly recommended to add a dollar-character or underscore character with the mod name as the prefix or suffix of the method name, in order to avoid method name conflict with other mods.
 +
 +===== Step 2: Implement the Interface with a Mixin =====
  
 Now you need to implement this interface into ''<yarn net.minecraft.class_3609>'' with a mixin implementing the interface: Now you need to implement this interface into ''<yarn net.minecraft.class_3609>'' with a mixin implementing the interface:
Line 47: Line 52:
 } }
 </yarncode> </yarncode>
 +
 +===== Step 3: Inject the Interface in ''fabric.mod.json'' =====
  
 Lastly you need to inject the interface into ''<yarn net.minecraft.class_3609>''. Lastly you need to inject the interface into ''<yarn net.minecraft.class_3609>''.
Line 61: Line 68:
 } }
 </code> </code>
 +
 +:!: Sometimes, your interface injections may need to include the ''$'' symbol, but the Groovy template processor may interpret this as a template variable if you are replacing variables (such as ''${version}'') in your ''fabric.mod.json''. A workaround for this is to use the Unicode escape for ''$'', which is ''\u0024''.
 +
 +==== Generic interfaces ====
 +
 +If your interface has generics, you can specify them when you add the injected interface. For this, you need to add ''<>'' angled brackets and write the generics in Java bytecode signature format between them.
 +
 +^ Description                                   ^ Java example                 ^ Syntax                                                                                                                                                                         ^ Signature format example       ^
 +| Class type                                    | ''java.lang.String''         | ''L'' + internal name + '';''                                                                                                                                                  | ''Ljava/lang/String;''         |
 +| Array type                                    | ''java.lang.String[]''       | ''['' + element type                                                                                                                                                           | ''[Ljava/lang/String;''        |
 +| Primitive type (may appear as array elements) | ''double''                   | A single capital letter representing the type. Mostly logical, such as ''int'' = ''I'', ''double'' = ''D'', with the 2 exceptions of ''boolean'' = ''Z'' and ''long'' = ''J''. | ''D''                          |
 +| Type variable                                 | ''T''                        | ''T'' + name + '';''                                                                                                                                                           | ''TT;''                        |
 +| Generic class type                            | ''java.util.List<T>''        | ''L'' + internal name + ''<'' + generics + ''>;''                                                                                                                              | ''Ljava/util/List<TT;>;''      |
 +| Wildcard                                      | ''?'', ''java.util.List<?>'' | The ''*'' character                                                                                                                                                            | ''*'', ''Ljava/util/List<*>;'' |
 +| Extends wildcard bound                        | ''? extends String''         | ''+'' + the bound                                                                                                                                                              | ''+Ljava/lang/String;''        |
 +| Super wildcard bound                          | ''? super String''           | ''-'' + the bound                                                                                                                                                              | ''-Ljava/lang/String;''        |
 +
 +Here is a full example using generics:
 +<code json fabric.mod.json [enable_line_numbers="false"]>
 +{
 + "custom": {
 + "loom:injected_interfaces": {
 + "net/minecraft/class_3609": ["net/fabricmc/example/MyGenericInterface<+TT;TU;>"]
 + }
 + }
 +}
 +</code>
 +which would generate the implementation:
 +<yarncode java [enable_line_numbers="false"]>
 +public class class_3609 implements MyGenericInterface<? extends T, U> {
 +   // ...
 +}
 +</yarncode>
 +
 +===== Step 4: Using the Injected Method =====
  
 Now you can use the new method: Now you can use the new method:
Line 67: Line 109:
 Optional<class_3414> sound = mytestfluid.getBucketEmptySound$myMod(); Optional<class_3414> sound = mytestfluid.getBucketEmptySound$myMod();
 </yarncode> </yarncode>
 +
  
 You could also override this method in classes extending ''<yarn class_3609>'' to implement custom behaviours. You could also override this method in classes extending ''<yarn class_3609>'' to implement custom behaviours.
  
-Sometimes, your interface injections may need to include the ''$'' symbol, but the Groovy template processor may interpret this as a template variable if you are replacing variables (such as ''${version}'') in your ''fabric.mod.json''. A workaround for this is to use the Unicode escape for ''$'', which is ''\u0024''.+
tutorial/interface_injection.1760920195.txt.gz · Last modified: 2025/10/20 00:29 by gauntrecluse