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