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.Objects;
028
029/**
030 * RarityComponent - Sets the rarity of this item, which affects the default color of its name. Can
031 * be common, uncommon, rare or epic.
032 *
033 * @author creatorfromhell
034 * @see <a href="https://minecraft.wiki/w/Data_component_format#rarity">Reference</a>
035 * @see <a href="https://minecraft.wiki/w/Rarity">What is Rarity?</a>
036 * <p>
037 * @since 0.2.0.0
038 */
039public abstract class RarityComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> {
040
041  protected String rarity = "common";
042
043  public RarityComponent() {
044
045  }
046
047  public RarityComponent(final String rarity) {
048
049    this.rarity = rarity;
050  }
051
052  @Override
053  public String identifier() {
054    return "rarity";
055  }
056
057  @Override
058  public JSONObject toJSON() {
059    final JSONObject json = new JSONObject();
060    json.put("rarity", rarity);
061    return json;
062  }
063
064  @Override
065  public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) {
066    rarity = json.getString("rarity");
067  }
068
069  @Override
070  public boolean similar(final SerialComponent<?, ?> component) {
071    if(!(component instanceof final RarityComponent<?, ?> other)) return false;
072    return Objects.equals(this.rarity, other.rarity);
073  }
074
075  @Override
076  public int hashCode() {
077    return Objects.hash(rarity);
078  }
079
080  public String rarity() {
081
082    return rarity;
083  }
084
085  public void rarity(final String rarity) {
086
087    this.rarity = rarity;
088  }
089}