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