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 * BreakSoundComponent - When present, this sound will be played when the item runs out of durability and breaks
031 * Added in MC 1.21.5
032 *
033 * @author creatorfromhell
034 * @see <a href="https://minecraft.wiki/w/Data_component_format#break_sound">Reference</a>
035 * <p>
036 * @since 0.2.0.0
037 */
038public abstract class BreakSoundComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> {
039
040  protected String sound = "";
041
042  public BreakSoundComponent() {
043
044  }
045
046  public BreakSoundComponent(final String sound) {
047
048    this.sound = sound;
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 "break_sound";
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("sound", sound);
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.sound = json.getString("sound");
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 BreakSoundComponent<?, ?> other)) return false;
102
103    return sound.equals(other.sound);
104  }
105
106  @Override
107  public int hashCode() {
108
109    return Objects.hash(sound);
110  }
111
112  public String sound() {
113
114    return sound;
115  }
116
117  public void sound(final String sound) {
118
119    this.sound = sound;
120  }
121}