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.ModelDataComponent;
022import net.tnemc.item.paper.PaperItemStack;
023import net.tnemc.item.providers.VersionUtil;
024import org.bukkit.Color;
025import org.bukkit.inventory.ItemStack;
026import org.bukkit.inventory.meta.ItemMeta;
027import org.bukkit.inventory.meta.components.CustomModelDataComponent;
028
029import java.util.ArrayList;
030import java.util.List;
031import java.util.Optional;
032
033/**
034 * BukkitModelData
035 *
036 * @author creatorfromhell
037 * @since 0.2.0.0
038 */
039public class PaperOldModelDataComponent extends ModelDataComponent<PaperItemStack, ItemStack> {
040
041  /**
042   * Checks if this component applies to the specified item.
043   *
044   * @param item The item to check against.
045   *
046   * @return True if this component applies to the item, false otherwise.
047   * @since 0.2.0.0
048   */
049  @Override
050  public boolean appliesTo(final ItemStack item) {
051
052    return item.hasItemMeta() && item.getItemMeta() != null && item.getItemMeta().hasCustomModelData();
053  }
054
055  /**
056   * @param serialized the serialized item stack to use
057   * @param item       the item that we should use to apply this applicator to.
058   *
059   * @return the updated item.
060   * @since 0.2.0.0
061   */
062  @Override
063  public ItemStack apply(final PaperItemStack serialized, final ItemStack item) {
064
065    final Optional<PaperOldModelDataComponent> componentOptional = serialized.component(identifier());
066
067    if(componentOptional.isPresent()) {
068
069      final ItemMeta meta = item.getItemMeta();
070      if(meta != null) {
071
072        final CustomModelDataComponent component = meta.getCustomModelDataComponent();
073        final List<Color> colourList = new ArrayList<>();
074        for(final String colourStr : colours) {
075
076          try {
077
078            colourList.add(Color.fromARGB(Integer.parseInt(colourStr)));
079          } catch(final Exception ignore) {}
080        }
081
082        component.setColors(colourList);
083        component.setFlags(flags);
084        component.setFloats(floats);
085        component.setStrings(strings);
086        meta.setCustomModelDataComponent(component);
087        item.setItemMeta(meta);
088      }
089    }
090    return item;
091  }
092
093  /**
094   * @param version the version being used when this check is called.
095   *
096   * @return true if this check is enabled for the version, otherwise false
097   * @since 0.2.0.0
098   */
099  @Override
100  public boolean enabled(final String version) {
101
102    return VersionUtil.isOneTwentyOneFour(version);
103  }
104
105  /**
106   * @param item       the item that we should use to deserialize.
107   * @param serialized the serialized item stack we should use to apply this deserializer to
108   *
109   * @return the updated serialized item.
110   * @since 0.2.0.0
111   */
112  @Override
113  public PaperItemStack serialize(final ItemStack item, final PaperItemStack serialized) {
114
115    if(item.hasItemMeta()) {
116
117      final CustomModelDataComponent dataComponent = item.getItemMeta().getCustomModelDataComponent();
118      for(final Color color : dataComponent.getColors()) {
119
120        this.colours.add(String.valueOf(color.asARGB()));
121      }
122
123      this.flags.addAll(dataComponent.getFlags());
124      this.floats.addAll(dataComponent.getFloats());
125      this.strings.addAll(dataComponent.getStrings());
126    }
127
128    serialized.applyComponent(this);
129    return serialized;
130  }
131}