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