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.component.impl.ContainerComponent;
024import net.tnemc.item.providers.VersionUtil;
025import org.bukkit.Material;
026import org.bukkit.inventory.ItemStack;
027import org.bukkit.inventory.meta.CrossbowMeta;
028
029import java.util.List;
030import java.util.Map;
031import java.util.Optional;
032
033/**
034 * BukkitChargedProjectilesComponent
035 *
036 * @author creatorfromhell
037 * @since 0.2.0.0
038 */
039public class BukkitChargedProjectilesComponent extends ContainerComponent<BukkitItemStack, ItemStack> {
040
041  public BukkitChargedProjectilesComponent() {
042
043  }
044
045  public BukkitChargedProjectilesComponent(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.isOneFourteen(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<BukkitChargedProjectilesComponent> componentOptional = serialized.component(identifier());
073    componentOptional.ifPresent(component->{
074
075      if(item.hasItemMeta() && item.getItemMeta() instanceof final CrossbowMeta meta) {
076
077        componentOptional.get().items.forEach((slot, stack)->meta.addChargedProjectile(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 CrossbowMeta meta) {
096
097      final List<ItemStack> projectiles = meta.getChargedProjectiles();
098      for(int i = 0; i < projectiles.size(); i++) {
099
100        final ItemStack stack = projectiles.get(i);
101
102        if(stack != null && !stack.getType().equals(Material.AIR)) {
103
104          items.put(i, new BukkitItemStack().of(stack));
105        }
106      }
107    }
108
109    serialized.applyComponent(this);
110    return serialized;
111  }
112
113  /**
114   * Checks if this component applies to the specified item.
115   *
116   * @param item The item to check against.
117   *
118   * @return True if this component applies to the item, false otherwise.
119   * @since 0.2.0.0
120   */
121  @Override
122  public boolean appliesTo(final ItemStack item) {
123
124    return item.hasItemMeta() && item.getItemMeta() instanceof final CrossbowMeta meta;
125  }
126}