001package net.tnemc.item.paper.platform.impl.modern;
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 io.papermc.paper.datacomponent.DataComponentTypes;
022import io.papermc.paper.datacomponent.item.ItemLore;
023import net.kyori.adventure.text.Component;
024import net.tnemc.item.component.impl.LoreComponent;
025import net.tnemc.item.paper.PaperItemStack;
026import net.tnemc.item.providers.VersionUtil;
027import org.bukkit.inventory.ItemStack;
028
029import java.util.List;
030import java.util.Optional;
031
032/**
033 * PaperOldLoreComponent
034 *
035 * @author creatorfromhell
036 * @since 0.2.0.0
037 */
038public class PaperLoreComponent extends LoreComponent<PaperItemStack, ItemStack> {
039
040  public PaperLoreComponent() {
041
042  }
043
044  public PaperLoreComponent(final List<Component> lore) {
045
046    super(lore);
047  }
048
049  public PaperLoreComponent(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 VersionUtil.isOneTwentyOneFour(version);
064  }
065
066  /**
067   * Checks if this component applies to the specified item.
068   *
069   * @param item The item to check against.
070   *
071   * @return True if this component applies to the item, false otherwise.
072   * @since 0.2.0.0
073   */
074  @Override
075  public boolean appliesTo(final ItemStack item) {
076
077    return item.hasItemMeta() && item.getItemMeta() != null;
078  }
079
080  /**
081   * @param serialized the serialized item stack to use
082   * @param item       the item that we should use to apply this applicator to.
083   *
084   * @return the updated item.
085   * @since 0.2.0.0
086   */
087  @Override
088  public ItemStack apply(final PaperItemStack serialized, final ItemStack item) {
089
090    final Optional<PaperLoreComponent> componentOptional = serialized.component(identifier());
091    if(componentOptional.isEmpty()) {
092      return item;
093    }
094
095    final ItemLore.Builder builder = ItemLore.lore().lines(componentOptional.get().lore);
096
097    item.setData(DataComponentTypes.LORE, builder);
098
099    return item;
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    final ItemLore itemLore = item.getData(DataComponentTypes.LORE);
113    if(itemLore == null) {
114      return serialized;
115    }
116
117    this.lore.addAll(itemLore.lines());
118
119    serialized.applyComponent(this);
120    return serialized;
121  }
122}