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.MaxStackSizeComponent;
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 * BukkitMaxStackSizeComponent
031 *
032 * @author creatorfromhell
033 * @since 0.2.0.0
034 */
035public class BukkitMaxStackSizeComponent extends MaxStackSizeComponent<BukkitItemStack, ItemStack> {
036
037  public BukkitMaxStackSizeComponent() {
038
039  }
040
041  public BukkitMaxStackSizeComponent(final int maxStackSize) {
042
043    super(maxStackSize);
044  }
045
046  /**
047   * @param version the version being used when this check is called.
048   *
049   * @return true if this check is enabled for the version, otherwise false
050   * @since 0.2.0.0
051   */
052  @Override
053  public boolean enabled(final String version) {
054
055    return VersionUtil.isOneTwentyOne(version);
056  }
057
058  /**
059   * @param serialized the serialized item stack to use
060   * @param item       the item that we should use to apply this applicator to.
061   *
062   * @return the updated item.
063   * @since 0.2.0.0
064   */
065  @Override
066  public ItemStack apply(final BukkitItemStack serialized, final ItemStack item) {
067
068    final Optional<BukkitMaxStackSizeComponent> componentOptional = serialized.component(identifier());
069
070    if(componentOptional.isPresent()) {
071
072      final ItemMeta meta = item.getItemMeta();
073      if(meta != null) {
074
075        meta.setMaxStackSize(componentOptional.get().maxStackSize);
076        item.setItemMeta(meta);
077      }
078    }
079    return item;
080  }
081
082  /**
083   * @param item       the item that we should use to deserialize.
084   * @param serialized the serialized item stack we should use to apply this deserializer to
085   *
086   * @return the updated serialized item.
087   * @since 0.2.0.0
088   */
089  @Override
090  public BukkitItemStack serialize(final ItemStack item, final BukkitItemStack serialized) {
091
092    final ItemMeta meta = item.getItemMeta();
093    if(meta != null && item.getItemMeta().hasMaxStackSize()) {
094
095      this.maxStackSize = meta.getMaxStackSize();
096    }
097
098    serialized.applyComponent(this);
099    return serialized;
100  }
101
102  /**
103   * Checks if this component applies to the specified item.
104   *
105   * @param item The item to check against.
106   *
107   * @return True if this component applies to the item, false otherwise.
108   * @since 0.2.0.0
109   */
110  @Override
111  public boolean appliesTo(final ItemStack item) {
112
113    return item.getItemMeta() != null && item.getItemMeta().hasMaxStackSize();
114  }
115}