Mixins

Not

Bu sayfa, SpongeCommon, SpongeForge ve SpongeVanilla için geçerlidir, çünkü bu üç depo, temel uygulamalara (Vanilla Minecraft sunucusu ve Forge) bağlanmak için Mixins kullanmaktadır.

Mixins, sınıflara ek davranış ekleyerek çalışma zamanında java kodunu değiştirmenin bir yoludur. Mevcut Minecraft nesnelerine istenilenin yapılmasını sağlarlar. Mixins, tüm resmi Sponge uygulamalarının çalışması için gereklidir.

Sponge uygulamalarında kullandığımız mixin işlevselliğini destekleyen temel kavramların bazılarına`Mixin Wiki’de <https://github.com/SpongePowered/Mixin/wiki/>` __ adresinde bulunabilir

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.

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.

Not

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.

Bunun anlamı mixinlerin çok hatalı ve Sponge kullanmak için yapılan tüm işlemlerin başarısız olmasına neden olacaktır.

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