Mixins

Nota

Esta página aplica para SpongeCommon, SpongeForge y SpongeVanilla estos tres repositorios utilizan Mixins para enlazarse con las implementaciones subyacentes (Vanilla Minecraft server and Forge).

Mixins son una manera de modificar códigos de Java en tiempo de ejecución añadiendo comportamiento adicional a las clases. Ellos permiten transplante del comportamiento previsto dentro de los objetivos de Minecraft existentes. Mixins son necesarios para poder funcionar todas las implementaciones oficiales de Sponge.

Una introducción básica a algunos de los conceptos fundamentales que sustentan la función de Mixin que nosotros usamos para implementar Sponge esta disponible en el Mixin Wiki

Comienza con los aspectos más básicos. Si usted es un desarrollador de Java experimentado, siéntase libre de ir directamente hacia la sección 4, en la cual se tratan las combinaciones.

If you’re looking to get started writing mixins, we also strongly recommend carefully examining all of the examples in the SpongeCommon repository which are extensively documented and cover many of the more complex scenarios. You should also consult the Javadoc of the Mixin repository itself, since almost everything is already documented.

Nota

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.

Mixins and Inner Classes

While you can use lambdas, anonymous and inner classes inside mixins, you cannot use an anonymous/inner class within another anonymous/inner class that is also inside a mixin, unless one of the inner classes is static.

Esto significa expresiones como las siguientes que causaran mixins a fallar horriblemente y traer muerte y destrucción a todos los que intenten utilizar Sponge.

return new Collection<ItemStack>() {
    @Override
    public Iterator<ItemStack> iterator() {
        return new Iterator<ItemStack>() {
            // THIS WILL NOT WORK!

            @Override
            public boolean hasNext() {
                // Code skipped
            }

            @Override
            public ItemStack next() {
                // Code skipped
            }
        };
    }

    // Other methods skipped
};

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. There are two ways to work around this.

One option is to use a separate utility class, 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 SampleCollection implements Collection<ItemStack> {

    private final TargetClass target;

    public SampleCollection(TargetClass target) {
        this.target = target;
    }

    @Override
    public Iterator<ItemStack> iterator() {
        return new Iterator<ItemStack>() {

            @Override
            public boolean hasNext() {
                // Code skipped
            }

            @Override
            public ItemStack next() {
                // Code skipped
            }
        };
    }

    // Other methods skipped
}

@Mixin(TargetClass.class)
public abstract class SomeMixin {
    public Collection<ItemStack> someFunction() {
        return new SampleCollection((TargetClass) (Object) this);
    }
}

The other option is simply to place all of the nested inner classes directly into the mixin or one of its methods, as opposed to one inside the other. For example:

@Mixin(TargetClass.class)
public abstract class SomeMixin {

    private final class SampleIterator implements Iterator<ItemStack> {

        @Override
        public boolean hasNext() {
            // Code skipped
        }

        @Override
        public ItemStack next() {
            // Code skipped
        }
    }

    public Collection<ItemStack> someFunction() {
        return new Collection<ItemStack>() {
            @Override
            public Iterator<ItemStack> iterator() {
                return new SampleIterator();
            }

            // Other methods skipped
        };
    }
}