001package net.tnemc.item.paper; 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.paper.platform.PaperItemPlatform; 023import net.tnemc.item.providers.ItemProvider; 024import org.bukkit.Material; 025import org.bukkit.NamespacedKey; 026import org.bukkit.Registry; 027import org.bukkit.inventory.ItemStack; 028 029/** 030 * VanillaProvider 031 * 032 * @author creatorfromhell 033 * @since 0.2.0.0 034 */ 035public class VanillaProvider implements ItemProvider<ItemStack> { 036 037 /** 038 * Checks if the given serialized item stack applies to the specified item. 039 * 040 * @param serialized The serialized item stack to check against the item. 041 * @param item The item to check against. 042 * 043 * @return True if the serialized item stack applies to the item, false otherwise. 044 * @since 0.2.0.0 045 */ 046 @Override 047 public boolean appliesTo(final AbstractItemStack<? extends ItemStack> serialized, final ItemStack item) { 048 049 return true; 050 } 051 052 /** 053 * Checks if the provided item stack is similar to the original item stack. 054 * 055 * @param original The original item stack to compare against. 056 * @param compare The item stack to compare. 057 * 058 * @return True if the two item stacks are similar, otherwise false. 059 * @since 0.2.0.0 060 */ 061 @Override 062 public boolean similar(final AbstractItemStack<? extends ItemStack> original, final ItemStack compare) { 063 064 return PaperItemPlatform.instance().check((PaperItemStack)original, new PaperItemStack().of(compare)); 065 } 066 067 /** 068 * Creates a copy of the original item stack with a specified amount. 069 * 070 * @param original The original item stack to copy. 071 * @param amount The amount for the new item stack. 072 * 073 * @return A new item stack with the specified amount. 074 * @since 0.2.0.0 075 */ 076 @Override 077 public ItemStack locale(final AbstractItemStack<? extends ItemStack> original, final int amount) { 078 079 if(original instanceof final PaperItemStack bukkit) { 080 081 if(!bukkit.isDirty()) { 082 return bukkit.cacheLocale(); 083 } 084 085 Material material = null; 086 087 try { 088 final NamespacedKey key = NamespacedKey.fromString(bukkit.material()); 089 if(key != null) { 090 091 material = Registry.MATERIAL.get(key); 092 } 093 } catch(final Exception ignore) { 094 material = Material.matchMaterial(bukkit.material()); 095 } 096 097 if(material == null) { 098 099 return null; 100 } 101 ItemStack stack = new ItemStack(material, amount); 102 103 stack = PaperItemPlatform.instance().apply(bukkit, stack); 104 105 bukkit.updateCache(stack); 106 bukkit.resetDirty(); 107 108 return stack; 109 } 110 return null; 111 } 112 113 /** 114 * @return the identifier for this check. 115 * @since 0.2.0.0 116 */ 117 @Override 118 public String identifier() { 119 120 return "vanilla"; 121 } 122}