Search in sources :

Example 1 with ShieldItem

use of net.minecraft.world.item.ShieldItem in project SpongeCommon by SpongePowered.

the class ShieldItemStackData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(ItemStack.class).create(Keys.DYE_COLOR).get(h -> {
        final CompoundTag tag = h.getTagElement(Constants.Item.BLOCK_ENTITY_TAG);
        if (tag == null || tag.contains(Constants.TileEntity.Banner.BANNER_PATTERNS, Constants.NBT.TAG_LIST)) {
            return DyeColors.WHITE.get();
        }
        final int id = tag.getInt(Constants.TileEntity.Banner.BANNER_BASE);
        return (DyeColor) (Object) net.minecraft.world.item.DyeColor.byId(id);
    }).set((h, v) -> {
        final CompoundTag tag = h.getOrCreateTagElement(Constants.Item.BLOCK_ENTITY_TAG);
        tag.putInt(Constants.TileEntity.Banner.BANNER_BASE, ((net.minecraft.world.item.DyeColor) (Object) v).getId());
    }).supports(h -> h.getItem() instanceof ShieldItem).create(Keys.BANNER_PATTERN_LAYERS).get(h -> {
        final CompoundTag tag = h.getTagElement(Constants.Item.BLOCK_ENTITY_TAG);
        if (tag == null || !tag.contains(Constants.TileEntity.Banner.BANNER_PATTERNS, Constants.NBT.TAG_LIST)) {
            return new ArrayList<>();
        }
        final ListTag layersList = tag.getList(Constants.TileEntity.Banner.BANNER_PATTERNS, Constants.NBT.TAG_COMPOUND);
        return layersList.stream().map(layer -> ShieldItemStackData.layerFromNbt((CompoundTag) layer)).collect(Collectors.toList());
    }).set((h, v) -> {
        final ListTag layersTag = v.stream().filter(layer -> layer.shape() != BannerPatternShapes.BASE.get()).map(ShieldItemStackData::layerToNbt).collect(NBTCollectors.toTagList());
        final CompoundTag blockEntity = h.getOrCreateTagElement(Constants.Item.BLOCK_ENTITY_TAG);
        blockEntity.put(Constants.TileEntity.Banner.BANNER_PATTERNS, layersTag);
        if (h.getItem() instanceof ShieldItem) {
            // TODO reject BannerPatternShapes.BASE for BannerItem?
            v.stream().filter(layer -> layer.shape() == BannerPatternShapes.BASE.get()).forEach(layer -> {
                blockEntity.putInt(Constants.TileEntity.Banner.BANNER_BASE, ((net.minecraft.world.item.DyeColor) (Object) layer.color()).getId());
            });
        }
    }).supports(h -> h.getItem() instanceof ShieldItem || h.getItem() instanceof BannerItem);
}
Also used : ShieldItem(net.minecraft.world.item.ShieldItem) DyeColor(org.spongepowered.api.data.type.DyeColor) Constants(org.spongepowered.common.util.Constants) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) BannerPattern(net.minecraft.world.level.block.entity.BannerPattern) ArrayList(java.util.ArrayList) BannerItem(net.minecraft.world.item.BannerItem) Keys(org.spongepowered.api.data.Keys) CompoundTag(net.minecraft.nbt.CompoundTag) BannerPatternLayer(org.spongepowered.api.data.meta.BannerPatternLayer) DyeColors(org.spongepowered.api.data.type.DyeColors) NBTCollectors(org.spongepowered.common.util.NBTCollectors) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) Map(java.util.Map) BannerPatternShape(org.spongepowered.api.data.type.BannerPatternShape) ItemStack(net.minecraft.world.item.ItemStack) BannerPatternShapes(org.spongepowered.api.data.type.BannerPatternShapes) ListTag(net.minecraft.nbt.ListTag) ShieldItem(net.minecraft.world.item.ShieldItem) BannerItem(net.minecraft.world.item.BannerItem) ArrayList(java.util.ArrayList) DyeColor(org.spongepowered.api.data.type.DyeColor) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 ListTag (net.minecraft.nbt.ListTag)1 BannerItem (net.minecraft.world.item.BannerItem)1 ItemStack (net.minecraft.world.item.ItemStack)1 ShieldItem (net.minecraft.world.item.ShieldItem)1 BannerPattern (net.minecraft.world.level.block.entity.BannerPattern)1 Keys (org.spongepowered.api.data.Keys)1 BannerPatternLayer (org.spongepowered.api.data.meta.BannerPatternLayer)1 BannerPatternShape (org.spongepowered.api.data.type.BannerPatternShape)1 BannerPatternShapes (org.spongepowered.api.data.type.BannerPatternShapes)1 DyeColor (org.spongepowered.api.data.type.DyeColor)1 DyeColors (org.spongepowered.api.data.type.DyeColors)1 DataProviderRegistrator (org.spongepowered.common.data.provider.DataProviderRegistrator)1 Constants (org.spongepowered.common.util.Constants)1 NBTCollectors (org.spongepowered.common.util.NBTCollectors)1