tutorial:interface_injection
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
tutorial:interface_injection [2022/03/08 21:06] – moved from the mixin inject page juuz | tutorial:interface_injection [2025/10/09 14:18] (current) – solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ======= | + | ======= |
This is a new technique introduced by Loom 0.11 to add methods into a specific existing class. | This is a new technique introduced by Loom 0.11 to add methods into a specific existing class. | ||
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: | ||
- | The scope of this example is to add the following method into FlowableFluid | + | The scope of this example is to add the following method into ''< |
- | This, normally, is not possible because | + | This, normally, is not possible because |
- | <code java [enable_line_numbers=" | + | <yarncode |
- | Optional< | + | Optional< |
- | </code> | + | </yarncode> |
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: | ||
- | <code java [enable_line_numbers=" | + | <yarncode |
package net.fabricmc.example; | package net.fabricmc.example; | ||
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(); | ||
} | } | ||
} | } | ||
- | </code> | + | </yarncode> |
- | Now you need to implement this interface | + | :!: The method body in the interface |
- | <code java [enable_line_numbers=" | + | ℹ️ 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. |
- | @Mixin(FlowableFluid.class) | + | |
+ | Now you need to implement this interface into ''< | ||
+ | |||
+ | <yarncode | ||
+ | @Mixin(class_3609.class) | ||
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. | + | |
- | return Optional.of(((FlowableFluid) (Object) this).isIn(FluidTags.LAVA) ? SoundEvents.ITEM_BUCKET_EMPTY_LAVA | + | |
} | } | ||
} | } | ||
- | </code> | + | </yarncode> |
- | Lastly you need to inject the interface into FlowableFluid. | + | 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 net/minecraft/ | + | 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 60: | Line 64: | ||
Now you can use the new method: | Now you can use the new method: | ||
- | <code java [enable_line_numbers=" | + | <yarncode |
- | Optional< | + | Optional< |
- | </code> | + | </yarncode> |
+ | |||
+ | You could also override this method in classes extending ''< | ||
- | You could also override | + | Sometimes, your interface injections may need to include the '' |
tutorial/interface_injection.1646773596.txt.gz · Last modified: 2022/03/08 21:06 by juuz