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.impl.BundleComponent;
025import net.tnemc.item.providers.VersionUtil;
026import org.bukkit.Material;
027import org.bukkit.inventory.ItemStack;
028import org.bukkit.inventory.meta.BundleMeta;
029
030import java.util.Map;
031import java.util.Optional;
032
033/**
034 * BukkitBundleComponent
035 *
036 * @author creatorfromhell
037 * @since 0.2.0.0
038 */
039public class BukkitBundleComponent extends BundleComponent<BukkitItemStack, ItemStack> {
040
041  public BukkitBundleComponent() {
042
043  }
044
045  public BukkitBundleComponent(final Map<Integer, AbstractItemStack<ItemStack>> items) {
046
047    super(items);
048  }
049
050  /**
051   * @param version the version being used when this check is called.
052   *
053   * @return true if this check is enabled for the version, otherwise false
054   * @since 0.2.0.0
055   */
056  @Override
057  public boolean enabled(final String version) {
058
059    return VersionUtil.isOneSeventeen(version);
060  }
061
062  /**
063   * @param serialized the serialized item stack to use
064   * @param item       the item that we should use to apply this applicator to.
065   *
066   * @return the updated item.
067   * @since 0.2.0.0
068   */
069  @Override
070  public ItemStack apply(final BukkitItemStack serialized, final ItemStack item) {
071
072    final Optional<BukkitBundleComponent> componentOptional = serialized.component(identifier());
073    componentOptional.ifPresent(component->{
074
075      if(item.hasItemMeta() && item.getItemMeta() instanceof final BundleMeta meta) {
076
077        componentOptional.get().items.forEach((slot, stack)->meta.addItem(stack.provider().locale(serialized)));
078
079        item.setItemMeta(meta);
080      }
081    });
082    return item;
083  }
084
085  /**
086   * @param item       the item that we should use to deserialize.
087   * @param serialized the serialized item stack we should use to apply this deserializer to
088   *
089   * @return the updated serialized item.
090   * @since 0.2.0.0
091   */
092  @Override
093  public BukkitItemStack serialize(final ItemStack item, final BukkitItemStack serialized) {
094
095    if(item.hasItemMeta() && item.getItemMeta() instanceof final BundleMeta meta) {
096
097      int i = 0;
098      for(final ItemStack stack : meta.getItems()) {
099
100        if(stack == null) {
101          continue;
102        }
103
104        if(stack.getType().equals(Material.AIR)) {
105          continue;
106        }
107
108        final BukkitItemStack containerSerial = new BukkitItemStack().of(stack);
109        BukkitItemPlatform.instance().providerApplies(containerSerial, stack);
110        items.put(i, containerSerial);
111        i++;
112      }
113    }
114
115    serialized.applyComponent(this);
116    return serialized;
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 item.hasItemMeta() && item.getItemMeta() instanceof BundleMeta;
131  }
132}