tutorial:mixin_injects
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorial:mixin_injects [2022/03/08 19:00] – salvopelux | tutorial:mixin_injects [2025/09/24 12:13] (current) – Mark page as WIP in preparation for major review and edits upcoming gauntrecluse | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Mixin Injects ====== | + | FIXME //This page is under heavy rewrites due to prior edits being from 2022, and it thus contains seriously outdated advice. This page may change very suddenly and should only be taken with a grain of salt.// |
| + | |||
| + | ====== Mixin Injects | ||
| ===== Introduction ===== | ===== Introduction ===== | ||
| Line 5: | Line 7: | ||
| <code java> | <code java> | ||
| - | @Inject(method = "", | + | @Inject(method = "METHOD NAME OR SIGNATURE", at = @At(" |
| private void injectMethod(METHOD ARGS, CallbackInfo info) { | private void injectMethod(METHOD ARGS, CallbackInfo info) { | ||
| Line 28: | Line 30: | ||
| | D | double | double-precision floating-point value | | | D | double | double-precision floating-point value | | ||
| | F | float | single-precision floating-point value | | | F | float | single-precision floating-point value | | ||
| - | | I | int | integer | | + | | I | int | signed |
| - | | J | long | long integer | | + | | J | long | signed |
| | L// | | L// | ||
| | S | short | signed short | | | S | short | signed short | | ||
| Line 35: | Line 37: | ||
| | [ | reference | one array dimension | | | [ | reference | one array dimension | | ||
| - | A method descriptor is comprised of the method name, followed by a set of parentheses containing the input types, followed by the output | + | A method descriptor is comprised of the method name, followed by a set of parentheses containing the parameter |
| - | Generics' | + | In the case that the return type is void, you need to use V (Void Descriptor Type) as the type (for example, |
| - | //@Inject// methods always have a void return type. The method name does not matter; using something that describes what the inject does is best. The target method' | + | Generics' |
| + | |||
| + | '' | ||
| === Returning & Cancelling from Inject === | === Returning & Cancelling from Inject === | ||
| Line 67: | Line 71: | ||
| For more information on this particular example, view its usage in the [[https:// | For more information on this particular example, view its usage in the [[https:// | ||
| - | |||
| - | ====== Inject an interface ====== | ||
| - | |||
| - | This is a new tecnique introduced by Loom 0.11 to add methods into a specific existing class. | ||
| - | More specifically, | ||
| - | 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=" | ||
| - | Optional< | ||
| - | </ | ||
| - | |||
| - | To add the method into the class, first of all you need to create an interface with it: | ||
| - | |||
| - | <code java [enable_line_numbers=" | ||
| - | package net.fabricmc.example; | ||
| - | |||
| - | public interface BucketEmptySoundGetter { | ||
| - | Optional< | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Now you need to implement this interface into FlowableFluid with a mixin implementing the interface: | ||
| - | |||
| - | <code java [enable_line_numbers=" | ||
| - | @Mixin(FlowableFluid.class) | ||
| - | public class MixinFlowableFluid implements BucketEmptySoundGetter { | ||
| - | @Override | ||
| - | public Optional< | ||
| - | //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); | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | 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/ | ||
| - | |||
| - | <code json [enable_line_numbers=" | ||
| - | { | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Now you can use the new method: | ||
| - | |||
| - | <code java [enable_line_numbers=" | ||
| - | Optional< | ||
| - | </ | ||
| - | |||
| - | You could also override this method in classes extending FlowableFluid to implement custom behaviours. | ||
tutorial/mixin_injects.1646766016.txt.gz · Last modified: 2022/03/08 19:00 by salvopelux