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.DamageComponent; 024import net.tnemc.item.providers.VersionUtil; 025import org.bukkit.inventory.ItemStack; 026import org.bukkit.inventory.meta.Damageable; 027 028import java.util.Optional; 029 030/** 031 * BukkitDamageComponent 032 * 033 * @author creatorfromhell 034 * @since 0.2.0.0 035 */ 036public class BukkitDamageComponent extends DamageComponent<BukkitItemStack, ItemStack> { 037 038 /** 039 * Represents a component that manages damage information. This component stores and provides 040 * methods for handling damage values. 041 * @since 0.2.0.0 042 */ 043 public BukkitDamageComponent() { 044 045 } 046 047 /** 048 * Constructs a new DamageComponent with the specified damage amount. 049 * 050 * @param damage the amount of damage for the component 051 * @since 0.2.0.0 052 */ 053 public BukkitDamageComponent(final int damage) { 054 055 super(damage); 056 } 057 058 /** 059 * @param version the version being used when this check is called. 060 * 061 * @return true if this check is enabled for the version, otherwise false 062 * @since 0.2.0.0 063 */ 064 @Override 065 public boolean enabled(final String version) { 066 067 return true; 068 } 069 070 /** 071 * @param serialized the serialized item stack to use 072 * @param item the item that we should use to apply this applicator to. 073 * 074 * @return the updated item. 075 * @since 0.2.0.0 076 */ 077 @Override 078 public ItemStack apply(final BukkitItemStack serialized, final ItemStack item) { 079 080 final Optional<BukkitDamageComponent> componentOptional = serialized.component(identifier()); 081 082 if(componentOptional.isPresent()) { 083 if(VersionUtil.isOneThirteen(BukkitItemPlatform.instance().version())) { 084 085 if(item.hasItemMeta() && item.getItemMeta() instanceof final Damageable meta) { 086 087 meta.setDamage(componentOptional.get().damage()); 088 item.setItemMeta(meta); 089 } 090 } else { 091 092 item.setDurability((short)componentOptional.get().damage); 093 } 094 } 095 return item; 096 } 097 098 /** 099 * @param item the item that we should use to deserialize. 100 * @param serialized the serialized item stack we should use to apply this deserializer to 101 * 102 * @return the updated serialized item. 103 * @since 0.2.0.0 104 */ 105 @Override 106 public BukkitItemStack serialize(final ItemStack item, final BukkitItemStack serialized) { 107 108 if(VersionUtil.isOneThirteen(BukkitItemPlatform.instance().version())) { 109 110 if(item.hasItemMeta() && item.getItemMeta() instanceof final Damageable meta) { 111 112 if(meta.hasDamage()) { 113 114 this.damage = meta.getDamage(); 115 116 serialized.applyComponent(this); 117 } 118 } 119 } else { 120 this.damage = item.getDurability(); 121 122 serialized.applyComponent(this); 123 } 124 return serialized; 125 } 126 127 /** 128 * Checks if this component applies to the specified item. 129 * 130 * @param item The item to check against. 131 * 132 * @return True if this component applies to the item, false otherwise. 133 * @since 0.2.0.0 134 */ 135 @Override 136 public boolean appliesTo(final ItemStack item) { 137 138 return true; 139 } 140}