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