001package net.tnemc.item.bukkit.platform.impl;
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.bukkit.BukkitItemStack;
022import net.tnemc.item.bukkit.platform.BukkitItemPlatform;
023import net.tnemc.item.component.impl.StoredEnchantmentsComponent;
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 * BukkitStoredEnchantmentsComponent
033 *
034 * @author creatorfromhell
035 * @since 0.2.0.0
036 */
037public class BukkitStoredEnchantmentsComponent extends StoredEnchantmentsComponent<BukkitItemStack, ItemStack> {
038
039  public BukkitStoredEnchantmentsComponent() {
040
041  }
042
043  public BukkitStoredEnchantmentsComponent(final Map<String, Integer> levels) {
044
045    super(levels);
046  }
047
048  /**
049   * @param version the version being used when this check is called.
050   *
051   * @return true if this check is enabled for the version, otherwise false
052   * @since 0.2.0.0
053   */
054  @Override
055  public boolean enabled(final String version) {
056
057    return true;
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 BukkitItemStack serialized, final ItemStack item) {
069
070    final Optional<BukkitStoredEnchantmentsComponent> componentOptional = serialized.component(identifier());
071    componentOptional.ifPresent(component->{
072
073      if(item.hasItemMeta() && item.getItemMeta() instanceof final EnchantmentStorageMeta meta) {
074
075        for(final Map.Entry<String, Integer> entry : componentOptional.get().levels.entrySet()) {
076
077          try {
078
079            final Enchantment enchant = BukkitItemPlatform.instance().converter().convert(entry.getKey(), Enchantment.class);
080            if(enchant != null) {
081
082              meta.addStoredEnchant(enchant, entry.getValue(), true);
083            }
084          } catch(final Exception ignore) {
085            //enchantment couldn't be found.
086          }
087        }
088      }
089    });
090    return item;
091  }
092
093  /**
094   * @param item       the item that we should use to deserialize.
095   * @param serialized the serialized item stack we should use to apply this deserializer to
096   *
097   * @return the updated serialized item.
098   * @since 0.2.0.0
099   */
100  @Override
101  public BukkitItemStack serialize(final ItemStack item, final BukkitItemStack serialized) {
102
103    if(item.hasItemMeta() && item.getItemMeta() instanceof final EnchantmentStorageMeta meta) {
104
105      for(final Map.Entry<Enchantment, Integer> entry : meta.getStoredEnchants().entrySet()) {
106
107        levels.put(BukkitItemPlatform.instance().converter().convert(entry.getKey(), String.class), entry.getValue());
108      }
109    }
110
111    serialized.applyComponent(this);
112    return serialized;
113  }
114
115  /**
116   * Checks if this component applies to the specified item.
117   *
118   * @param item The item to check against.
119   *
120   * @return True if this component applies to the item, false otherwise.
121   * @since 0.2.0.0
122   */
123  @Override
124  public boolean appliesTo(final ItemStack item) {
125
126    return item.hasItemMeta() && item.getItemMeta() instanceof EnchantmentStorageMeta;
127  }
128}