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 * ModelDataOldComponent
031 *
032 * @author creatorfromhell
033 * @since 0.2.0.0
034 */
035public abstract class ModelDataOldComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> {
036
037  protected int modelData;
038
039  public ModelDataOldComponent() {
040    this.modelData = -1;
041  }
042
043  public ModelDataOldComponent(final int modelData) {
044
045    this.modelData = modelData;
046  }
047
048  /**
049   * @return the type of component this is.
050   * @since 0.2.0.0
051   */
052  @Override
053  public String identifier() {
054    return "model-data-old";
055  }
056
057  /**
058   * Converts this component's data to a JSON object.
059   *
060   * @return The JSONObject representing this component's data.
061   * @since 0.2.0.0
062   */
063  @Override
064  public JSONObject toJSON() {
065    final JSONObject json = new JSONObject();
066    json.put("modelData", modelData);
067    return json;
068  }
069
070  /**
071   * Reads JSON data and converts it back to this component's data.
072   *
073   * @param json      The JSONHelper instance of the json data.
074   * @param platform  The ItemPlatform instance.
075   * @since 0.2.0.0
076   */
077  @Override
078  public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) {
079    this.modelData = json.getInteger("modelData");
080  }
081
082  /**
083   * Used to determine if some data is equal to this data. This means that it has to be an exact
084   * copy of this data.
085   *
086   * @param component The component to compare.
087   * @return True if similar, otherwise false.
088   * @since 0.2.0.0
089   */
090  @Override
091  public boolean similar(final SerialComponent<?, ?> component) {
092    if(!(component instanceof final ModelDataOldComponent<?, ?> other)) return false;
093    return this.modelData == other.modelData();
094  }
095
096  @Override
097  public int hashCode() {
098    return Objects.hash(modelData);
099  }
100
101  public int modelData() {
102
103    return modelData;
104  }
105
106  public void modelData(final int modelData) {
107
108    this.modelData = modelData;
109  }
110}