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.component.impl.ModelDataComponent; 023import net.tnemc.item.providers.VersionUtil; 024import org.bukkit.Color; 025import org.bukkit.inventory.ItemStack; 026import org.bukkit.inventory.meta.ItemMeta; 027import org.bukkit.inventory.meta.components.CustomModelDataComponent; 028 029import java.util.ArrayList; 030import java.util.List; 031import java.util.Optional; 032 033/** 034 * BukkitModelData 035 * 036 * @author creatorfromhell 037 * @since 0.2.0.0 038 */ 039public class BukkitModelDataComponent extends ModelDataComponent<BukkitItemStack, ItemStack> { 040 041 public BukkitModelDataComponent() { 042 043 } 044 045 public BukkitModelDataComponent(final List<String> colours, final List<Float> floats, final List<Boolean> flags, final List<String> strings) { 046 047 super(colours, floats, flags, strings); 048 } 049 050 /** 051 * Checks if this component applies to the specified item. 052 * 053 * @param item The item to check against. 054 * 055 * @return True if this component applies to the item, false otherwise. 056 * @since 0.2.0.0 057 */ 058 @Override 059 public boolean appliesTo(final ItemStack item) { 060 061 return item.hasItemMeta() && item.getItemMeta() != null && item.getItemMeta().hasCustomModelData(); 062 } 063 064 /** 065 * @param serialized the serialized item stack to use 066 * @param item the item that we should use to apply this applicator to. 067 * 068 * @return the updated item. 069 * @since 0.2.0.0 070 */ 071 @Override 072 public ItemStack apply(final BukkitItemStack serialized, final ItemStack item) { 073 074 final Optional<BukkitModelDataComponent> componentOptional = serialized.component(identifier()); 075 076 if(componentOptional.isPresent()) { 077 078 final ItemMeta meta = item.getItemMeta(); 079 if(meta != null) { 080 081 final CustomModelDataComponent component = meta.getCustomModelDataComponent(); 082 final List<Color> colourList = new ArrayList<>(); 083 for(final String colourStr : colours) { 084 085 try { 086 087 colourList.add(Color.fromARGB(Integer.parseInt(colourStr))); 088 } catch(final Exception ignore) {} 089 } 090 091 component.setColors(colourList); 092 component.setFlags(flags); 093 component.setFloats(floats); 094 component.setStrings(strings); 095 meta.setCustomModelDataComponent(component); 096 item.setItemMeta(meta); 097 } 098 } 099 return item; 100 } 101 102 /** 103 * @param version the version being used when this check is called. 104 * 105 * @return true if this check is enabled for the version, otherwise false 106 * @since 0.2.0.0 107 */ 108 @Override 109 public boolean enabled(final String version) { 110 111 return VersionUtil.isOneTwentyOneFive(version); 112 } 113 114 /** 115 * @param item the item that we should use to deserialize. 116 * @param serialized the serialized item stack we should use to apply this deserializer to 117 * 118 * @return the updated serialized item. 119 * @since 0.2.0.0 120 */ 121 @Override 122 public BukkitItemStack serialize(final ItemStack item, final BukkitItemStack serialized) { 123 124 if(item.hasItemMeta()) { 125 126 final CustomModelDataComponent dataComponent = item.getItemMeta().getCustomModelDataComponent(); 127 for(final Color color : dataComponent.getColors()) { 128 129 this.colours.add(String.valueOf(color.asARGB())); 130 } 131 132 this.flags.addAll(dataComponent.getFlags()); 133 this.floats.addAll(dataComponent.getFloats()); 134 this.strings.addAll(dataComponent.getStrings()); 135 } 136 137 serialized.applyComponent(this); 138 return serialized; 139 } 140}