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/11/10 02:07] – Add headings to interface injection article earthcomputer | tutorial:interface_injection [2025/12/27 12:27] (current) – gauntrecluse | ||
|---|---|---|---|
| Line 6: | Line 6: | ||
| More specifically, | 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. | ||
| Line 17: | Line 16: | ||
| < | < | ||
| - | Optional< | + | Optional< |
| </ | </ | ||
| Line 28: | Line 27: | ||
| public interface BucketEmptySoundGetter { | public interface BucketEmptySoundGetter { | ||
| - | default Optional< | + | default Optional< |
| return Optional.empty(); | return Optional.empty(); | ||
| } | } | ||
| Line 34: | Line 33: | ||
| </ | </ | ||
| - | :!: The method body in the interface | + | :!: Even if the method body in the interface |
| - | ℹ️ | + | ℹ️ |
| ===== Step 2: Implement the Interface with a Mixin ===== | ===== Step 2: Implement the Interface with a Mixin ===== | ||
| - | Now you need to implement this interface | + | 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, |
| < | < | ||
| Line 46: | Line 45: | ||
| abstract class MixinFlowableFluid implements BucketEmptySoundGetter { | abstract class MixinFlowableFluid implements BucketEmptySoundGetter { | ||
| @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 53: | Line 52: | ||
| </ | </ | ||
| - | ===== Step 3: Inject the Interface in '' | + | |
| + | ===== 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 '' | + | 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/ | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| </ | </ | ||
| - | :!: Sometimes, | + | :!: To make your interface |
| + | |||
| + | ==== 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 ===== | ===== Step 4: Using the Injected Method ===== | ||
| Line 76: | Line 100: | ||
| < | < | ||
| - | Optional< | + | Optional< |
| </ | </ | ||
tutorial/interface_injection.1762740421.txt.gz · Last modified: 2025/11/10 02:07 by earthcomputer