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 * BaseColorComponent - The base dye color of the banner applied on a shield
031 *
032 * @see <a href="https://minecraft.wiki/w/Data_component_format#base_color">Reference</a>
033 *
034 * @author creatorfromhell
035 * @since 0.2.0.0
036 */
037
038public abstract class BaseColorComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> {
039
040  protected String color = "none";
041
042  public BaseColorComponent() {
043
044  }
045
046  /**
047   * Constructs a new BaseColorComponent with the specified color.
048   *
049   * @param color The base color value for the component.
050   * @since 0.2.0.0
051   */
052  public BaseColorComponent(final String color) {
053
054    this.color = color;
055  }
056
057  /**
058   * @return the type of component this is.
059   * @since 0.2.0.0
060   */
061  @Override
062  public String identifier() {
063    return "base_color";
064  }
065
066  /**
067   * Converts this component's data to a JSON object.
068   *
069   * @return The JSONObject representing this component's data.
070   * @since 0.2.0.0
071   */
072  @Override
073  public JSONObject toJSON() {
074    final JSONObject json = new JSONObject();
075    json.put("color", color);
076    return json;
077  }
078
079  /**
080   * Reads JSON data and converts it back to this component's data.
081   *
082   * @param json The JSONHelper instance of the JSON data.
083   * @param platform The ItemPlatform instance.
084   * @since 0.2.0.0
085   */
086  @Override
087  public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) {
088    if(json.has("color")) {
089      color = json.getString("color");
090    }
091  }
092
093  /**
094   * Used to determine if some data is equal to this data. This means that it has to be an exact
095   * copy of this data.
096   *
097   * @param component The component to compare.
098   * @return True if similar, otherwise false.
099   * @since 0.2.0.0
100   */
101  @Override
102  public boolean similar(final SerialComponent<?, ?> component) {
103    if(!(component instanceof final BaseColorComponent<?, ?> other)) return false;
104
105    return Objects.equals(color, other.color);
106  }
107
108  @Override
109  public int hashCode() {
110    return Objects.hash(color);
111  }
112
113  public String color() {
114
115    return color;
116  }
117
118  public void color(final String color) {
119
120    this.color = color;
121  }
122}