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