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 * DyedColorComponent -The color applied of this leather armor piece. Color codes are the hex code of
031 * the color converted to a decimal number, or can be calculated from the Red, Green and Blue components
032 * using this formula: R << 16 + G << 8 + B
033 *
034 * @author creatorfromhell
035 * @see <a href="https://minecraft.wiki/w/Data_component_format#dyed_color">Reference</a>
036 * <p>
037 * @since 0.2.0.0
038 */
039public abstract class DyedColorComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> {
040
041  protected int rgb;
042
043  public DyedColorComponent() {
044
045  }
046
047  public DyedColorComponent(final int rgb) {
048
049    this.rgb = rgb;
050  }
051
052  public DyedColorComponent(final int red, final int green, final int blue) {
053
054    this.rgb = (red << 16) + (green << 8) + blue;
055  }
056
057  @Override
058  public String identifier() {
059    return "dyed_color";
060  }
061
062  @Override
063  public JSONObject toJSON() {
064    final JSONObject json = new JSONObject();
065    json.put("rgb", rgb);
066    return json;
067  }
068
069  @Override
070  public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) {
071    rgb = json.getInteger("rgb");
072  }
073
074  @Override
075  public boolean similar(final SerialComponent<?, ?> component) {
076    if(!(component instanceof final DyedColorComponent<?, ?> other)) return false;
077
078    return this.rgb == other.rgb;
079  }
080
081  @Override
082  public int hashCode() {
083    return Objects.hash(rgb);
084  }
085
086  public int rgb() {
087
088    return rgb;
089  }
090
091  public void rgb(final int rgb) {
092
093    this.rgb = rgb;
094  }
095
096  public void rgb(final int red, final int green, final int blue) {
097
098    this.rgb = (red << 16) + (green << 8) + blue;
099  }
100}