User Tools

Site Tools


tutorial:mixin_injects

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
tutorial:mixin_injects [2022/03/08 19:00] salvopeluxtutorial:mixin_injects [2022/03/08 21:03] – remove interface injection from this incorrect place juuz
Line 67: Line 67:
  
 For more information on this particular example, view its usage in the [[https://github.com/FabricMC/fabric-example-mod/blob/master/src/main/java/net/fabricmc/example/mixin/ExampleMixin.java|Fabric Example Mod repo]]. For more information on this particular example, view its usage in the [[https://github.com/FabricMC/fabric-example-mod/blob/master/src/main/java/net/fabricmc/example/mixin/ExampleMixin.java|Fabric Example Mod repo]].
- 
-====== Inject an interface ====== 
- 
-This is a new tecnique introduced by Loom 0.11 to add methods into a specific existing class. 
-More specifically, you can create an Interface, and then inject this interface into the class. 
-As result the target class will acquire all the methods of the interface, as if it always had them. 
-Interface injection is a compile time only feature, this means that a Mixin should also be used to implement the interface into the target class. 
- 
-This is particulatly 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. 
- 
-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. 
-This, normally, is not possible because FlowableFluid does not has a similar method. 
- 
-<code java [enable_line_numbers="false"]> 
-Optional<SoundEvent> getBucketEmptySound() 
-</code> 
- 
-To add the method into the class, first of all you need to create an interface with it: 
- 
-<code java [enable_line_numbers="false"]> 
-package net.fabricmc.example; 
- 
-public interface BucketEmptySoundGetter { 
- Optional<SoundEvent> getBucketEmptySound(); 
-} 
-</code> 
- 
-Now you need to implement this interface into FlowableFluid with a mixin implementing the interface: 
- 
-<code java [enable_line_numbers="false"]> 
-@Mixin(FlowableFluid.class) 
-public class MixinFlowableFluid implements BucketEmptySoundGetter { 
- @Override 
- public Optional<SoundEvent> getBucketEmptySound() { 
- //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); 
- } 
-} 
-</code> 
- 
-Lastly you need to inject the interface into FabricFluid. 
-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. 
- 
-<code json [enable_line_numbers="false"]> 
-{ 
- "custom": { 
- "loom:injected_interfaces": { 
- "net/minecraft/class_3609": ["net/fabricmc/example/BucketEmptySoundGetter"] 
- } 
- } 
-} 
-</code> 
- 
-Now you can use the new method: 
- 
-<code java [enable_line_numbers="false"]> 
-Optional<SoundEvent> sound = mytestfluid.getBucketEmptySound(); 
-</code> 
- 
-You could also override this method in classes extending FlowableFluid to implement custom behaviours. 
tutorial/mixin_injects.txt · Last modified: 2022/08/05 19:19 by clomclem