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 io.papermc.paper.datacomponent.DataComponentTypes;
022import net.tnemc.item.component.impl.DamageComponent;
023import net.tnemc.item.paper.PaperItemStack;
024import net.tnemc.item.providers.VersionUtil;
025import org.bukkit.inventory.ItemStack;
026
027import java.util.Optional;
028
029/**
030 * PaperOldDamageComponent
031 *
032 * @author creatorfromhell
033 * @since 0.2.0.0
034 */
035public class PaperDamageComponent extends DamageComponent<PaperItemStack, ItemStack> {
036
037  /**
038   * Represents a component that manages damage information. This component stores and provides
039   * methods for handling damage values.
040   * @since 0.2.0.0
041   */
042  public PaperDamageComponent() {
043
044  }
045
046  /**
047   * Constructs a new DamageComponent with the specified damage amount.
048   *
049   * @param damage the amount of damage for the component
050   * @since 0.2.0.0
051   */
052  public PaperDamageComponent(final int damage) {
053
054    super(damage);
055  }
056
057  /**
058   * @param version the version being used when this check is called.
059   *
060   * @return true if this check is enabled for the version, otherwise false
061   * @since 0.2.0.0
062   */
063  @Override
064  public boolean enabled(final String version) {
065
066    return VersionUtil.isOneTwentyOneFour(version);
067  }
068
069  /**
070   * @param serialized the serialized item stack to use
071   * @param item       the item that we should use to apply this applicator to.
072   *
073   * @return the updated item.
074   * @since 0.2.0.0
075   */
076  @Override
077  public ItemStack apply(final PaperItemStack serialized, final ItemStack item) {
078
079    final Optional<PaperDamageComponent> componentOptional = serialized.component(identifier());
080    if(componentOptional.isEmpty()) {
081      return item;
082    }
083
084    item.setData(DataComponentTypes.DAMAGE, this.damage);
085    return item;
086  }
087
088  /**
089   * @param item       the item that we should use to deserialize.
090   * @param serialized the serialized item stack we should use to apply this deserializer to
091   *
092   * @return the updated serialized item.
093   * @since 0.2.0.0
094   */
095  @Override
096  public PaperItemStack serialize(final ItemStack item, final PaperItemStack serialized) {
097
098    final Integer damageValue = item.getData(DataComponentTypes.DAMAGE);
099    if(damageValue == null) {
100      return serialized;
101    }
102
103    this.damage = damageValue;
104
105    serialized.applyComponent(this);
106    return serialized;
107  }
108
109  /**
110   * Checks if this component applies to the specified item.
111   *
112   * @param item The item to check against.
113   *
114   * @return True if this component applies to the item, false otherwise.
115   * @since 0.2.0.0
116   */
117  @Override
118  public boolean appliesTo(final ItemStack item) {
119
120    return true;
121  }
122}