tutorial:interface_injection

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
tutorial:interface_injection [2025/11/27 23:22] – Fix . to / earthcomputertutorial:interface_injection [2025/11/28 00:09] (current) – Emphasize that implementing an injected interface with mixin is no longer necessary earthcomputer
Line 33: Line 33:
 </yarncode> </yarncode>
  
-:!: 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 ''default''. You can make it return null or throw ''UnsupportedOperationException'', but you //cannot// make it abstract, such as ''Optional<yarn class_3414> getBucketEmptySound()'', or exceptions will be thrown when compiling! +:!: 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 ''default''. You can make it return null or throw ''UnsupportedOperationException'', but you //cannot// make it abstract, such as ''Optional<yarn class_3414> getBucketEmptySound()'', or exceptions will be thrown when compiling!
- +
-ℹ️ 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 =====
  
-Now you need to implement this interface into ''<yarn net.minecraft.class_3609>'' with a mixin implementing the 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, you need to implement this interface on a mixin to ''<yarn net.minecraft.class_3609>'' if you want to implement any methods without providing the implementation in a default method:
  
 <yarncode java [enable_line_numbers="false"]> <yarncode java [enable_line_numbers="false"]>
Line 51: Line 49:
 } }
 </yarncode> </yarncode>
 +
 +ℹ️ 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 65:
 </code> </code>
  
-To make your interface injection visible to other mods depending on your mod (if you are writing a library mod), use ''transitive-inject-interface'' instead of ''inject-interface''.+ℹ️ To make your interface injection visible to other mods depending on your mod (if you are writing a library mod), use ''transitive-inject-interface'' instead of ''inject-interface''.
  
 ==== Generic interfaces ==== ==== Generic interfaces ====
tutorial/interface_injection.txt · Last modified: 2025/11/28 00:09 by earthcomputer