001package net.tnemc.item.component.helper.effect;
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.JSONArray;
023import org.json.simple.JSONObject;
024
025import java.util.ArrayList;
026import java.util.List;
027import java.util.Objects;
028
029/**
030 * ApplyEffectsReviveEffect
031 *
032 * @author creatorfromhell
033 * @since 0.2.0.0
034 */
035public class ApplyEffectsComponentEffect extends ComponentEffect {
036
037  private final List<EffectInstance> effects = new ArrayList<>();
038
039  @Override
040  public String getType() {
041    return "apply_effects";
042  }
043
044  @Override
045  public JSONObject toJSON() {
046    final JSONObject json = new JSONObject();
047    json.put("type", getType());
048    json.put("probability", probability);
049
050    final JSONArray effectsArray = new JSONArray();
051    for (final EffectInstance effect : effects) {
052      effectsArray.add(effect.toJSON());
053    }
054    json.put("effects", effectsArray);
055
056    return json;
057  }
058
059  @Override
060  public void readJSON(final JSONHelper json) {
061    probability = json.getFloat("probability");
062
063    effects.clear();
064    if(json.has("effects")) {
065      final JSONArray effectsArray = (JSONArray) json.getObject().get("effects");
066      for (final Object obj : effectsArray) {
067        final EffectInstance effect = new EffectInstance();
068        effect.readJSON(new JSONHelper((JSONObject) obj));
069        effects.add(effect);
070      }
071    }
072  }
073
074  public List<EffectInstance> getEffects() {
075    return effects;
076  }
077
078  @Override
079  public boolean equals(final Object obj) {
080    if(!(obj instanceof final ApplyEffectsComponentEffect other)) return false;
081
082    return super.equals(obj) && Objects.equals(this.effects, other.effects);
083  }
084
085  @Override
086  public int hashCode() {
087    return Objects.hash(super.hashCode(), effects);
088  }
089
090  @Override
091  public ApplyEffectsComponentEffect clone() {
092
093    final ApplyEffectsComponentEffect copy = new ApplyEffectsComponentEffect();
094    copy.probability(this.probability);
095
096    for(final EffectInstance effect : this.effects) {
097      copy.getEffects().add(new EffectInstance(
098              effect.id(),
099              effect.amplifier(),
100              effect.duration(),
101              effect.showParticles(),
102              effect.ambient(),
103              effect.showIcon()
104      ));
105    }
106    return copy;
107  }
108}