tutorial:interface_injection
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorial:interface_injection [2025/10/09 14:18] – solidblock | tutorial: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 | + | ===== Overview ===== |
| - | More specifically, | + | |
| + | Interface injection | ||
| + | More specifically, | ||
| 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, '' | Fabric API takes advantage of this technique. For example, '' | ||
| - | Let's explain better with an example: | + | ===== Example Goal ===== |
| The scope of this example is to add the following method into ''< | The scope of this example is to add the following method into ''< | ||
| This, normally, is not possible because ''< | This, normally, is not possible because ''< | ||
| Line 18: | Line 19: | ||
| Optional< | Optional< | ||
| </ | </ | ||
| + | |||
| + | ===== 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 ''< | Now you need to implement this interface into ''< | ||
| Line 39: | Line 44: | ||
| < | < | ||
| @Mixin(class_3609.class) | @Mixin(class_3609.class) | ||
| - | public | + | abstract |
| @Override | @Override | ||
| public Optional< | public Optional< | ||
| Line 47: | Line 52: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | ===== Step 3: Inject the Interface in '' | ||
| Lastly you need to inject the interface into ''< | Lastly you need to inject the interface into ''< | ||
| Line 61: | Line 68: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | :!: Sometimes, your interface injections may need to include the '' | ||
| + | |||
| + | ==== Generic interfaces ==== | ||
| + | |||
| + | If your interface has generics, you can specify them when you add the injected interface. For this, you need to add ''<>'' | ||
| + | |||
| + | ^ Description | ||
| + | | Class type | '' | ||
| + | | Array type | '' | ||
| + | | Primitive type (may appear as array elements) | '' | ||
| + | | Type variable | ||
| + | | Generic class type | '' | ||
| + | | Wildcard | ||
| + | | Extends wildcard bound | ''? | ||
| + | | Super wildcard bound | ''? | ||
| + | |||
| + | Here is a full example using generics: | ||
| + | <code json fabric.mod.json [enable_line_numbers=" | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | which would generate the implementation: | ||
| + | < | ||
| + | public class class_3609 implements MyGenericInterface<? | ||
| + | // ... | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== 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< | Optional< | ||
| </ | </ | ||
| + | |||
| You could also override this method in classes extending ''< | You could also override this method in classes extending ''< | ||
| - | Sometimes, your interface injections may need to include the '' | + | |
tutorial/interface_injection.1760019525.txt.gz · Last modified: 2025/10/09 14:18 by solidblock