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