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/28 00:09] – Emphasize that implementing an injected interface with mixin is no longer necessary earthcomputer | tutorial:interface_injection [2025/12/27 12:27] (current) – gauntrecluse | ||
|---|---|---|---|
| Line 16: | Line 16: | ||
| < | < | ||
| - | Optional< | + | Optional< |
| </ | </ | ||
| Line 27: | Line 27: | ||
| public interface BucketEmptySoundGetter { | public interface BucketEmptySoundGetter { | ||
| - | default Optional< | + | default Optional< |
| return Optional.empty(); | return Optional.empty(); | ||
| } | } | ||
| Line 33: | Line 33: | ||
| </ | </ | ||
| - | :!: 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 '' | + | :!: 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 '' |
| + | |||
| + | ℹ️ 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 2: Implement the Interface with a Mixin ===== | ===== Step 2: Implement the Interface with a Mixin ===== | ||
| Line 43: | 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 50: | Line 52: | ||
| </ | </ | ||
| - | ℹ️ 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 ===== | ===== Step 3: Inject the Interface in the class tweaker ===== | ||
| Line 65: | Line 66: | ||
| </ | </ | ||
| - | ℹ️ | + | :!: To make your interface injection visible to other mods depending on your mod (if you are writing a library mod), use '' |
| ==== Generic interfaces ==== | ==== Generic interfaces ==== | ||
| Line 99: | Line 100: | ||
| < | < | ||
| - | Optional< | + | Optional< |
| </ | </ | ||
tutorial/interface_injection.1764288585.txt.gz · Last modified: 2025/11/28 00:09 by earthcomputer