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 * TooltipDisplayComponent - Allows the tooltips provided specifically by any given item component to
035 * be surpressed. As of MC 1.21.5.
036 *
037 * @author creatorfromhell
038 * @since 0.2.0.0
039 */
040public abstract class TooltipDisplayComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> {
041
042  protected final List<String> hiddenComponents = new ArrayList<>();
043
044  protected boolean hideTooltip = false;
045
046  public TooltipDisplayComponent() {
047
048  }
049
050  public TooltipDisplayComponent(final boolean hideTooltip) {
051
052    this.hideTooltip = hideTooltip;
053  }
054
055  public TooltipDisplayComponent(final boolean hideTooltip, final List<String> hiddenComponents) {
056
057    this.hideTooltip = hideTooltip;
058    this.hiddenComponents.addAll(hiddenComponents);
059  }
060
061  public TooltipDisplayComponent(final boolean hideTooltip, final String... hiddenComponents) {
062
063    this.hideTooltip = hideTooltip;
064    this.hiddenComponents.addAll(Arrays.asList(hiddenComponents));
065  }
066
067  @Override
068  public String identifier() {
069    return "tooltip_display";
070  }
071
072  @Override
073  public JSONObject toJSON() {
074    final JSONObject json = new JSONObject();
075    final JSONArray componentArray = new JSONArray();
076
077    json.put("hideTooltip", hideTooltip);
078
079    for(final String component : hiddenComponents) {
080      componentArray.add(component);
081    }
082
083    json.put("hiddenComponents", componentArray);
084    return json; // Empty JSON as no additional data is needed
085  }
086
087  @Override
088  public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) {
089    hideTooltip = json.getBoolean("hideTooltip");
090
091    hiddenComponents.clear();
092
093    for(final String component : json.getStringList("hiddenComponents")) {
094      hiddenComponents.add(component);
095    }
096  }
097
098  @Override
099  public boolean similar(final SerialComponent<?, ?> component) {
100    if(!(component instanceof final TooltipDisplayComponent<?, ?> other)) return false;
101    return Objects.equals(this.hiddenComponents, other.hiddenComponents) && Objects.equals(this.hideTooltip, other.hideTooltip);
102  }
103
104  @Override
105  public int hashCode() {
106    return Objects.hash(identifier(), hiddenComponents, hideTooltip);
107  }
108
109  public List<String> hiddenComponents() {
110
111    return hiddenComponents;
112  }
113
114  public void hiddenComponents(final List<String> hiddenComponents) {
115
116    this.hiddenComponents.clear();
117    this.hiddenComponents.addAll(hiddenComponents);
118  }
119
120  public void hiddenComponents(final String... hiddenComponents) {
121
122    this.hiddenComponents.clear();
123    this.hiddenComponents.addAll(Arrays.asList(hiddenComponents));
124  }
125
126  public boolean hideTooltip() {
127
128    return hideTooltip;
129  }
130
131  public void hideTooltip(final boolean hideTooltip) {
132
133    this.hideTooltip = hideTooltip;
134  }
135}