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.component.impl.UnbreakableComponent;
022import net.tnemc.item.paper.PaperItemStack;
023import net.tnemc.item.providers.VersionUtil;
024import org.bukkit.inventory.ItemStack;
025import org.bukkit.inventory.meta.ItemMeta;
026
027import java.util.Optional;
028
029/**
030 * PaperOldUnbreakableComponent
031 *
032 * @author creatorfromhell
033 * @since 0.2.0.0
034 */
035public class PaperOldUnbreakableComponent extends UnbreakableComponent<PaperItemStack, ItemStack> {
036
037  /**
038   * @param version the version being used when this check is called.
039   *
040   * @return true if this check is enabled for the version, otherwise false
041   * @since 0.2.0.0
042   */
043  @Override
044  public boolean enabled(final String version) {
045
046    //TODO: Look up version
047    return VersionUtil.isOneThirteen(version);
048  }
049
050  /**
051   * @param serialized the serialized item stack to use
052   * @param item       the item that we should use to apply this applicator to.
053   *
054   * @return the updated item.
055   * @since 0.2.0.0
056   */
057  @Override
058  public ItemStack apply(final PaperItemStack serialized, final ItemStack item) {
059
060    final Optional<PaperOldUnbreakableComponent> componentOptional = serialized.component(identifier());
061
062    if(componentOptional.isPresent()) {
063
064      final ItemMeta meta = item.getItemMeta();
065      if(meta != null) {
066
067        meta.setUnbreakable(true);
068        item.setItemMeta(meta);
069      }
070    }
071    return item;
072  }
073
074  /**
075   * @param item       the item that we should use to deserialize.
076   * @param serialized the serialized item stack we should use to apply this deserializer to
077   *
078   * @return the updated serialized item.
079   * @since 0.2.0.0
080   */
081  @Override
082  public PaperItemStack serialize(final ItemStack item, final PaperItemStack serialized) {
083
084    final ItemMeta meta = item.getItemMeta();
085    if(meta != null && meta.isUnbreakable()) {
086
087      serialized.applyComponent(this);
088    }
089    return serialized;
090  }
091
092  /**
093   * Checks if this component applies to the specified item.
094   *
095   * @param item The item to check against.
096   *
097   * @return True if this component applies to the item, false otherwise.
098   * @since 0.2.0.0
099   */
100  @Override
101  public boolean appliesTo(final ItemStack item) {
102
103    return item.getItemMeta() != null;
104  }
105}