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.ArrayList;
028import java.util.Arrays;
029import java.util.List;
030import java.util.Objects;
031
032/**
033 * DamageResistantComponent
034 *
035 * @see <a href="https://minecraft.wiki/w/Data_component_format#damage_resistant">Reference</a>
036 *
037 * @author creatorfromhell
038 * @since 0.2.0.0
039 */
040public abstract class DamageResistantComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> {
041
042  protected final List<String> types = new ArrayList<>();
043
044  public DamageResistantComponent() {
045
046  }
047
048  public DamageResistantComponent(final String type) {
049    this.types.add(type);
050  }
051
052  public DamageResistantComponent(final List<String> types) {
053    this.types.addAll(types);
054  }
055
056  @Override
057  public String identifier() {
058    return "damage_resistant";
059  }
060
061  @Override
062  public JSONObject toJSON() {
063
064    final JSONObject json = new JSONObject();
065    json.put("types", types);
066    return json;
067  }
068
069  @Override
070  public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) {
071    types.clear();
072    if(json.has("types")) {
073      types.addAll(json.getStringList("types"));
074    }
075  }
076
077  @Override
078  public boolean similar(final SerialComponent<?, ?> component) {
079    if(!(component instanceof final DamageResistantComponent<?, ?> other)) return false;
080
081    return Objects.equals(this.types, other.types);
082  }
083
084  @Override
085  public int hashCode() {
086    return Objects.hash(types);
087  }
088
089  public List<String> types() {
090
091    return types;
092  }
093
094  public void types(final List<String> types) {
095
096    this.types.clear();
097    this.types.addAll(types);
098  }
099
100  public void types(final String... types) {
101
102    this.types.clear();
103    this.types.addAll(Arrays.asList(types));
104  }
105}