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.component.impl.TooltipStyleComponent;
023import net.tnemc.item.providers.VersionUtil;
024import org.bukkit.NamespacedKey;
025import org.bukkit.inventory.ItemStack;
026import org.bukkit.inventory.meta.ItemMeta;
027
028import java.util.Optional;
029
030/**
031 * BukkitTooltipStyleComponent
032 *
033 * @author creatorfromhell
034 * @since 0.2.0.0
035 */
036public class BukkitTooltipStyleComponent extends TooltipStyleComponent<BukkitItemStack, ItemStack> {
037
038  public BukkitTooltipStyleComponent() {
039
040  }
041
042  public BukkitTooltipStyleComponent(final String style) {
043
044    super(style);
045  }
046
047  /**
048   * @param version the version being used when this check is called.
049   *
050   * @return true if this check is enabled for the version, otherwise false
051   * @since 0.2.0.0
052   */
053  @Override
054  public boolean enabled(final String version) {
055
056    return VersionUtil.isOneTwentyOneTwo(version);
057  }
058
059  /**
060   * @param serialized the serialized item stack to use
061   * @param item       the item that we should use to apply this applicator to.
062   *
063   * @return the updated item.
064   * @since 0.2.0.0
065   */
066  @Override
067  public ItemStack apply(final BukkitItemStack serialized, final ItemStack item) {
068
069    final Optional<BukkitTooltipStyleComponent> componentOptional = serialized.component(identifier());
070
071    if(componentOptional.isPresent()) {
072
073      final ItemMeta meta = item.getItemMeta();
074      final String toolTip = componentOptional.get().style;
075      if(meta != null && !toolTip.isEmpty()) {
076
077        meta.setTooltipStyle(NamespacedKey.fromString(toolTip));
078        item.setItemMeta(meta);
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 BukkitItemStack serialize(final ItemStack item, final BukkitItemStack serialized) {
093
094    final ItemMeta meta = item.getItemMeta();
095    if(meta != null && meta.hasTooltipStyle() && meta.getTooltipStyle() != null) {
096
097      style = meta.getTooltipStyle().toString();
098    }
099    serialized.applyComponent(this);
100    return serialized;
101  }
102
103  /**
104   * Checks if this component applies to the specified item.
105   *
106   * @param item The item to check against.
107   *
108   * @return True if this component applies to the item, false otherwise.
109   * @since 0.2.0.0
110   */
111  @Override
112  public boolean appliesTo(final ItemStack item) {
113
114    return item.getItemMeta() != null;
115  }
116}