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 * PotionDurationScaleComponent - as of MC Snapshot 25w02a
031 *
032 * @author creatorfromhell
033 * @see <a href="https://minecraft.wiki/w/Data_component_format#potion_duration_scale">Reference</a>
034 * <p>
035 * @since 0.2.0.0
036 */
037public abstract class PotionDurationScaleComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> {
038
039  protected float potionDuration = 1.0f;
040
041  public PotionDurationScaleComponent() {
042
043  }
044
045  public PotionDurationScaleComponent(final float potionDuration) {
046
047    this.potionDuration = potionDuration;
048  }
049
050  /**
051   * @return the type of component this is.
052   * @since 0.2.0.0
053   */
054  @Override
055  public String identifier() {
056
057    return "potion_duration_scale";
058  }
059
060  /**
061   * Converts the {@link SerialComponent} to a JSON object.
062   *
063   * @return The JSONObject representing this {@link SerialComponent}.
064   * @since 0.2.0.0
065   */
066  @Override
067  public JSONObject toJSON() {
068    final JSONObject json = new JSONObject();
069    json.put("potion_duration", potionDuration);
070    return json;
071  }
072
073  @Override
074  public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) {
075    potionDuration = json.getFloat("potion_duration");
076  }
077
078  @Override
079  public boolean similar(final SerialComponent<?, ?> component) {
080    if(!(component instanceof final PotionDurationScaleComponent<?, ?> other)) return false;
081    return Float.compare(this.potionDuration, other.potionDuration) == 0;
082  }
083
084  @Override
085  public int hashCode() {
086    return Objects.hash(potionDuration);
087  }
088
089  public float potionDuration() {
090
091    return potionDuration;
092  }
093
094  public void potionDuration(final float potionDuration) {
095
096    this.potionDuration = potionDuration;
097  }
098}