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.AbstractItemStack;
022import net.tnemc.item.bukkit.BukkitItemStack;
023import net.tnemc.item.bukkit.platform.BukkitItemPlatform;
024import net.tnemc.item.component.SerialComponent;
025import net.tnemc.item.component.impl.AttributeModifiersComponent;
026import org.bukkit.NamespacedKey;
027import org.bukkit.attribute.Attribute;
028import org.bukkit.attribute.AttributeModifier;
029import org.bukkit.inventory.EquipmentSlotGroup;
030import org.bukkit.inventory.ItemStack;
031import org.bukkit.inventory.meta.ItemMeta;
032
033import java.util.List;
034import java.util.Optional;
035
036/**
037 * BukkitAttributeModifiersComponent
038 *
039 * @author creatorfromhell
040 * @since 0.2.0.0
041 */
042public class BukkitAttributeModifiersComponent extends AttributeModifiersComponent<BukkitItemStack, ItemStack> {
043
044  /**
045   * Constructor for AttributeModifiersComponent. Initializes an empty list of AttributeModifiers.
046   * @since 0.2.0.0
047   */
048  public BukkitAttributeModifiersComponent() {
049
050  }
051
052  /**
053   * Constructor for AttributeModifiersComponent. Initializes the component with a list of
054   * AttributeModifiers and a boolean flag to show in tooltip.
055   *
056   * @param modifiers The list of AttributeModifiers to associate with this component.
057   * @since 0.2.0.0
058   */
059  public BukkitAttributeModifiersComponent(final List<net.tnemc.item.component.helper.AttributeModifier> modifiers) {
060
061    super(modifiers);
062  }
063
064  /**
065   * @param version the version being used when this check is called.
066   *
067   * @return true if this check is enabled for the version, otherwise false
068   * @since 0.2.0.0
069   */
070  @Override
071  public boolean enabled(final String version) {
072
073    return true;
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 BukkitItemStack serialized, final ItemStack item) {
085
086    final ItemMeta meta = item.getItemMeta();
087    final Optional<AttributeModifiersComponent<AbstractItemStack<ItemStack>, ItemStack>> componentOptional = serialized.attributeModifiers();
088    if(meta != null && componentOptional.isPresent()) {
089
090      for(final net.tnemc.item.component.helper.AttributeModifier attribute : componentOptional.get().modifiers()) {
091
092        final AttributeModifier.Operation operation = BukkitItemPlatform.instance().converter().convert(attribute.getOperation(), AttributeModifier.Operation.class);
093        final EquipmentSlotGroup slot = BukkitItemPlatform.instance().converter().convert(attribute.getSlot(), EquipmentSlotGroup.class);
094        final AttributeModifier attr = new AttributeModifier(NamespacedKey.fromString(attribute.getType()),
095                                                             attribute.getAmount(),
096                                                             operation,
097                                                             slot);
098
099        meta.addAttributeModifier(Attribute.valueOf(attribute.getId()), attr);
100      }
101    }
102
103    return item;
104  }
105
106  /**
107   * @param item       the item that we should use to deserialize.
108   * @param serialized the serialized item stack we should use to apply this deserializer to
109   *
110   * @return the updated serialized item.
111   * @since 0.2.0.0
112   */
113  @Override
114  public BukkitItemStack serialize(final ItemStack item, final BukkitItemStack serialized) {
115
116    return null;
117  }
118
119  /**
120   * Checks if this component applies to the specified item.
121   *
122   * @param item The item to check against.
123   *
124   * @return True if this component applies to the item, false otherwise.
125   * @since 0.2.0.0
126   */
127  @Override
128  public boolean appliesTo(final ItemStack item) {
129
130    return true;
131  }
132}