001package net.tnemc.item.component.helper;
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.JSONHelper;
022import org.json.simple.JSONObject;
023
024import java.util.ArrayList;
025import java.util.Arrays;
026import java.util.List;
027import java.util.Objects;
028
029/**
030 * ExplosionData
031 *
032 * @author creatorfromhell
033 * @since 0.2.0.0
034 */
035public class ExplosionData {
036
037  protected String shape; // small_ball, large_ball, star, creeper, burst
038  protected final List<Integer> colors = new ArrayList<>();
039  protected final List<Integer> fadeColors = new ArrayList<>();
040  protected boolean hasTrail;
041  protected boolean hasTwinkle;
042
043  public ExplosionData() {
044
045  }
046
047  public ExplosionData(final String shape, final boolean hasTrail, final boolean hasTwinkle) {
048
049    this.shape = shape;
050    this.hasTrail = hasTrail;
051    this.hasTwinkle = hasTwinkle;
052  }
053
054  public ExplosionData(final String shape, final boolean hasTrail, final boolean hasTwinkle, final List<Integer> colors, final List<Integer> fadeColors) {
055
056    this.shape = shape;
057    this.hasTrail = hasTrail;
058    this.hasTwinkle = hasTwinkle;
059    this.colors.addAll(colors);
060    this.fadeColors.addAll(fadeColors);
061  }
062
063  /**
064   * Converts this explosion data to a JSON object.
065   *
066   * @return The JSONObject representing this explosion data.
067   * @since 0.2.0.0
068   */
069  public JSONObject toJSON() {
070    final JSONObject json = new JSONObject();
071    json.put("shape", shape);
072    json.put("colors", colors);
073    json.put("fade_colors", fadeColors);
074    json.put("has_trail", hasTrail);
075    json.put("has_twinkle", hasTwinkle);
076    return json;
077  }
078
079  /**
080   * Reads JSON data and populates this explosion data.
081   *
082   * @param json The JSONHelper instance of the JSON data.
083   * @since 0.2.0.0
084   */
085  public void readJSON(final JSONHelper json) {
086    shape = json.getString("shape");
087
088    colors.clear();
089    colors.addAll(json.getIntegerList("colors"));
090
091    fadeColors.clear();
092    fadeColors.addAll(json.getIntegerList("fade_colors"));
093
094    hasTrail = json.getBoolean("has_trail");
095    hasTwinkle = json.getBoolean("has_twinkle");
096  }
097
098  @Override
099  public boolean equals(final Object obj) {
100    if(!(obj instanceof final ExplosionData other)) return false;
101
102    return Objects.equals(this.shape, other.shape) &&
103           Objects.equals(this.colors, other.colors) &&
104           Objects.equals(this.fadeColors, other.fadeColors) &&
105           this.hasTrail == other.hasTrail &&
106           this.hasTwinkle == other.hasTwinkle;
107  }
108
109  @Override
110  public int hashCode() {
111    return Objects.hash(shape, colors, fadeColors, hasTrail, hasTwinkle);
112  }
113
114  public String shape() {
115
116    return shape;
117  }
118
119  public void shape(final String shape) {
120
121    this.shape = shape;
122  }
123
124  public List<Integer> colors() {
125
126    return colors;
127  }
128
129  public void colors(final List<Integer> colors) {
130
131    this.colors.clear();
132    this.colors.addAll(colors);
133  }
134
135  public void colors(final Integer... colors) {
136
137    this.colors.clear();
138    this.colors.addAll(Arrays.asList(colors));
139  }
140
141  public List<Integer> fadeColors() {
142
143    return fadeColors;
144  }
145
146  public void fadeColors(final List<Integer> colors) {
147
148    this.fadeColors.clear();
149    this.fadeColors.addAll(colors);
150  }
151
152  public void fadeColors(final Integer... colors) {
153
154    this.fadeColors.clear();
155    this.fadeColors.addAll(Arrays.asList(colors));
156  }
157
158  public boolean hasTrail() {
159
160    return hasTrail;
161  }
162
163  public void hasTrail(final boolean hasTrail) {
164
165    this.hasTrail = hasTrail;
166  }
167
168  public boolean hasTwinkle() {
169
170    return hasTwinkle;
171  }
172
173  public void hasTwinkle(final boolean hasTwinkle) {
174
175    this.hasTwinkle = hasTwinkle;
176  }
177
178  @Override
179  public ExplosionData clone() throws CloneNotSupportedException {
180
181    return new ExplosionData(
182            this.shape,
183            this.hasTrail,
184            this.hasTwinkle,
185            new ArrayList<>(this.colors),
186            new ArrayList<>(this.fadeColors)
187    );
188  }
189}