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.StoredEnchantmentsComponent;
022import net.tnemc.item.paper.PaperItemStack;
023import net.tnemc.item.paper.platform.PaperItemPlatform;
024import org.bukkit.enchantments.Enchantment;
025import org.bukkit.inventory.ItemStack;
026import org.bukkit.inventory.meta.EnchantmentStorageMeta;
027
028import java.util.Map;
029import java.util.Optional;
030
031/**
032 * PaperOldStoredEnchantmentsComponent
033 *
034 * @author creatorfromhell
035 * @since 0.2.0.0
036 */
037public class PaperOldStoredEnchantmentsComponent extends StoredEnchantmentsComponent<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    return true;
049  }
050
051  /**
052   * @param serialized the serialized item stack to use
053   * @param item       the item that we should use to apply this applicator to.
054   *
055   * @return the updated item.
056   * @since 0.2.0.0
057   */
058  @Override
059  public ItemStack apply(final PaperItemStack serialized, final ItemStack item) {
060
061    final Optional<PaperOldStoredEnchantmentsComponent> componentOptional = serialized.component(identifier());
062    componentOptional.ifPresent(component->{
063
064      if(item.hasItemMeta() && item.getItemMeta() instanceof final EnchantmentStorageMeta meta) {
065
066        for(final Map.Entry<String, Integer> entry : componentOptional.get().levels.entrySet()) {
067
068          try {
069
070            final Enchantment enchant = PaperItemPlatform.instance().converter().convert(entry.getKey(), Enchantment.class);
071            if(enchant != null) {
072
073              meta.addStoredEnchant(enchant, entry.getValue(), true);
074            }
075          } catch(final Exception ignore) {
076            //enchantment couldn't be found.
077          }
078        }
079      }
080    });
081    return item;
082  }
083
084  /**
085   * @param item       the item that we should use to deserialize.
086   * @param serialized the serialized item stack we should use to apply this deserializer to
087   *
088   * @return the updated serialized item.
089   * @since 0.2.0.0
090   */
091  @Override
092  public PaperItemStack serialize(final ItemStack item, final PaperItemStack serialized) {
093
094    if(item.hasItemMeta() && item.getItemMeta() instanceof final EnchantmentStorageMeta meta) {
095
096      for(final Map.Entry<Enchantment, Integer> entry : meta.getStoredEnchants().entrySet()) {
097
098        levels.put(PaperItemPlatform.instance().converter().convert(entry.getKey(), String.class), entry.getValue());
099      }
100    }
101
102    serialized.applyComponent(this);
103    return serialized;
104  }
105
106  /**
107   * Checks if this component applies to the specified item.
108   *
109   * @param item The item to check against.
110   *
111   * @return True if this component applies to the item, false otherwise.
112   * @since 0.2.0.0
113   */
114  @Override
115  public boolean appliesTo(final ItemStack item) {
116
117    return item.hasItemMeta() && item.getItemMeta() instanceof EnchantmentStorageMeta;
118  }
119}