User Tools

Site Tools


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/02/12 18:42] – Add node on unicode escape for $ earthcomputertutorial: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, ''TagBuilder'' implements ''FabricTagBuilder'', ''BlockEntityType'' implements ''FabricBlockEntityType'', so that you can directly use the instance methods in the Fabric API on the vanilla objects.
  
 Let's explain better with an example: Let's explain better with an example:
Line 14: Line 16:
  
 <yarncode java [enable_line_numbers="false"]> <yarncode java [enable_line_numbers="false"]>
-Optional<class_3414> getBucketEmptySound()+Optional<class_3414> getBucketEmptySound$myMod()
 </yarncode> </yarncode>
  
Line 23: Line 25:
  
 public interface BucketEmptySoundGetter { public interface BucketEmptySoundGetter {
- // The methods in an injected interface MUST be default, + default Optional<class_3414> getBucketEmptySound$myMod() {
- // otherwise code referencing them won't compile! +
- default Optional<class_3414> getBucketEmptySound() {+
  return Optional.empty();  return Optional.empty();
  }  }
 } }
 </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!
 +
 +ℹ️ 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 ''<yarn net.minecraft.class_3609>'' with a mixin implementing the interface: Now you need to implement this interface into ''<yarn net.minecraft.class_3609>'' with a mixin implementing the interface:
Line 37: Line 41:
 public class MixinFlowableFluid implements BucketEmptySoundGetter { public class MixinFlowableFluid implements BucketEmptySoundGetter {
  @Override  @Override
- public Optional<class_3414> getBucketEmptySound() {+ public Optional<class_3414> getBucketEmptySound$myMod() {
      //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 ''<yarn net.minecraft.class_3609>''. Lastly you need to inject the interface into ''<yarn net.minecraft.class_3609>''.
-The following snippet can be added to your fabric.mod.json file to add one or more interfaces to the ''<yarn net.minecraft.class_3609>'' class.+The following snippet can be added to your ''fabric.mod.json'' file to add one or more interfaces to the ''<yarn net.minecraft.class_3609>'' class.
 Note that all class names here must use the "internal names" that use slashes instead of dots (''path/to/my/Class''). Note that all class names here must use the "internal names" that use slashes instead of dots (''path/to/my/Class'').
  
-<code json [enable_line_numbers="false"]>+<code json fabric.mod.json [enable_line_numbers="false"]>
 { {
  "custom": {  "custom": {
Line 61: Line 65:
  
 <yarncode java [enable_line_numbers="false"]> <yarncode java [enable_line_numbers="false"]>
-Optional<class_3414> sound = mytestfluid.getBucketEmptySound();+Optional<class_3414> sound = mytestfluid.getBucketEmptySound$myMod();
 </yarncode> </yarncode>
  
-You could also override this method in classes extending <yarn class_3609> to implement custom behaviours.+You could also override this method in classes extending ''<yarn class_3609>'' to implement custom behaviours.
  
 Sometimes, your interface injections may need to include the ''$'' symbol, but the Groovy template processor may interpret this as a template variable if you are replacing variables (such as ''${version}'') in your ''fabric.mod.json''. A workaround for this is to use the Unicode escape for ''$'', which is ''\u0024''. Sometimes, your interface injections may need to include the ''$'' symbol, but the Groovy template processor may interpret this as a template variable if you are replacing variables (such as ''${version}'') in your ''fabric.mod.json''. A workaround for this is to use the Unicode escape for ''$'', which is ''\u0024''.
tutorial/interface_injection.txt · Last modified: 2025/10/09 14:18 by solidblock