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. Target value is specified using JVM bytecode descriptors.

Oracle defines the following field descriptors:

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
LClassName; reference an instance of ClassName
S short signed short
Z boolean true or false
[ 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 type. A method defined in Java as Object m(int i, double[] d, Thread t) would have the method descriptor m(I[DLjava/lang/Thread;)Ljava/lang/Object;.

Generics' types are left out, as Generics don't exist on runtime. So Pair<Integer, ? extends Task<? super VillagerEntity>‍> would become Lcom/mojang/datafixers/util/Pair.

@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). Note that cancel does not have to be called after setReturnValue. In both instances, cancellable will have to be set to true in the inject annotation:

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

Injecting into Constructors

To inject into a constructor, use <init>()V as the method target, with () containing the constructor argument descriptors. When injecting into constructors, @At must be set to either TAIL or RETURN. No other forms of injection are officially supported. Note that some classes have methods named init which are different from <init>. Don't get confused!

To inject into a static constructor, use <clinit> as the method name.

Practical Example

The following example injects a print statement at the top of TitleScreen#init (note: the method init is a normal method and not a constructor).

@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.

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.

Optional<SoundEvent> getBucketEmptySound()

To add the method into the class, first of all you need to create an interface with it:

package net.fabricmc.example;
 
public interface BucketEmptySoundGetter {
	Optional<SoundEvent> getBucketEmptySound();
}

Now you need to implement this interface into FlowableFluid with a mixin implementing the interface:

@Mixin(FlowableFluid.class)
public class MixinFlowableFluid implements BucketEmptySoundGetter {
	@Override
	public Optional<SoundEvent> getBucketEmptySound() {
 
		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/minecraft/fluid/FlowableFluid class.

{
	"custom": {
		"loom:injected_interfaces": {
			"net/minecraft/class_3609": ["net/fabricmc/example/BucketEmptySoundGetter"]
		}
	}
}

Now you can use the new method:

Optional<SoundEvent> sound = mytestfluid.getBucketEmptySound();

You could also override this method in classes extending FlowableFluid to implement custom behaviours.

tutorial/mixin_injects.1646765768.txt.gz · Last modified: 2022/03/08 18:56 by salvopelux