User Tools

Site Tools


tutorial:mixin_injects

This is an old revision of the document!


Mixin Injects

Introduction

Injects allows you to place custom code at a specified position inside an existing method. For a working example, view the Practical Example category 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.

Returning & Cancelling from Inject

To cancel or return early inside a method, use CallbackInfo#cancel or CallbackInfoReturnable<T>#setReturnValue(T). In both instances, cancellable will have to be set to true in the inject annotation:

@Inject(method = "...", at = @At("..."), cancellable = true)

Practical Example

The following example injects a print statement at the top of TitleScreen#init.

@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!");
	}
}

For more information on this particular example, view its usage in the Fabric Example Mod repo.

tutorial/mixin_injects.1581040122.txt.gz · Last modified: 2020/02/07 01:48 by draylar