001package net.tnemc.item.bukkit;
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.platform.BukkitItemPlatform;
023import net.tnemc.item.component.SerialComponent;
024import net.tnemc.item.providers.ItemProvider;
025import org.bukkit.Material;
026import org.bukkit.NamespacedKey;
027import org.bukkit.Registry;
028import org.bukkit.inventory.ItemStack;
029
030/**
031 * VanillaProvider
032 *
033 * @author creatorfromhell
034 * @since 0.2.0.0
035 */
036public class VanillaProvider implements ItemProvider<ItemStack> {
037
038  /**
039   * Checks if the given serialized item stack applies to the specified item.
040   *
041   * @param serialized The serialized item stack to check against the item.
042   * @param item       The item to check against.
043   *
044   * @return True if the serialized item stack applies to the item, false otherwise.
045   * @since 0.2.0.0
046   */
047  @Override
048  public boolean appliesTo(final AbstractItemStack<? extends ItemStack> serialized, final ItemStack item) {
049
050    return true;
051  }
052
053  /**
054   * Checks if the provided item stack is similar to the original item stack.
055   *
056   * @param original The original item stack to compare against.
057   * @param compare  The item stack to compare.
058   *
059   * @return True if the two item stacks are similar, otherwise false.
060   * @since 0.2.0.0
061   */
062  @Override
063  public boolean similar(final AbstractItemStack<? extends ItemStack> original, final ItemStack compare) {
064
065    final BukkitItemStack compareStack = new BukkitItemStack().of(compare);
066
067    System.out.println("==== Similar call ====");
068
069    System.out.println("original Components");
070
071    for(final SerialComponent component : original.components().values()) {
072
073      System.out.println("Entry: " + component.identifier());
074    }
075
076    System.out.println("Compare Components");
077
078    for(final SerialComponent component : compareStack.components().values()) {
079
080      System.out.println("Entry: " + component.identifier());
081    }
082
083    System.out.println("==== Similar end ====");
084
085
086    return BukkitItemPlatform.instance().check((BukkitItemStack)original, compareStack);
087  }
088
089  /**
090   * Creates a copy of the original item stack with a specified amount.
091   *
092   * @param original The original item stack to copy.
093   * @param amount   The amount for the new item stack.
094   *
095   * @return A new item stack with the specified amount.
096   * @since 0.2.0.0
097   */
098  @Override
099  public ItemStack locale(final AbstractItemStack<? extends ItemStack> original, final int amount) {
100
101    System.out.println("instanceof: " + (original instanceof BukkitItemStack));
102
103    if(original instanceof final BukkitItemStack bukkit) {
104
105      if(!bukkit.isDirty()) {
106        return bukkit.cacheLocale();
107      }
108
109      System.out.println("Material: " + bukkit.material());
110
111      Material material = null;
112
113      try {
114        final NamespacedKey key = NamespacedKey.fromString(bukkit.material());
115        if(key != null) {
116
117          System.out.println("Key is not null");
118
119          material = Registry.MATERIAL.get(key);
120        }
121      } catch(final NoSuchMethodError ignore) {
122        material = Material.matchMaterial(bukkit.material());
123      }
124
125      if(material == null) {
126
127        System.out.println("Material is null");
128        return null;
129      }
130      ItemStack stack = new ItemStack(material, amount);
131
132      stack = BukkitItemPlatform.instance().apply(bukkit, stack);
133
134      bukkit.updateCache(stack);
135      bukkit.resetDirty();
136
137      return stack;
138    }
139    return null;
140  }
141
142  /**
143   * @return the identifier for this check.
144   * @since 0.2.0.0
145   */
146  @Override
147  public String identifier() {
148
149    return "vanilla";
150  }
151}