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.AbstractItemStack;
022import net.tnemc.item.component.impl.AttributeModifiersComponent;
023import net.tnemc.item.paper.PaperItemStack;
024import net.tnemc.item.paper.platform.PaperItemPlatform;
025import org.bukkit.NamespacedKey;
026import org.bukkit.attribute.Attribute;
027import org.bukkit.attribute.AttributeModifier;
028import org.bukkit.inventory.EquipmentSlotGroup;
029import org.bukkit.inventory.ItemStack;
030import org.bukkit.inventory.meta.ItemMeta;
031
032import java.util.Optional;
033
034/**
035 * PaperOldAttributeModifiersComponent
036 *
037 * @author creatorfromhell
038 * @since 0.2.0.0
039 */
040public class PaperOldAttributeModifiersComponent extends AttributeModifiersComponent<PaperItemStack, ItemStack> {
041
042  /**
043   * @param version the version being used when this check is called.
044   *
045   * @return true if this check is enabled for the version, otherwise false
046   * @since 0.2.0.0
047   */
048  @Override
049  public boolean enabled(final String version) {
050
051    return true;
052  }
053
054  /**
055   * @param serialized the serialized item stack to use
056   * @param item       the item that we should use to apply this applicator to.
057   *
058   * @return the updated item.
059   * @since 0.2.0.0
060   */
061  @Override
062  public ItemStack apply(final PaperItemStack serialized, final ItemStack item) {
063
064    final ItemMeta meta = item.getItemMeta();
065    final Optional<AttributeModifiersComponent<AbstractItemStack<ItemStack>, ItemStack>> componentOptional = serialized.attributeModifiers();
066    if(meta != null && componentOptional.isPresent()) {
067
068      for(final net.tnemc.item.component.helper.AttributeModifier attribute : componentOptional.get().modifiers()) {
069
070        final AttributeModifier.Operation operation = PaperItemPlatform.instance().converter().convert(attribute.getOperation(), AttributeModifier.Operation.class);
071        final EquipmentSlotGroup slot = PaperItemPlatform.instance().converter().convert(attribute.getSlot(), EquipmentSlotGroup.class);
072        final AttributeModifier attr = new AttributeModifier(NamespacedKey.fromString(attribute.getType()),
073                                                             attribute.getAmount(),
074                                                             operation,
075                                                             slot);
076
077        meta.addAttributeModifier(Attribute.valueOf(attribute.getId()), attr);
078      }
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 PaperItemStack serialize(final ItemStack item, final PaperItemStack serialized) {
093
094    return null;
095  }
096
097  /**
098   * Checks if this component applies to the specified item.
099   *
100   * @param item The item to check against.
101   *
102   * @return True if this component applies to the item, false otherwise.
103   * @since 0.2.0.0
104   */
105  @Override
106  public boolean appliesTo(final ItemStack item) {
107
108    return true;
109  }
110}