Conflict

Notitie

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.

Een basis introductie om enkele van de kernaspecten van de mixin functionaliteit, die we gebruiken bij het implementeren van Sponge, uit te leggen, is beschikbaar op de Mixin Wiki

Het begint volledig van nul. Als je een ervaren Java ontwikkelaar bent kan je beginnen met sectie 4, waar mixins zelf worden besproken.

Als u van plan bent om zelf mixins te schrijven raden we u ten sterkste aan om zorgvuldig alle voorbeelden te bekijken in de SpongeCommon repository, deze zijn uitgebreid gedocumenteerd en omvatten veel van de meer complexe scenario’s. U doet er goed aan om ook de Javadoc van de Mixin repository zelf raad te plegen, aangezien bijna alles daar al gedocumenteerd is.

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);
    }
}

Notitie

Het Mixin project zal gebruikt worden in een aantal projecten buiten Sponge zelf. Hierdoor zal Mixin zijn eigen documentatie hebben samen met de repository.