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.RarityComponent;
022import net.tnemc.item.paper.PaperItemStack;
023import net.tnemc.item.paper.platform.PaperItemPlatform;
024import net.tnemc.item.providers.VersionUtil;
025import org.bukkit.inventory.ItemRarity;
026import org.bukkit.inventory.ItemStack;
027import org.bukkit.inventory.meta.ItemMeta;
028
029import java.util.Optional;
030
031/**
032 * PaperOldRarityComponent
033 *
034 * @author creatorfromhell
035 * @since 0.2.0.0
036 */
037public class PaperOldRarityComponent extends RarityComponent<PaperItemStack, ItemStack> {
038
039  /**
040   * @param version the version being used when this check is called.
041   *
042   * @return true if this check is enabled for the version, otherwise false
043   * @since 0.2.0.0
044   */
045  @Override
046  public boolean enabled(final String version) {
047
048    //TODO: Look up version
049    return VersionUtil.isOneThirteen(version);
050  }
051
052  /**
053   * @param serialized the serialized item stack to use
054   * @param item       the item that we should use to apply this applicator to.
055   *
056   * @return the updated item.
057   * @since 0.2.0.0
058   */
059  @Override
060  public ItemStack apply(final PaperItemStack serialized, final ItemStack item) {
061
062    final Optional<PaperOldRarityComponent> componentOptional = serialized.component(identifier());
063
064    if(componentOptional.isPresent()) {
065
066      final ItemMeta meta = item.getItemMeta();
067      if(meta != null) {
068
069        meta.setRarity(PaperItemPlatform.instance().converter().convert(componentOptional.get().rarity,
070                                                                       ItemRarity.class));
071        item.setItemMeta(meta);
072      }
073    }
074    return item;
075  }
076
077  /**
078   * @param item       the item that we should use to deserialize.
079   * @param serialized the serialized item stack we should use to apply this deserializer to
080   *
081   * @return the updated serialized item.
082   * @since 0.2.0.0
083   */
084  @Override
085  public PaperItemStack serialize(final ItemStack item, final PaperItemStack serialized) {
086
087    final ItemMeta meta = item.getItemMeta();
088    if(meta != null && meta.hasRarity()) {
089
090      rarity = PaperItemPlatform.instance().converter().convert(meta.getRarity(), String.class);
091    }
092    serialized.applyComponent(this);
093    return serialized;
094  }
095
096  /**
097   * Checks if this component applies to the specified item.
098   *
099   * @param item The item to check against.
100   *
101   * @return True if this component applies to the item, false otherwise.
102   * @since 0.2.0.0
103   */
104  @Override
105  public boolean appliesTo(final ItemStack item) {
106
107    return item.getItemMeta() != null;
108  }
109}