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.effect.EffectInstance;
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 * SuspiciousStewEffectsComponent - The effects applied when consuming this suspicious stew.
036 *
037 * @author creatorfromhell
038 * @see <a href="https://minecraft.wiki/w/Data_component_format#suspicious_stew_effects">Reference</a>
039 * @see <a href="https://minecraft.wiki/w/Suspicious_Stew">Suspicious_Stew</a>
040 * <p>
041 * @since 0.2.0.0
042 */
043public abstract class SuspiciousStewEffectsComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> {
044
045  protected final List<EffectInstance> effects = new ArrayList<>();
046
047  public SuspiciousStewEffectsComponent() {
048
049  }
050
051  public SuspiciousStewEffectsComponent(final List<EffectInstance> effects) {
052
053    this.effects.addAll(effects);
054  }
055
056  public SuspiciousStewEffectsComponent(final EffectInstance... effects) {
057
058    this.effects.addAll(Arrays.asList(effects));
059  }
060
061  @Override
062  public String identifier() {
063    return "suspicious_stew_effects";
064  }
065
066  @Override
067  public JSONObject toJSON() {
068    final JSONObject json = new JSONObject();
069
070    final JSONArray effectsArray = new JSONArray();
071    for (final EffectInstance effect : effects) {
072      effectsArray.add(effect.toJSON());
073    }
074    json.put("custom_effects", effectsArray);
075    return json;
076  }
077
078  @Override
079  public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) {
080
081    effects.clear();
082    final JSONArray effectsArray = (JSONArray) json.getObject().get("custom_effects");
083    for (final Object obj : effectsArray) {
084
085      final EffectInstance effect = new EffectInstance();
086      effect.readJSON(new JSONHelper((JSONObject) obj));
087      effects.add(effect);
088    }
089  }
090
091  @Override
092  public boolean similar(final SerialComponent<?, ?> component) {
093    if(!(component instanceof final SuspiciousStewEffectsComponent<?, ?> other)) return false;
094    return Objects.equals(this.effects, other.effects);
095  }
096
097  @Override
098  public int hashCode() {
099    return Objects.hash(effects);
100  }
101
102  public List<EffectInstance> effects() {
103
104    return effects;
105  }
106
107  public void effects(final List<EffectInstance> effects) {
108
109    this.effects.clear();
110    this.effects.addAll(effects);
111  }
112
113  public void effects(final EffectInstance... effects) {
114
115    this.effects.clear();
116    this.effects.addAll(Arrays.asList(effects));
117  }
118}