This is an old revision of the document!
Table of Contents
Mixin Injects
Introduction
Injects allows you to place custom code at a specified position inside an existing method. For a functional example, view the example section at the bottom of this page. The standard form of an inject is as shown:
@Inject(method = "", at = @At(//"INJECTION POINT REFERENCE"//)) private void injectMethod(//METHOD ARGS//, CallbackInfo info) { }
The Injection Point Reference defines where the code inside the method body is injected inside the target method. The following table describes a few of the options:
Name | Description |
---|---|
HEAD | Top of the method |
RETURN | Before every return statement |
INVOKE | At a method call |
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.
@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's arguments are placed first in the method's header, followed by a CallbackInfo
object. If the target method has a return type (T), CallbackInfoReturnable<T>
is used instead of CallbackInfo
.
Annotation
Method Header
Method Body
Practical Example
An example is as follows:
@Mixin(TitleScreen.class) public class ExampleMixin { @Inject(at = @At("HEAD"), method = "init()V") private void init(CallbackInfo info) { System.out.println("This line is printed by an example mod mixin!"); } }