001package net.tnemc.item.bukkitbase.platform.providers;
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.Indyuce.mmoitems.MMOItems;
022import net.Indyuce.mmoitems.api.Type;
023import net.tnemc.item.AbstractItemStack;
024import net.tnemc.item.providers.ItemProvider;
025import org.bukkit.inventory.ItemStack;
026
027/**
028 * MMOItemProvider
029 *
030 * @author creatorfromhell
031 * @since 0.2.0.0
032 */
033public class MMOItemProvider implements ItemProvider<ItemStack> {
034
035  /**
036   * Checks if the given serialized item stack applies to the specified item.
037   *
038   * @param serialized The serialized item stack to check against the item.
039   * @param item       The item to check against.
040   *
041   * @return True if the serialized item stack applies to the item, false otherwise.
042   */
043  @Override
044  public boolean appliesTo(final AbstractItemStack<? extends ItemStack> serialized, final ItemStack item) {
045
046    final Type type = MMOItems.getType(item);
047    if(type == null) {
048      return false;
049    }
050
051    final String id = MMOItems.getID(item);
052    if(id == null) {
053      return false;
054    }
055
056    serialized.setItemProvider(identifier());
057    serialized.setProviderItemID(type.getId() + ":" + id);
058
059    return true;
060  }
061
062  /**
063   * Checks if the provided item stack is similar to the original item stack.
064   *
065   * @param original The original item stack to compare against.
066   * @param compare  The item stack to compare.
067   *
068   * @return True if the two item stacks are similar, otherwise false.
069   */
070  @Override
071  public boolean similar(final AbstractItemStack<? extends ItemStack> original, final ItemStack compare) {
072
073    final String id = MMOItems.getID(compare);
074    if(id == null) {
075      return false;
076    }
077
078    return id.equals(original.providerItemID());
079  }
080
081  /**
082   * Creates a copy of the original item stack with a specified amount.
083   *
084   * @param original The original item stack to copy.
085   * @param amount   The amount for the new item stack.
086   *
087   * @return A new item stack with the specified amount.
088   */
089  @Override
090  public ItemStack locale(final AbstractItemStack<? extends ItemStack> original, final int amount) {
091
092    final String[] splitValue = original.providerItemID().split(":");
093
094    return MMOItems.plugin.getItem(splitValue[0], original.providerItemID());
095  }
096
097  /**
098   * @return the identifier for this check.
099   */
100  @Override
101  public String identifier() {
102
103    return "mmoitem";
104  }
105}