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