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