All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minestom.server.item.component.BannerPatterns Maven / Gradle / Ivy

There is a newer version: 7320437640
Show newest version
package net.minestom.server.item.component;

import net.kyori.adventure.nbt.BinaryTag;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.minestom.server.color.DyeColor;
import net.minestom.server.instance.block.banner.BannerPattern;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.network.NetworkBufferTemplate;
import net.minestom.server.registry.DynamicRegistry;
import net.minestom.server.utils.nbt.BinaryTagSerializer;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public record BannerPatterns(@NotNull List layers) {
    public static final int MAX_LAYERS = 1024;

    public static final NetworkBuffer.Type NETWORK_TYPE = Layer.NETWORK_TYPE.list(MAX_LAYERS).transform(BannerPatterns::new, BannerPatterns::layers);
    public static final BinaryTagSerializer NBT_TYPE = Layer.NBT_TYPE.list().map(BannerPatterns::new, BannerPatterns::layers);

    public record Layer(@NotNull DynamicRegistry.Key pattern, @NotNull DyeColor color) {
        public static final NetworkBuffer.Type NETWORK_TYPE = NetworkBufferTemplate.template(
                BannerPattern.NETWORK_TYPE, Layer::pattern,
                DyeColor.NETWORK_TYPE, Layer::color,
                Layer::new
        );
        public static final BinaryTagSerializer NBT_TYPE = new BinaryTagSerializer() {
            @Override
            public @NotNull BinaryTag write(@NotNull Context context, @NotNull Layer value) {
                return CompoundBinaryTag.builder()
                        .put("pattern", BannerPattern.NBT_TYPE.write(value.pattern))
                        .put("color", DyeColor.NBT_TYPE.write(value.color))
                        .build();
            }

            @Override
            public @NotNull Layer read(@NotNull Context context, @NotNull BinaryTag tag) {
                if (!(tag instanceof CompoundBinaryTag compound))
                    throw new IllegalArgumentException("Expected a compound tag");
                return new Layer(BannerPattern.NBT_TYPE.read(context, compound.get("pattern")),
                        DyeColor.NBT_TYPE.read(context, compound.get("color")));
            }
        };
    }

    public BannerPatterns {
        layers = List.copyOf(layers);
    }

    public BannerPatterns(@NotNull Layer layer) {
        this(List.of(layer));
    }

    public BannerPatterns(@NotNull DynamicRegistry.Key pattern, @NotNull DyeColor color) {
        this(new Layer(pattern, color));
    }

    public @NotNull BannerPatterns with(@NotNull Layer layer) {
        List layers = new ArrayList<>(this.layers);
        layers.add(layer);
        return new BannerPatterns(layers);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy