Mixins
Note
This page applies to SpongeCommon, SpongeForge, and SpongeVanilla as these three repositories utilize Mixins to hook into the underlying implementations (Vanilla Minecraft server and Forge).
Mixins are a way of modifying java code at runtime by adding additional behavior to classes. They enable transplanting of intended behavior into existing Minecraft objects. Mixins are necessary for all official Sponge implementations to function.
A basic introduction to some of the core concepts underpinning the mixin functionality we’re using to implement Sponge is available at the Mixin Wiki
It starts with absolute basics. If you’re an experienced java developer, feel free to skip to section 4, where the mixins themselves are actually discussed.
Hvis du ønsker å komme i undersøker med å skrive mixins,eksemplene vi også sterkt at du nøye gang alle anbefaler i `SpongeForge-depotet<https://github.com/SpongePowered/SpongeCommon/tree/stable-5/src/example/java/org/spongepowered>`__ som er omfattende dokumentert og scenariene mange av de mer komplekse dekker. Du bør også konsultere Javadoc av Mixin-depotet selv, siden dokumentert alt er allerede nesten.
Caveat: When contributing mixins, note that you can use neither anonymous classes nor lambda expressions.
This means expressions like the following will cause mixins to fail horribly and bring death and destruction upon all that attempt to use Sponge.
return new Predicate<ItemStack>() {
@Override
public boolean test(ItemStack input) {
return input.getItem().equals(Items.golden_apple);
}
}
return input -> input.getItem().equals(Items.golden_apple);
return this::checkItem;
This applies to all classes that are annotated with @Mixin
. Classes that are not touched by the mixin processor may
make use of those features. However, you can use a static utility class to create your anonymous classes, as unlike
your mixin class that utility class will still exist at runtime, while your mixin class will be merged into the
specified target class. The following code therefore will work.
public class ItemUtil {
public static Predicate<ItemStack> typeChecker(final Item item) {
return new Predicate<ItemStack>() {
@Override
public boolean test(ItemStack input) {
return input.getItem().equals(item);
}
}
}
}
@Mixin(TargetClass.class)
public abstract class SomeMixin {
public Predicate<ItemStack> someFunction() {
return ItemUtil.typeChecker(Items.golden_apple);
}
}
Note
The Mixin project will be servicing a number of other projects in addition to Sponge itself. Therefore Mixin has its’ own documentation together with the repository.