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 * UseCooldownComponent 031 * 032 * @author creatorfromhell 033 * @see <a href="https://minecraft.wiki/w/Data_component_format#use_cooldown">Reference</a> 034 * <p> 035 * @since 0.2.0.0 036 */ 037public abstract class UseCooldownComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> { 038 039 protected float seconds; 040 protected String cooldownGroup; 041 042 public UseCooldownComponent(final float seconds) { 043 044 this.seconds = seconds; 045 } 046 047 public UseCooldownComponent(final String cooldownGroup) { 048 049 this.cooldownGroup = cooldownGroup; 050 } 051 052 public UseCooldownComponent(final String cooldownGroup, final float seconds) { 053 054 this.cooldownGroup = cooldownGroup; 055 this.seconds = seconds; 056 } 057 058 @Override 059 public String identifier() { 060 return "use_cooldown"; 061 } 062 063 @Override 064 public JSONObject toJSON() { 065 final JSONObject json = new JSONObject(); 066 json.put("seconds", seconds); 067 json.put("cooldown_group", cooldownGroup); 068 return json; 069 } 070 071 @Override 072 public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) { 073 seconds = json.getFloat("seconds"); 074 cooldownGroup = json.getString("cooldown_group"); 075 } 076 077 @Override 078 public boolean similar(final SerialComponent<?, ?> component) { 079 if(!(component instanceof final UseCooldownComponent<?, ?> other)) return false; 080 return this.seconds == other.seconds && Objects.equals(this.cooldownGroup, other.cooldownGroup); 081 } 082 083 @Override 084 public int hashCode() { 085 return Objects.hash(seconds, cooldownGroup); 086 } 087 088 public float seconds() { 089 090 return seconds; 091 } 092 093 public void seconds(final float seconds) { 094 095 this.seconds = seconds; 096 } 097 098 public String cooldownGroup() { 099 100 return cooldownGroup; 101 } 102 103 public void cooldownGroup(final String cooldownGroup) { 104 105 this.cooldownGroup = cooldownGroup; 106 } 107}