Mixins

Nota

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.

Una introduzione base ad alcuni dei principali concetti alla base della funzionalità mixin che stiamo usando è disponibile su Mixin Wiki

Inizia con le basi più elementari. Se sei uno sviluppatore java navigato, sentiti libero di saltare alla sezione 4, dove sono propriamente discussi i mixins stessi

Se vuoi iniziare a sviluppare dei mixin, ti raccomandiamo caldamente di esaminare tutti gli esempi nella Repository di SpongeForge dove sono ampiamente documentati e coprono molti degli scenari più complessi. Dovresti inoltre consultare i Javadocs relativi al repository della Mixin stessa, dal momento che è già quasi tutto documentato.

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

Nota

Il progetto Mixin viene usato usato da altri progetti oltre a Sponge stesso. Per questo è dotato di una propria documentazione assime alla repository.