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.kyori.adventure.text.Component;
022import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
023import net.tnemc.item.AbstractItemStack;
024import net.tnemc.item.JSONHelper;
025import net.tnemc.item.component.SerialComponent;
026import net.tnemc.item.platform.ItemPlatform;
027import net.tnemc.item.providers.Util;
028import org.json.simple.JSONArray;
029import org.json.simple.JSONObject;
030
031import java.util.ArrayList;
032import java.util.Arrays;
033import java.util.List;
034import java.util.Objects;
035
036/**
037 * LoreComponent
038 *
039 * @author creatorfromhell
040 * @see <a href="https://minecraft.wiki/w/Data_component_format#lore">Reference</a>
041 * <p>
042 * @since 0.2.0.0
043 */
044public abstract class LoreComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> {
045
046  protected final List<Component> lore = new ArrayList<>();
047
048  public LoreComponent() {
049
050  }
051
052  public LoreComponent(final List<Component> lore) {
053
054    this.lore.addAll(lore);
055  }
056
057  public LoreComponent(final Component... lore) {
058
059    this.lore.addAll(Arrays.asList(lore));
060  }
061
062  @Override
063  public String identifier() {
064    return "lore";
065  }
066
067  @Override
068  public JSONObject toJSON() {
069    final JSONObject json = new JSONObject();
070    final JSONArray loreArray = new JSONArray();
071
072    for(final Component component : lore) {
073      loreArray.add(LegacyComponentSerializer.legacySection().serialize(component));
074    }
075
076    json.put("lore", loreArray);
077    return json;
078  }
079
080  @Override
081  public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) {
082    lore.clear();
083
084    for(final String str : json.getStringList("lore")) {
085      lore.add(LegacyComponentSerializer.legacySection().deserialize(str));
086    }
087  }
088
089  @Override
090  public boolean similar(final SerialComponent<?, ?> component) {
091
092    System.out.println("Lore equals");
093
094    if(!(component instanceof final LoreComponent<?, ?> other)) {
095      System.out.println("incompatible component");
096      return false;
097    }
098
099    return Util.textComponentsEqual(this.lore, other.lore);
100  }
101
102  @Override
103  public int hashCode() {
104    return Objects.hash(super.hashCode(), lore);
105  }
106
107  public List<Component> lore() {
108
109    return lore;
110  }
111
112  public void lore(final List<Component> lore) {
113
114    this.lore.clear();
115    this.lore.addAll(lore);
116  }
117
118  public void lore(final Component... lore) {
119
120    this.lore.clear();
121    this.lore.addAll(Arrays.asList(lore));
122  }
123}