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.TrimComponent; 024import net.tnemc.item.providers.VersionUtil; 025import org.bukkit.inventory.ItemStack; 026import org.bukkit.inventory.meta.ArmorMeta; 027import org.bukkit.inventory.meta.trim.ArmorTrim; 028import org.bukkit.inventory.meta.trim.TrimMaterial; 029import org.bukkit.inventory.meta.trim.TrimPattern; 030 031import java.util.Optional; 032 033/** 034 * BukkitTrimComponent 035 * 036 * @author creatorfromhell 037 * @since 0.2.0.0 038 */ 039public class BukkitTrimComponent extends TrimComponent<BukkitItemStack, ItemStack> { 040 041 public BukkitTrimComponent() { 042 043 } 044 045 public BukkitTrimComponent(final String pattern, final String material) { 046 047 super(pattern, material); 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.isOneTwenty(version); 060 } 061 062 /** 063 * @param serialized the serialized item stack to use 064 * @param item the item that we should use to apply this applicator to. 065 * 066 * @return the updated item. 067 * @since 0.2.0.0 068 */ 069 @Override 070 public ItemStack apply(final BukkitItemStack serialized, final ItemStack item) { 071 072 final Optional<BukkitTrimComponent> componentOptional = serialized.component(identifier()); 073 componentOptional.ifPresent(component->{ 074 075 if(item.hasItemMeta() && item.getItemMeta() instanceof final ArmorMeta meta) { 076 077 try { 078 079 final TrimMaterial material = BukkitItemPlatform.instance().converter().convert(componentOptional.get().material, TrimMaterial.class); 080 final TrimPattern pattern = BukkitItemPlatform.instance().converter().convert(componentOptional.get().pattern, TrimPattern.class); 081 if(material != null && pattern != null) { 082 083 meta.setTrim(new ArmorTrim(material, pattern)); 084 } 085 086 } catch(final Exception ignore) { 087 088 //invalid material/pattern 089 } 090 091 item.setItemMeta(meta); 092 } 093 }); 094 return item; 095 } 096 097 /** 098 * @param item the item that we should use to deserialize. 099 * @param serialized the serialized item stack we should use to apply this deserializer to 100 * 101 * @return the updated serialized item. 102 * @since 0.2.0.0 103 */ 104 @Override 105 public BukkitItemStack serialize(final ItemStack item, final BukkitItemStack serialized) { 106 107 if(item.hasItemMeta() && item.getItemMeta() instanceof final ArmorMeta meta && meta.getTrim() != null) { 108 109 try { 110 111 final String material = BukkitItemPlatform.instance().converter().convert(meta.getTrim().getMaterial(), String.class); 112 final String pattern = BukkitItemPlatform.instance().converter().convert(meta.getTrim().getPattern(), String.class); 113 if(material != null && pattern != null) { 114 115 this.material = material; 116 this.pattern = pattern; 117 } 118 119 } catch(final Exception ignore) { 120 //invalid material/pattern 121 } 122 } 123 124 serialized.applyComponent(this); 125 return serialized; 126 } 127 128 /** 129 * Checks if this component applies to the specified item. 130 * 131 * @param item The item to check against. 132 * 133 * @return True if this component applies to the item, false otherwise. 134 * @since 0.2.0.0 135 */ 136 @Override 137 public boolean appliesTo(final ItemStack item) { 138 139 return item.hasItemMeta() && item.getItemMeta() instanceof ArmorMeta; 140 } 141}