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 [2022/04/13 07:20] – external edit 127.0.0.1 | tutorial:interface_injection [2025/11/28 00:09] (current) – Emphasize that implementing an injected interface with mixin is no longer necessary 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. | ||
| This is particularly useful for libraries, with this you can add new methods to existing classes and use them without the need of casting or reimplementing the interface every time. | This is particularly useful for libraries, with this you can add new methods to existing classes and use them without the need of casting or reimplementing the interface every time. | ||
| - | Let's explain better with an example: | + | Fabric API takes advantage of this technique. For 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 ''< |
| < | < | ||
| - | 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 23: | Line 27: | ||
| public interface BucketEmptySoundGetter { | public interface BucketEmptySoundGetter { | ||
| - | // The methods in an injected interface MUST be default, | + | default Optional< |
| - | // otherwise code referencing them won't compile! | + | |
| - | default Optional< | + | |
| return Optional.empty(); | return Optional.empty(); | ||
| } | } | ||
| Line 31: | Line 33: | ||
| </ | </ | ||
| - | Now you need to implement this interface | + | :!: Even if the method body in the interface isn't supposed to be used because it will be overridden by the mixin class, you must still generally specify the method body, which means the method must be '' |
| + | |||
| + | ===== Step 2: Implement the Interface with a Mixin ===== | ||
| + | |||
| + | The class tweaker step below on its own will be enough if you only want to implement the interface (for example, for marker interfaces, or where you have the whole implementation in default methods). However, | ||
| < | < | ||
| @Mixin(class_3609.class) | @Mixin(class_3609.class) | ||
| - | public | + | abstract |
| @Override | @Override | ||
| - | public Optional< | + | public Optional< |
| //This is how to get the default sound, copied from BucketItem class. | //This is how to get the default sound, copied from BucketItem class. | ||
| return Optional.of(((class_3609) (Object) this).method_15791(class_3486.field_15518) ? class_3417.field_15010 : class_3417.field_14834); | return Optional.of(((class_3609) (Object) this).method_15791(class_3486.field_15518) ? class_3417.field_15010 : class_3417.field_14834); | ||
| Line 43: | Line 49: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | ℹ️ If implementing the method with an interface, 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 3: Inject the Interface in the class tweaker ===== | ||
| Lastly you need to inject the interface into ''< | Lastly you need to inject the interface into ''< | ||
| - | The following snippet can be added to your fabric.mod.json | + | The following snippet can be added to your class tweaker |
| + | Follow the [[tutorial: | ||
| Note that all class names here must use the " | Note that all class names here must use the " | ||
| - | < | + | < |
| - | { | + | classTweaker v1 named |
| - | " | + | |
| - | " | + | inject-interface |
| - | "net/ | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| </ | </ | ||
| + | |||
| + | ℹ️ To make your interface injection visible to other mods depending on your mod (if you are writing a library mod), use '' | ||
| + | |||
| + | ==== 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 - mymod.classtweaker [enable_line_numbers=" | ||
| + | classTweaker v1 named | ||
| + | |||
| + | inject-interface net/ | ||
| + | </ | ||
| + | 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: | ||
| < | < | ||
| - | Optional< | + | Optional< |
| </ | </ | ||
| - | You could also override this method in classes extending <yarn class_3609> | + | |
| + | You could also override this method in classes extending | ||
| + | |||
tutorial/interface_injection.1649834421.txt.gz · Last modified: 2022/04/13 07:20 by 127.0.0.1