001package net.tnemc.item.bukkitbase.platform.check;
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.platform.check.LocaleItemCheck;
022import org.bukkit.NamespacedKey;
023import org.bukkit.inventory.ItemStack;
024import org.bukkit.persistence.PersistentDataType;
025
026import java.util.Objects;
027
028/**
029 * PDCCheck
030 *
031 * @author creatorfromhell
032 * @since 0.1.7.7
033 */
034public abstract class PDCCheck implements LocaleItemCheck<ItemStack> {
035
036  protected final NamespacedKey key;
037  protected final PersistentDataType<?, ?> type;
038
039  public PDCCheck(final NamespacedKey key, final PersistentDataType<?, ?> type) {
040
041    this.key = key;
042    this.type = type;
043  }
044
045  /**
046   * @param original the original stack
047   * @param check    the stack to use for the check
048   *
049   * @return True if the check passes, otherwise false.
050   */
051  @Override
052  public boolean check(final ItemStack original, final ItemStack check) {
053
054    if(original.getItemMeta() != null && original.getItemMeta().getPersistentDataContainer().has(key, type)) {
055      if(check.getItemMeta() == null || !check.getItemMeta().getPersistentDataContainer().has(key, type)) {
056        return false;
057      }
058      return Objects.equals(check.getItemMeta().getPersistentDataContainer().get(key, type), original.getItemMeta().getPersistentDataContainer().get(key, type));
059    }
060
061    if(check.getItemMeta() != null && check.getItemMeta().getPersistentDataContainer().has(key, type)) {
062      return false;
063    }
064    return true;
065  }
066}