001package net.tnemc.item.paper.platform.impl.modern;
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 io.papermc.paper.datacomponent.DataComponentTypes;
022import io.papermc.paper.datacomponent.item.CustomModelData;
023import net.tnemc.item.component.impl.ModelDataComponent;
024import net.tnemc.item.paper.PaperItemStack;
025import net.tnemc.item.providers.VersionUtil;
026import org.bukkit.Color;
027import org.bukkit.inventory.ItemStack;
028
029import java.util.LinkedList;
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 PaperModelDataComponent extends ModelDataComponent<PaperItemStack, ItemStack> {
040
041  public PaperModelDataComponent() {
042
043  }
044
045  public PaperModelDataComponent(final List<String> colours, final List<Float> floats, final List<Boolean> flags, final List<String> strings) {
046
047    super(colours, floats, flags, strings);
048  }
049
050  /**
051   * @param version the version being used when this check is called.
052   *
053   * @return true if this check is enabled for the version, otherwise false
054   * @since 0.2.0.0
055   */
056  @Override
057  public boolean enabled(final String version) {
058
059    return VersionUtil.isOneTwentyOneTwo(version);
060  }
061
062  /**
063   * Checks if this component applies to the specified item.
064   *
065   * @param item The item to check against.
066   *
067   * @return True if this component applies to the item, false otherwise.
068   * @since 0.2.0.0
069   */
070  @Override
071  public boolean appliesTo(final ItemStack item) {
072
073    return true;
074  }
075
076  /**
077   * @param serialized the serialized item stack to use
078   * @param item       the item that we should use to apply this applicator to.
079   *
080   * @return the updated item.
081   * @since 0.2.0.0
082   */
083  @Override
084  public ItemStack apply(final PaperItemStack serialized, final ItemStack item) {
085
086    final Optional<PaperModelDataComponent> componentOptional = serialized.component(identifier());
087    if(componentOptional.isEmpty()) {
088      return item;
089    }
090
091    final CustomModelData.Builder builder = CustomModelData.customModelData();
092
093    builder.addFlags(this.flags);
094    builder.addFloats(this.floats);
095    builder.addStrings(this.strings);
096
097    final List<Color> colorList = new LinkedList<>();
098    for(final String colorStr : this.colours) {
099
100      try {
101
102        colorList.add(Color.fromARGB(Integer.parseInt(colorStr)));
103
104      } catch(final Exception ignore){}
105    }
106    builder.addColors(colorList);
107
108    item.setData(DataComponentTypes.CUSTOM_MODEL_DATA, builder);
109
110    return item;
111  }
112
113  /**
114   * @param item       the item that we should use to deserialize.
115   * @param serialized the serialized item stack we should use to apply this deserializer to
116   *
117   * @return the updated serialized item.
118   * @since 0.2.0.0
119   */
120  @Override
121  public PaperItemStack serialize(final ItemStack item, final PaperItemStack serialized) {
122
123    final CustomModelData color = item.getData(DataComponentTypes.CUSTOM_MODEL_DATA);
124    if(color == null) {
125      return serialized;
126    }
127
128    this.flags.addAll(color.flags());
129    this.floats.addAll(color.floats());
130    this.strings.addAll(color.strings());
131
132    for(final Color colorObj : color.colors()) {
133      this.colours.add(String.valueOf(colorObj.asARGB()));
134    }
135
136    serialized.applyComponent(this);
137    return serialized;
138  }
139}