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/27 23:17] – Rewrite to use class tweakers 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: | ||
| </ | </ | ||
| - | :!: 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 45: | 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 51: | Line 51: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| ===== Step 3: Inject the Interface in the class tweaker ===== | ===== Step 3: Inject the Interface in the class tweaker ===== | ||
| Line 59: | Line 60: | ||
| Note that all class names here must use the " | Note that all class names here must use the " | ||
| - | <yarncode none mymod.classtweaker [enable_line_numbers=" | + | <code - mymod.classtweaker [enable_line_numbers=" |
| classTweaker v1 named | classTweaker v1 named | ||
| - | inject-interface net.minecraft.class_3609 | + | inject-interface net/minecraft/ |
| - | </yarncode> | + | </code> |
| - | To make your interface injection visible to other mods depending on your mod (if you are writing a library mod), use '' | + | :!: 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 82: | Line 83: | ||
| Here is a full example using generics: | Here is a full example using generics: | ||
| - | <yarncode none mymod.classtweaker [enable_line_numbers=" | + | <code - mymod.classtweaker [enable_line_numbers=" |
| classTweaker v1 named | classTweaker v1 named | ||
| - | inject-interface net/ | + | inject-interface net/ |
| - | </yarncode> | + | </code> |
| which would generate the implementation: | which would generate the implementation: | ||
| < | < | ||
| Line 99: | Line 100: | ||
| < | < | ||
| - | Optional< | + | Optional< |
| </ | </ | ||
tutorial/interface_injection.1764285474.txt.gz · Last modified: 2025/11/27 23:17 by earthcomputer