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.kyori.adventure.text.Component; 022import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; 023import net.tnemc.item.AbstractItemStack; 024import net.tnemc.item.JSONHelper; 025import net.tnemc.item.component.SerialComponent; 026import net.tnemc.item.platform.ItemPlatform; 027import org.json.simple.JSONObject; 028 029import java.util.Objects; 030 031/** 032 * CustomNameComponent 033 * 034 * @author creatorfromhell 035 * @see <a href="https://minecraft.wiki/w/Data_component_format#custom_name">Reference</a> 036 * <p> 037 * @since 0.2.0.0 038 */ 039public abstract class CustomNameComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> { 040 041 protected Component customName; // Stores the item's custom name as a string 042 043 public CustomNameComponent() { 044 045 } 046 047 public CustomNameComponent(final Component customName) { 048 049 this.customName = customName; 050 } 051 052 @Override 053 public String identifier() { 054 return "custom_name"; 055 } 056 057 @Override 058 public JSONObject toJSON() { 059 final JSONObject json = new JSONObject(); 060 json.put("custom_name", LegacyComponentSerializer.legacySection().serialize(customName)); 061 return json; 062 } 063 064 @Override 065 public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) { 066 if(json.has("custom_name")) { 067 customName = LegacyComponentSerializer.legacySection().deserialize(json.getString("custom_name")); 068 } 069 } 070 071 @Override 072 public boolean similar(final SerialComponent<?, ?> component) { 073 if(!(component instanceof final CustomNameComponent<?, ?> other)) return false; 074 075 return Objects.equals(this.customName, other.customName); 076 } 077 078 @Override 079 public int hashCode() { 080 return Objects.hash(customName); 081 } 082 083 public Component customName() { 084 085 return customName; 086 } 087 088 public void customName(final Component customName) { 089 090 this.customName = customName; 091 } 092}