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
Next revision
Previous revision
tutorial:interface_injection [2022/03/08 21:06] juuztutorial:interface_injection [2022/04/13 07:20] (current) – external edit 127.0.0.1
Line 1: Line 1:
-======= Interface injection =======+======= Interface Injection =======
  
 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 10: Line 10:
 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 to get the sound of the bucket when emptied. +The scope of this example is to add the following method into ''<yarn net.minecraft.class_3609>'' to get the sound of the bucket when emptied. 
-This, normally, is not possible because FlowableFluid does not has a similar method.+This, normally, is not possible because ''<yarn net.minecraft.class_3609>'' does not has a similar method.
  
-<code java [enable_line_numbers="false"]> +<yarncode java [enable_line_numbers="false"]> 
-Optional<SoundEvent> getBucketEmptySound() +Optional<class_3414> getBucketEmptySound() 
-</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="false"]>+<yarncode java [enable_line_numbers="false"]>
 package net.fabricmc.example; package net.fabricmc.example;
  
Line 25: Line 25:
  // The methods in an injected interface MUST be default,  // The methods in an injected interface MUST be default,
  // otherwise code referencing them won't compile!  // otherwise code referencing them won't compile!
- default Optional<SoundEvent> getBucketEmptySound() {+ default Optional<class_3414> getBucketEmptySound() {
  return Optional.empty();  return Optional.empty();
  }  }
 } }
-</code>+</yarncode>
  
-Now you need to implement this interface into FlowableFluid 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:
  
-<code java [enable_line_numbers="false"]> +<yarncode java [enable_line_numbers="false"]> 
-@Mixin(FlowableFluid.class)+@Mixin(class_3609.class)
 public class MixinFlowableFluid implements BucketEmptySoundGetter { public class MixinFlowableFluid implements BucketEmptySoundGetter {
  @Override  @Override
- public Optional<SoundEvent> getBucketEmptySound() { + public Optional<class_3414> getBucketEmptySound() { 
- //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(((FlowableFluid) (Object) this).isIn(FluidTags.LAVA) ? SoundEvents.ITEM_BUCKET_EMPTY_LAVA SoundEvents.ITEM_BUCKET_EMPTY);+     return Optional.of(((class_3609) (Object) this).method_15791(class_3486.field_15518) ? class_3417.field_15010 class_3417.field_14834);
  }  }
 } }
-</code>+</yarncode>
  
-Lastly you need to inject the interface into FlowableFluid+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 net/minecraft/fluid/FlowableFluid 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'').
  
Line 60: Line 60:
 Now you can use the new method: Now you can use the new method:
  
-<code java [enable_line_numbers="false"]> +<yarncode java [enable_line_numbers="false"]> 
-Optional<SoundEvent> sound = mytestfluid.getBucketEmptySound(); +Optional<class_3414> sound = mytestfluid.getBucketEmptySound(); 
-</code>+</yarncode>
  
-You could also override this method in classes extending FlowableFluid to implement custom behaviours.+You could also override this method in classes extending <yarn class_3609> to implement custom behaviours.
tutorial/interface_injection.1646773611.txt.gz · Last modified: 2022/03/08 21:06 by juuz