001package net.tnemc.item.paper.platform.impl.old;
002/*
003 * The New Item Library
004 * Copyright (C) 2022 - 2025 Daniel "creatorfromhell" Vidmar
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020
021import net.tnemc.item.component.helper.PatternData;
022import net.tnemc.item.component.impl.BannerPatternsComponent;
023import net.tnemc.item.paper.PaperItemStack;
024import net.tnemc.item.paper.platform.PaperItemPlatform;
025import org.bukkit.Color;
026import org.bukkit.DyeColor;
027import org.bukkit.block.banner.Pattern;
028import org.bukkit.block.banner.PatternType;
029import org.bukkit.inventory.ItemStack;
030import org.bukkit.inventory.meta.BannerMeta;
031
032import java.util.Optional;
033
034/**
035 * PaperOldModernBannerPatternsComponent
036 *
037 * @author creatorfromhell
038 * @since 0.2.0.0
039 */
040public class PaperOldBannerPatternsComponent extends BannerPatternsComponent<PaperItemStack, ItemStack> {
041
042  /**
043   * @param version the version being used when this check is called.
044   *
045   * @return true if this check is enabled for the version, otherwise false
046   * @since 0.2.0.0
047   */
048  @Override
049  public boolean enabled(final String version) {
050
051    return true;
052  }
053
054  /**
055   * @param serialized the serialized item stack to use
056   * @param item       the item that we should use to apply this applicator to.
057   *
058   * @return the updated item.
059   * @since 0.2.0.0
060   */
061  @Override
062  public ItemStack apply(final PaperItemStack serialized, final ItemStack item) {
063
064    final Optional<PaperOldBannerPatternsComponent> componentOptional = serialized.component(identifier());
065    componentOptional.ifPresent(component->{
066
067      if(item.hasItemMeta() && item.getItemMeta() instanceof final BannerMeta meta) {
068
069        for(final PatternData pattern : componentOptional.get().patterns) {
070
071          final DyeColor color = DyeColor.getByColor(Color.fromRGB(Integer.parseInt(pattern.getColor())));
072
073          if(color != null) {
074
075            try {
076
077              meta.addPattern(new Pattern(color, PaperItemPlatform.instance().converter().convert(pattern.getPattern(), PatternType.class)));
078            } catch(final Exception ignore) {
079
080              //pattern type doesn't exist.
081            }
082          }
083        }
084
085        item.setItemMeta(meta);
086      }
087    });
088    return item;
089  }
090
091  /**
092   * @param item       the item that we should use to deserialize.
093   * @param serialized the serialized item stack we should use to apply this deserializer to
094   *
095   * @return the updated serialized item.
096   * @since 0.2.0.0
097   */
098  @Override
099  public PaperItemStack serialize(final ItemStack item, final PaperItemStack serialized) {
100
101    if(item.hasItemMeta() && item.getItemMeta() instanceof final BannerMeta meta) {
102      for(final Pattern pattern : meta.getPatterns()) {
103
104        try {
105          patterns.add(new PatternData(String.valueOf(pattern.getColor().getColor().asRGB()),
106                                       PaperItemPlatform.instance().converter().convert(pattern.getPattern(), String.class)));
107        } catch(final Exception ignore) {
108
109          //key isn't found
110        }
111
112      }
113    }
114
115    serialized.applyComponent(this);
116    return serialized;
117  }
118
119  /**
120   * Checks if this component applies to the specified item.
121   *
122   * @param item The item to check against.
123   *
124   * @return True if this component applies to the item, false otherwise.
125   * @since 0.2.0.0
126   */
127  @Override
128  public boolean appliesTo(final ItemStack item) {
129
130    return item.hasItemMeta() && item.getItemMeta() instanceof BannerMeta;
131  }
132}