tutorial:interface_injection
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
tutorial:interface_injection [2025/02/12 18:42] – Add node on unicode escape for $ earthcomputer | tutorial:interface_injection [2025/10/09 14:18] (current) – solidblock | ||
---|---|---|---|
Line 7: | Line 7: | ||
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. | ||
+ | |||
+ | Fabric API takes advantage of this technique. For example, '' | ||
Let's explain better with an example: | Let's explain better with an example: | ||
Line 14: | Line 16: | ||
< | < | ||
- | Optional< | + | Optional< |
</ | </ | ||
Line 23: | Line 25: | ||
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(); | ||
} | } | ||
} | } | ||
</ | </ | ||
+ | |||
+ | :!: The method body in the interface may not be used because it will be overridden by the mixin class. However, you must specify the method body, which means the method must be '' | ||
+ | |||
+ | ℹ️ 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. | ||
Now you need to implement this interface into ''< | Now you need to implement this interface into ''< | ||
Line 37: | Line 41: | ||
public class MixinFlowableFluid implements BucketEmptySoundGetter { | public 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 45: | Line 49: | ||
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 file to add one or more interfaces to the ''< | + | The following snippet can be added to your '' |
Note that all class names here must use the " | Note that all class names here must use the " | ||
- | <code json [enable_line_numbers=" | + | < |
{ | { | ||
" | " | ||
Line 61: | Line 65: | ||
< | < | ||
- | Optional< | + | Optional< |
</ | </ | ||
- | You could also override this method in classes extending <yarn class_3609> | + | You could also override this method in classes extending |
Sometimes, your interface injections may need to include the '' | Sometimes, your interface injections may need to include the '' |
tutorial/interface_injection.txt · Last modified: 2025/10/09 14:18 by solidblock