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.kyori.adventure.text.Component;
022import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
023import net.tnemc.item.bukkit.BukkitItemStack;
024import net.tnemc.item.component.impl.LoreComponent;
025import org.bukkit.inventory.ItemStack;
026import org.bukkit.inventory.meta.ItemMeta;
027
028import java.util.LinkedList;
029import java.util.List;
030import java.util.Optional;
031
032/**
033 * BukkitLoreComponent
034 *
035 * @author creatorfromhell
036 * @since 0.2.0.0
037 */
038public class BukkitLoreComponent extends LoreComponent<BukkitItemStack, ItemStack> {
039
040  public BukkitLoreComponent() {
041
042  }
043
044  public BukkitLoreComponent(final List<Component> lore) {
045
046    super(lore);
047  }
048
049  public BukkitLoreComponent(final Component... lore) {
050
051    super(lore);
052  }
053
054  /**
055   * @param version the version being used when this check is called.
056   *
057   * @return true if this check is enabled for the version, otherwise false
058   * @since 0.2.0.0
059   */
060  @Override
061  public boolean enabled(final String version) {
062
063    return true;
064  }
065
066  /**
067   * @param serialized the serialized item stack to use
068   * @param item       the item that we should use to apply this applicator to.
069   *
070   * @return the updated item.
071   * @since 0.2.0.0
072   */
073  @Override
074  public ItemStack apply(final BukkitItemStack serialized, final ItemStack item) {
075
076    final Optional<BukkitLoreComponent> componentOptional = serialized.component(identifier());
077    componentOptional.ifPresent(component->{
078
079      final ItemMeta meta = item.getItemMeta();
080      if(meta != null) {
081
082        final LinkedList<String> newLore = new LinkedList<>();
083        for(final Component comp : componentOptional.get().lore) {
084
085          newLore.add(LegacyComponentSerializer.legacySection().serialize(comp));
086        }
087        meta.setLore(newLore);
088
089        item.setItemMeta(meta);
090      }
091    });
092    return item;
093  }
094
095  /**
096   * @param item       the item that we should use to deserialize.
097   * @param serialized the serialized item stack we should use to apply this deserializer to
098   *
099   * @return the updated serialized item.
100   * @since 0.2.0.0
101   */
102  @Override
103  public BukkitItemStack serialize(final ItemStack item, final BukkitItemStack serialized) {
104
105    final ItemMeta meta = item.getItemMeta();
106    if(meta != null && meta.getLore() != null) {
107
108      lore.clear();
109
110      for(final String str : meta.getLore()) {
111
112        lore.add(LegacyComponentSerializer.legacySection().deserialize(str));
113      }
114    }
115
116    serialized.applyComponent(this);
117    return serialized;
118  }
119
120  /**
121   * Checks if this component applies to the specified item.
122   *
123   * @param item The item to check against.
124   *
125   * @return True if this component applies to the item, false otherwise.
126   * @since 0.2.0.0
127   */
128  @Override
129  public boolean appliesTo(final ItemStack item) {
130
131    return item.hasItemMeta() && item.getItemMeta() != null;
132  }
133}