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 * ItemNameComponent
033 *
034 * @see <a href="https://minecraft.wiki/w/Data_component_format#item_name">Reference</a>
035 * <p>
036 * @author creatorfromhell
037 * @since 0.2.0.0
038 */
039public abstract class ItemNameComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> {
040
041  protected Component itemName;
042
043  public ItemNameComponent() {
044
045  }
046
047  public ItemNameComponent(final Component itemName) {
048
049    this.itemName = itemName;
050  }
051
052  @Override
053  public String identifier() {
054    return "item_name";
055  }
056
057  @Override
058  public JSONObject toJSON() {
059    final JSONObject json = new JSONObject();
060    json.put("item_name", LegacyComponentSerializer.legacySection().serialize(itemName));
061    return json;
062  }
063
064  @Override
065  public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) {
066    itemName = LegacyComponentSerializer.legacySection().deserialize(json.getString("item_name"));
067  }
068
069  @Override
070  public boolean similar(final SerialComponent<?, ?> component) {
071    if(!(component instanceof final ItemNameComponent<?, ?> other)) return false;
072
073    return Objects.equals(this.itemName, other.itemName);
074  }
075
076  @Override
077  public int hashCode() {
078    return Objects.hash(itemName);
079  }
080
081  public Component itemName() {
082
083    return itemName;
084  }
085
086  public void itemName(final Component itemName) {
087
088    this.itemName = itemName;
089  }
090}