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