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.component.helper.ExplosionData;
025import net.tnemc.item.platform.ItemPlatform;
026import org.json.simple.JSONArray;
027import org.json.simple.JSONObject;
028
029import java.util.ArrayList;
030import java.util.Arrays;
031import java.util.List;
032import java.util.Objects;
033
034/**
035 * FireworksComponent
036 *
037 * @author creatorfromhell
038 * @since 0.2.0.0
039 */
040public abstract class FireworksComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> {
041
042  protected final List<ExplosionData> explosions = new ArrayList<>();
043  protected byte flightDuration;
044
045  public FireworksComponent() {
046
047  }
048
049  public FireworksComponent(final byte flightDuration) {
050
051    this.flightDuration = flightDuration;
052  }
053
054  public FireworksComponent(final byte flightDuration, final List<ExplosionData> explosions) {
055
056    this.flightDuration = flightDuration;
057    this.explosions.addAll(explosions);
058  }
059
060  @Override
061  public String identifier() {
062    return "fireworks";
063  }
064
065  @Override
066  public JSONObject toJSON() {
067    final JSONObject json = new JSONObject();
068
069    final JSONArray explosionsArray = new JSONArray();
070    for(final ExplosionData explosion : explosions) {
071
072      explosionsArray.add(explosion.toJSON());
073    }
074    json.put("explosions", explosionsArray);
075    json.put("flight_duration", flightDuration);
076
077    return json;
078  }
079
080  @Override
081  public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) {
082    explosions.clear();
083
084    final JSONArray explosionsArray = (JSONArray) json.getObject().get("explosions");
085    if(explosionsArray != null) {
086      for(final Object obj : explosionsArray) {
087
088        final ExplosionData explosion = new ExplosionData();
089        explosion.readJSON(new JSONHelper((JSONObject) obj));
090        explosions.add(explosion);
091      }
092    }
093
094    if(json.has("flight_duration")) {
095      flightDuration = Byte.parseByte(json.getString("flight_duration"));
096    }
097  }
098
099  @Override
100  public boolean similar(final SerialComponent<?, ?> component) {
101    if(!(component instanceof final FireworksComponent<?, ?> other)) return false;
102
103    return this.flightDuration == other.flightDuration &&
104           Objects.equals(this.explosions, other.explosions);
105  }
106
107  @Override
108  public int hashCode() {
109    return Objects.hash(explosions, flightDuration);
110  }
111
112  public List<ExplosionData> explosions() {
113
114    return explosions;
115  }
116
117  public void explosions(final List<ExplosionData> explosions) {
118
119    this.explosions.clear();
120    this.explosions.addAll(explosions);
121  }
122
123  public void explosions(final ExplosionData... explosions) {
124
125    this.explosions.clear();
126    this.explosions.addAll(Arrays.asList(explosions));
127  }
128
129  public byte flightDuration() {
130
131    return flightDuration;
132  }
133
134  public void flightDuration(final byte flightDuration) {
135
136    this.flightDuration = flightDuration;
137  }
138}