001package net.tnemc.item.bukkit.platform.impl;
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.bukkit.BukkitItemStack;
022import net.tnemc.item.bukkit.platform.BukkitItemPlatform;
023import net.tnemc.item.component.helper.PatternData;
024import net.tnemc.item.component.impl.BannerPatternsComponent;
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.List;
033import java.util.Optional;
034
035/**
036 * BukkitModernBannerPatternsComponent
037 *
038 * @author creatorfromhell
039 * @since 0.2.0.0
040 */
041public class BukkitBannerPatternsComponent extends BannerPatternsComponent<BukkitItemStack, ItemStack> {
042
043  /**
044   * Represents a component that handles banner patterns for an object.
045   * @since 0.2.0.0
046   */
047  public BukkitBannerPatternsComponent() {
048
049  }
050
051  /**
052   * Creates a new BannerPatternsComponent with the provided list of PatternData objects.
053   *
054   * @param patterns The list of PatternData objects to initialize the component with.
055   * @since 0.2.0.0
056   */
057  public BukkitBannerPatternsComponent(final List<PatternData> patterns) {
058
059    super(patterns);
060  }
061
062  /**
063   * @param version the version being used when this check is called.
064   *
065   * @return true if this check is enabled for the version, otherwise false
066   * @since 0.2.0.0
067   */
068  @Override
069  public boolean enabled(final String version) {
070
071    return true;
072  }
073
074  /**
075   * @param serialized the serialized item stack to use
076   * @param item       the item that we should use to apply this applicator to.
077   *
078   * @return the updated item.
079   * @since 0.2.0.0
080   */
081  @Override
082  public ItemStack apply(final BukkitItemStack serialized, final ItemStack item) {
083
084    final Optional<BukkitBannerPatternsComponent> componentOptional = serialized.component(identifier());
085    componentOptional.ifPresent(component->{
086
087      if(item.hasItemMeta() && item.getItemMeta() instanceof final BannerMeta meta) {
088
089        for(final PatternData pattern : componentOptional.get().patterns) {
090
091          final DyeColor color = DyeColor.getByColor(Color.fromRGB(Integer.parseInt(pattern.getColor())));
092
093          if(color != null) {
094
095            try {
096
097              meta.addPattern(new Pattern(color, BukkitItemPlatform.instance().converter().convert(pattern.getPattern(), PatternType.class)));
098            } catch(final Exception ignore) {
099
100              //pattern type doesn't exist.
101            }
102          }
103        }
104
105        item.setItemMeta(meta);
106      }
107    });
108    return item;
109  }
110
111  /**
112   * @param item       the item that we should use to deserialize.
113   * @param serialized the serialized item stack we should use to apply this deserializer to
114   *
115   * @return the updated serialized item.
116   * @since 0.2.0.0
117   */
118  @Override
119  public BukkitItemStack serialize(final ItemStack item, final BukkitItemStack serialized) {
120
121    if(item.hasItemMeta() && item.getItemMeta() instanceof final BannerMeta meta) {
122      for(final Pattern pattern : meta.getPatterns()) {
123
124        try {
125          patterns.add(new PatternData(String.valueOf(pattern.getColor().getColor().asRGB()),
126                                       BukkitItemPlatform.instance().converter().convert(pattern.getPattern(), String.class)));
127        } catch(final Exception ignore) {
128
129          //key isn't found
130        }
131
132      }
133    }
134
135    serialized.applyComponent(this);
136    return serialized;
137  }
138
139  /**
140   * Checks if this component applies to the specified item.
141   *
142   * @param item The item to check against.
143   *
144   * @return True if this component applies to the item, false otherwise.
145   * @since 0.2.0.0
146   */
147  @Override
148  public boolean appliesTo(final ItemStack item) {
149
150    return item.hasItemMeta() && item.getItemMeta() instanceof BannerMeta;
151  }
152}