001package net.tnemc.item.component.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.AbstractItemStack; 022import net.tnemc.item.JSONHelper; 023import net.tnemc.item.component.SerialComponent; 024import net.tnemc.item.platform.ItemPlatform; 025import org.json.simple.JSONObject; 026 027import java.util.Objects; 028 029/** 030 * DamageComponent - The number of uses consumed (not remaining) of the item's durability. 031 * 032 * @author creatorfromhell 033 * @see <a href="https://minecraft.wiki/w/Data_component_format#damage">Reference</a> 034 * <p> 035 * @since 0.2.0.0 036 */ 037public abstract class DamageComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> { 038 039 protected int damage = 0; // The number of uses consumed 040 041 /** 042 * Represents a component that manages damage information. 043 * This component stores and provides methods for handling damage values. 044 * @since 0.2.0.0 045 */ 046 public DamageComponent() { 047 048 } 049 050 /** 051 * Constructs a new DamageComponent with the specified damage amount. 052 * 053 * @param damage the amount of damage for the component 054 * @since 0.2.0.0 055 */ 056 public DamageComponent(final int damage) { 057 058 if(damage >= 0) { 059 060 this.damage = damage; 061 } 062 } 063 064 @Override 065 public String identifier() { 066 return "damage"; 067 } 068 069 @Override 070 public JSONObject toJSON() { 071 final JSONObject json = new JSONObject(); 072 json.put("damage", damage); 073 return json; 074 } 075 076 @Override 077 public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) { 078 if(json.has("damage")) { 079 damage = json.getInteger("damage"); 080 } 081 } 082 083 @Override 084 public boolean similar(final SerialComponent<?, ?> component) { 085 if(!(component instanceof final DamageComponent<?, ?> other)) { 086 087 System.out.println("mismatch damage component"); 088 return false; 089 } 090 091 System.out.println("This damage: " + this.damage); 092 System.out.println("other damage: " + other.damage); 093 094 return this.damage == other.damage; 095 } 096 097 @Override 098 public int hashCode() { 099 return Objects.hash(damage); 100 } 101 102 /** 103 * Retrieves the current damage value. 104 * 105 * @return the current damage value 106 * @since 0.2.0.0 107 */ 108 public int damage() { 109 110 return damage; 111 } 112 113 /** 114 * Sets the amount of damage. 115 * 116 * @param damage the amount of damage to set 117 * @since 0.2.0.0 118 */ 119 public void damage(final int damage) { 120 121 if(damage >= 0) { 122 123 this.damage = damage; 124 } 125 } 126}