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.impl.TrimComponent;
022import net.tnemc.item.paper.PaperItemStack;
023import net.tnemc.item.paper.platform.PaperItemPlatform;
024import net.tnemc.item.providers.VersionUtil;
025import org.bukkit.inventory.ItemStack;
026import org.bukkit.inventory.meta.ArmorMeta;
027import org.bukkit.inventory.meta.trim.ArmorTrim;
028import org.bukkit.inventory.meta.trim.TrimMaterial;
029import org.bukkit.inventory.meta.trim.TrimPattern;
030
031import java.util.Optional;
032
033/**
034 * PaperOldTrimComponent
035 *
036 * @author creatorfromhell
037 * @since 0.2.0.0
038 */
039public class PaperOldTrimComponent extends TrimComponent<PaperItemStack, ItemStack> {
040
041  /**
042   * @param version the version being used when this check is called.
043   *
044   * @return true if this check is enabled for the version, otherwise false
045   * @since 0.2.0.0
046   */
047  @Override
048  public boolean enabled(final String version) {
049
050    return VersionUtil.isOneTwenty(version);
051  }
052
053  /**
054   * @param serialized the serialized item stack to use
055   * @param item       the item that we should use to apply this applicator to.
056   *
057   * @return the updated item.
058   * @since 0.2.0.0
059   */
060  @Override
061  public ItemStack apply(final PaperItemStack serialized, final ItemStack item) {
062
063    final Optional<PaperOldTrimComponent> componentOptional = serialized.component(identifier());
064    componentOptional.ifPresent(component->{
065
066      if(item.hasItemMeta() && item.getItemMeta() instanceof final ArmorMeta meta) {
067
068        try {
069
070          final TrimMaterial material = PaperItemPlatform.instance().converter().convert(componentOptional.get().material, TrimMaterial.class);
071          final TrimPattern pattern = PaperItemPlatform.instance().converter().convert(componentOptional.get().pattern, TrimPattern.class);
072          if(material != null && pattern != null) {
073
074            meta.setTrim(new ArmorTrim(material, pattern));
075          }
076
077        } catch(final Exception ignore) {
078
079          //invalid material/pattern
080        }
081
082        item.setItemMeta(meta);
083      }
084    });
085    return item;
086  }
087
088  /**
089   * @param item       the item that we should use to deserialize.
090   * @param serialized the serialized item stack we should use to apply this deserializer to
091   *
092   * @return the updated serialized item.
093   * @since 0.2.0.0
094   */
095  @Override
096  public PaperItemStack serialize(final ItemStack item, final PaperItemStack serialized) {
097
098    if(item.hasItemMeta() && item.getItemMeta() instanceof final ArmorMeta meta && meta.getTrim() != null) {
099
100      try {
101
102        final String material = PaperItemPlatform.instance().converter().convert(meta.getTrim().getMaterial(), String.class);
103        final String pattern = PaperItemPlatform.instance().converter().convert(meta.getTrim().getPattern(), String.class);
104        if(material != null && pattern != null) {
105
106          this.material = material;
107          this.pattern = pattern;
108        }
109
110      } catch(final Exception ignore) {
111        //invalid material/pattern
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 ArmorMeta;
131  }
132}