tutorial:mixin_injects
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:mixin_injects [2020/02/07 01:58] – draylar | tutorial:mixin_injects [2022/08/05 14:14] – clomclem | ||
---|---|---|---|
Line 2: | Line 2: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | Injects allows you to place custom code at a specified position inside an existing method. For a working example, view the [[https:// | + | Injects allows you to place custom code at a specified position inside an existing method. For a working example, view the [[tutorial: |
<code java> | <code java> | ||
- | @Inject(method = "", | + | @Inject(method = "METHODNAME", at = @At(" |
private void injectMethod(METHOD ARGS, CallbackInfo info) { | private void injectMethod(METHOD ARGS, CallbackInfo info) { | ||
Line 19: | Line 19: | ||
| TAIL | Before the final return statement | | | TAIL | Before the final return statement | | ||
- | In the case of injection points that reference statements or members, the target value can be set inside //@At//. | + | In the case of injection points that reference statements or members, the target value can be set inside //@At//. Target value is specified using JVM bytecode descriptors. |
- | //@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' | + | Oracle defines the following [[https://docs.oracle.com/javase/specs/ |
+ | |||
+ | ^ Descriptor ^ Primitive ^ Description ^ | ||
+ | | B | byte | signed byte | | ||
+ | | C | char | Unicode character code point in the Basic Multilingual Plane, encoded with UTF-16 | | ||
+ | | D | double | double-precision floating-point value | | ||
+ | | F | float | single-precision floating-point value | | ||
+ | | I | int | integer | | ||
+ | | J | long | long integer | | ||
+ | | L// | ||
+ | | S | short | signed short | | ||
+ | | Z | boolean | '' | ||
+ | | [ | reference | one array dimension | | ||
+ | |||
+ | A method descriptor is comprised of the method name, followed by a set of parentheses containing the parameter types, followed by the return type. A method defined in Java as '' | ||
+ | |||
+ | In the case that the return type is void, you need to use V (Void Descriptor Type) as the type (for example, '' | ||
+ | |||
+ | Generics' | ||
+ | |||
+ | '' | ||
=== Returning & Cancelling from Inject === | === Returning & Cancelling from Inject === | ||
Line 31: | Line 51: | ||
=== Injecting into Constructors === | === Injecting into Constructors === | ||
- | To inject into a constructor, | + | To inject into a constructor, |
+ | |||
+ | To inject into a static constructor, | ||
===== Practical Example ===== | ===== Practical Example ===== | ||
- | The following example injects a print statement at the top of '' | + | The following example injects a print statement at the top of '' |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 46: | Line 68: | ||
</ | </ | ||
- | For more information on this particular example, view its usage in the [[Fabric Example Mod repo]]. | + | For more information on this particular example, view its usage in the [[https:// |
tutorial/mixin_injects.txt · Last modified: 2022/08/05 19:19 by clomclem