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.tnemc.item.component.impl.ProfileComponent;
022import net.tnemc.item.paper.PaperItemStack;
023import net.tnemc.item.providers.SkullProfile;
024import org.bukkit.Bukkit;
025import org.bukkit.inventory.ItemStack;
026import org.bukkit.inventory.meta.ItemMeta;
027import org.bukkit.inventory.meta.SkullMeta;
028
029import java.util.Optional;
030
031/**
032 * PaperOldProfileComponent
033 *
034 * @author creatorfromhell
035 * @since 0.2.0.0
036 */
037public class PaperOldProfileComponent extends ProfileComponent<PaperItemStack, ItemStack> {
038
039  /**
040   * @param version the version being used when this check is called.
041   *
042   * @return true if this check is enabled for the version, otherwise false
043   * @since 0.2.0.0
044   */
045  @Override
046  public boolean enabled(final String version) {
047
048    return true;
049  }
050
051  /**
052   * @param serialized the serialized item stack to use
053   * @param item       the item that we should use to apply this applicator to.
054   *
055   * @return the updated item.
056   * @since 0.2.0.0
057   */
058  @Override
059  public ItemStack apply(final PaperItemStack serialized, final ItemStack item) {
060
061    final ItemMeta meta = item.getItemMeta();
062    final Optional<PaperOldProfileComponent> componentOptional = serialized.component(identifier());
063    if(meta instanceof final SkullMeta skullMeta && componentOptional.isPresent()) {
064
065      if(profile != null) {
066
067        try {
068
069          if(profile.getUuid() != null) {
070            skullMeta.setOwningPlayer(Bukkit.getOfflinePlayer(profile.getUuid()));
071          }
072
073        } catch(final Exception ignore) {
074
075          skullMeta.setOwner(profile.getName());
076        }
077      }
078      item.setItemMeta(meta);
079    }
080    return item;
081  }
082
083  /**
084   * @param item       the item that we should use to deserialize.
085   * @param serialized the serialized item stack we should use to apply this deserializer to
086   *
087   * @return the updated serialized item.
088   * @since 0.2.0.0
089   */
090  @Override
091  public PaperItemStack serialize(final ItemStack item, final PaperItemStack serialized) {
092
093    if(item.getItemMeta() instanceof final SkullMeta meta) {
094
095      profile = new SkullProfile();
096
097      try {
098
099        if(meta.getOwningPlayer() != null) {
100
101          profile.setUuid(meta.getOwningPlayer().getUniqueId());
102        }
103
104      } catch(final Exception ignore) {
105
106        profile.setName(meta.getOwner());
107      }
108      serialized.applyComponent(this);
109    }
110    return serialized;
111  }
112
113  /**
114   * Checks if this component applies to the specified item.
115   *
116   * @param item The item to check against.
117   *
118   * @return True if this component applies to the item, false otherwise.
119   * @since 0.2.0.0
120   */
121  @Override
122  public boolean appliesTo(final ItemStack item) {
123
124    return item.getItemMeta() instanceof SkullMeta;
125  }
126}