001package net.tnemc.item.paper.platform.impl.old; 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.component.impl.EnchantmentsComponent; 022import net.tnemc.item.paper.PaperItemStack; 023import net.tnemc.item.paper.platform.PaperItemPlatform; 024import org.bukkit.enchantments.Enchantment; 025import org.bukkit.inventory.ItemStack; 026 027import java.util.Map; 028import java.util.Optional; 029 030/** 031 * PaperOldEnchantmentsComponent 032 * 033 * @author creatorfromhell 034 * @since 0.2.0.0 035 */ 036public class PaperOldEnchantmentsComponent extends EnchantmentsComponent<PaperItemStack, ItemStack> { 037 038 /** 039 * @param version the version being used when this check is called. 040 * 041 * @return true if this check is enabled for the version, otherwise false 042 * @since 0.2.0.0 043 */ 044 @Override 045 public boolean enabled(final String version) { 046 047 return true; 048 } 049 050 /** 051 * @param serialized the serialized item stack to use 052 * @param item the item that we should use to apply this applicator to. 053 * 054 * @return the updated item. 055 * @since 0.2.0.0 056 */ 057 @Override 058 public ItemStack apply(final PaperItemStack serialized, final ItemStack item) { 059 060 final Optional<PaperOldEnchantmentsComponent> componentOptional = serialized.component(identifier()); 061 componentOptional.ifPresent(component->{ 062 063 for(final Map.Entry<String, Integer> entry : componentOptional.get().levels.entrySet()) { 064 065 try { 066 067 final Enchantment enchant = PaperItemPlatform.instance().converter().convert(entry.getKey(), Enchantment.class); 068 if(enchant != null) { 069 070 item.addUnsafeEnchantment(enchant, entry.getValue()); 071 } 072 } catch(final Exception ignore) { 073 //enchantment couldn't be found. 074 } 075 } 076 }); 077 return item; 078 } 079 080 /** 081 * @param item the item that we should use to deserialize. 082 * @param serialized the serialized item stack we should use to apply this deserializer to 083 * 084 * @return the updated serialized item. 085 * @since 0.2.0.0 086 */ 087 @Override 088 public PaperItemStack serialize(final ItemStack item, final PaperItemStack serialized) { 089 090 for(final Map.Entry<Enchantment, Integer> entry : item.getEnchantments().entrySet()) { 091 092 levels.put(PaperItemPlatform.instance().converter().convert(entry.getKey(), String.class), entry.getValue()); 093 } 094 095 serialized.applyComponent(this); 096 return serialized; 097 } 098 099 /** 100 * Checks if this component applies to the specified item. 101 * 102 * @param item The item to check against. 103 * 104 * @return True if this component applies to the item, false otherwise. 105 * @since 0.2.0.0 106 */ 107 @Override 108 public boolean appliesTo(final ItemStack item) { 109 110 return true; 111 } 112}