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 com.destroystokyo.paper.profile.ProfileProperty;
022import io.papermc.paper.datacomponent.DataComponentTypes;
023import io.papermc.paper.datacomponent.item.ResolvableProfile;
024import net.tnemc.item.component.impl.ProfileComponent;
025import net.tnemc.item.paper.PaperItemStack;
026import net.tnemc.item.providers.SkullProfile;
027import net.tnemc.item.providers.VersionUtil;
028import org.bukkit.inventory.ItemStack;
029import org.bukkit.inventory.meta.SkullMeta;
030
031import java.util.Optional;
032
033/**
034 * PaperOldProfileComponent
035 *
036 * @author creatorfromhell
037 * @since 0.2.0.0
038 */
039public class PaperProfileComponent extends ProfileComponent<PaperItemStack, ItemStack> {
040
041  public PaperProfileComponent() {
042
043  }
044
045  public PaperProfileComponent(final SkullProfile profile) {
046
047    super(profile);
048  }
049
050  /**
051   * @param version the version being used when this check is called.
052   *
053   * @return true if this check is enabled for the version, otherwise false
054   * @since 0.2.0.0
055   */
056  @Override
057  public boolean enabled(final String version) {
058
059    return VersionUtil.isOneTwentyOneFour(version);
060  }
061
062  /**
063   * Checks if this component applies to the specified item.
064   *
065   * @param item The item to check against.
066   *
067   * @return True if this component applies to the item, false otherwise.
068   * @since 0.2.0.0
069   */
070  @Override
071  public boolean appliesTo(final ItemStack item) {
072
073    return item.getItemMeta() instanceof SkullMeta;
074  }
075
076  /**
077   * @param serialized the serialized item stack to use
078   * @param item       the item that we should use to apply this applicator to.
079   *
080   * @return the updated item.
081   * @since 0.2.0.0
082   */
083  @Override
084  public ItemStack apply(final PaperItemStack serialized, final ItemStack item) {
085
086    final Optional<PaperProfileComponent> componentOptional = serialized.component(identifier());
087    if(componentOptional.isEmpty()) {
088      return item;
089    }
090
091    final SkullProfile profile = componentOptional.get().profile;
092    if(profile.getTexture() == null) {
093      return item;
094    }
095
096    final ResolvableProfile.Builder builder = ResolvableProfile.resolvableProfile();
097
098    builder.name(profile.getName());
099    builder.uuid(profile.getUuid());
100    builder.addProperty(new ProfileProperty("textures", profile.getTexture()));
101
102    item.setData(DataComponentTypes.PROFILE, builder);
103
104    return item;
105  }
106
107  /**
108   * @param item       the item that we should use to deserialize.
109   * @param serialized the serialized item stack we should use to apply this deserializer to
110   *
111   * @return the updated serialized item.
112   * @since 0.2.0.0
113   */
114  @Override
115  public PaperItemStack serialize(final ItemStack item, final PaperItemStack serialized) {
116
117    final ResolvableProfile resolvableProfile = item.getData(DataComponentTypes.PROFILE);
118    if(resolvableProfile == null) {
119      return serialized;
120    }
121
122    final SkullProfile skull = new SkullProfile();
123    skull.setUuid(resolvableProfile.uuid());
124    skull.setName(resolvableProfile.name());
125
126    for(final ProfileProperty property : resolvableProfile.properties()) {
127      if(property.getName().equalsIgnoreCase("textures")) {
128
129        skull.setTexture(property.getValue());
130      }
131    }
132
133    serialized.applyComponent(this);
134    return serialized;
135  }
136}