Mods/SpongePowered Mixin/@Mixin

From Wildermyth Wiki

Modded Content Icon.png      Modded Content: This content is part of the Mixin coremod

Annotation @Mixin

Fully Qualified Name org.spongepowered.asm.mixin.Mixin
Meta Annotations
@Target TYPE
@Retention CLASS

The main decorator for defining a mixin. This annotation defines a class as a mixin class. Mixin classes are not traditional classes, as they cannot be referenced at runtime (With the exception of Accessor Mixins). Instead, when a class targeted by a mixin is classloaded, the transformations defined within this mixin are applied to the target class.

Every mixin requires at least one target class in order to be valid. Target classes can be specified using class literals in value. If a target class is not publicly available at compile time, it may be specified in targets instead.

Element Summary
Modifier and Type Element Name Default Description
int priority 1000 Priority for the mixin, relative to other mixins targeting the same classes.
boolean remap true Used for obfuscation. Since Wildermyth is not obfuscated, you never need to worry about this.
String[1] targets "" This property allows you to specify package-private, anonymous inner, private inner, unavailable @Pseudo classes, or any other classes that cannot be directly referenced at compile time.
Class<?>[2] value "" Target classes for this mixin

Explanation

Suppose we have this class:

public class Example {

  public static void printFoo() {
    System.out.println("Foo");
  }

}

And we want to change the behavior so that "Foo" and "Bar" are printed. We can target the class with @Mixin, then @Inject at the tail of the method to do so.

@Mixin(Example.class)       //Example.class is our target class
public class FooBarMixin {

  @Inject(                    //We are injecting
    method = "printFoo",        //into the 'printFoo' method 
    at = @At("TAIL")            //at the tail end of the method
  )
  private static void makeFooAlsoPrintBar(CallbackInfo callback) {
    System.out.println("Bar");
  }

}

This effectively changes the logic of Example.class to:

public class Example {

  public static void printFoo() {
    System.out.println("Foo");
    System.out.println("Bar");
  }

}

Executing Example.printFoo(); would then print the following:

Foo
Bar