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
027/**
028 * EnchantableComponent - outline from Snapshot 24w33a
029 *
030 * @see <a href="https://minecraft.wiki/w/Data_component_format#enchantable">Reference</a>
031 *
032 * @author creatorfromhell
033 * @since 0.2.0.0
034 */
035public abstract class EnchantableComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> {
036
037  protected int value;
038
039  public EnchantableComponent() {
040
041  }
042
043  public EnchantableComponent(final int value) {
044
045    this.value = value;
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
055    return "enchantable";
056  }
057
058  /**
059   * Converts the {@link SerialComponent} to a JSON object.
060   *
061   * @return The JSONObject representing this {@link SerialComponent}.
062   * @since 0.2.0.0
063   */
064  @Override
065  public JSONObject toJSON() {
066
067    final JSONObject enchantable = new JSONObject();
068    enchantable.put("name", "enchantable-component");
069    enchantable.put("value", value);
070
071    return enchantable;
072  }
073
074  /**
075   * Reads JSON data and converts it back to a {@link SerialComponent} object.
076   *
077   * @param json The JSONHelper instance of the json data.
078   * @since 0.2.0.0
079   */
080  @Override
081  public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) {
082
083    value = json.getInteger("value");
084  }
085
086  /**
087   * Used to determine if some data is equal to this data. This means that it has to be an exact
088   * copy of this data. For instance, book copies will return false when compared to the original.
089   *
090   * @param component The component to compare.
091   *
092   * @return True if similar, otherwise false.
093   * @since 0.2.0.0
094   */
095  @Override
096  public boolean similar(final SerialComponent<?, ?> component) {
097
098    if(component instanceof final EnchantableComponent<?, ?> enchant) {
099
100      return this.value == enchant.value;
101    }
102    return false;
103  }
104
105  public int value() {
106
107    return value;
108  }
109
110  public void value(final int value) {
111
112    this.value = value;
113  }
114}