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.ModelDataOldComponent;
022import net.tnemc.item.paper.PaperItemStack;
023import net.tnemc.item.providers.VersionUtil;
024import org.bukkit.inventory.ItemStack;
025import org.bukkit.inventory.meta.ItemMeta;
026
027import java.util.Optional;
028
029/**
030 * BukkitModelDataOldComponent
031 *
032 * @author creatorfromhell
033 * @since 0.2.0.0
034 */
035public class PaperOldModelDataLegacyComponent extends ModelDataOldComponent<PaperItemStack, ItemStack> {
036
037  public PaperOldModelDataLegacyComponent() {
038
039  }
040
041  public PaperOldModelDataLegacyComponent(final int modelData) {
042
043    super(modelData);
044  }
045
046  /**
047   * Checks if this component applies to the specified item.
048   *
049   * @param item The item to check against.
050   *
051   * @return True if this component applies to the item, false otherwise.
052   * @since 0.2.0.0
053   */
054  @Override
055  public boolean appliesTo(final ItemStack item) {
056
057    return item.hasItemMeta() && item.getItemMeta() != null;
058  }
059
060  /**
061   * @param serialized the serialized item stack to use
062   * @param item       the item that we should use to apply this applicator to.
063   *
064   * @return the updated item.
065   * @since 0.2.0.0
066   */
067  @Override
068  public ItemStack apply(final PaperItemStack serialized, final ItemStack item) {
069
070    final Optional<PaperOldModelDataLegacyComponent> componentOptional = serialized.component(identifier());
071    if(componentOptional.isEmpty()) {
072      return item;
073    }
074
075    final ItemMeta meta = item.getItemMeta();
076    if(meta == null) {
077      return item;
078    }
079
080    if(modelData == -1) {
081      meta.setCustomModelData(null);
082    } else {
083      meta.setCustomModelData(modelData);
084    }
085
086    item.setItemMeta(meta);
087    return item;
088  }
089
090  /**
091   * @param version the version being used when this check is called.
092   *
093   * @return true if this check is enabled for the version, otherwise false
094   * @since 0.2.0.0
095   */
096  @Override
097  public boolean enabled(final String version) {
098
099    return VersionUtil.isLessThan(version, "1.21.4");
100  }
101
102  /**
103   * @param item       the item that we should use to deserialize.
104   * @param serialized the serialized item stack we should use to apply this deserializer to
105   *
106   * @return the updated serialized item.
107   * @since 0.2.0.0
108   */
109  @Override
110  public PaperItemStack serialize(final ItemStack item, final PaperItemStack serialized) {
111
112    if(item.hasItemMeta() && item.getItemMeta().hasCustomModelData()) {
113
114      this.modelData = item.getItemMeta().getCustomModelData();
115    }
116
117    serialized.applyComponent(this);
118    return serialized;
119  }
120}