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.ArrayList; 028import java.util.Arrays; 029import java.util.List; 030import java.util.Objects; 031 032/** 033 * ModelDataComponent 034 * 035 * @see <a href="https://minecraft.wiki/w/Data_component_format#custom_model_data">Minecraft Reference</a> 036 * <p> 037 * Note: Paper API v 1.21.4 038 * @author creatorfromhell 039 * @since 0.2.0.0 040 */ 041public abstract class ModelDataComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> { 042 043 protected final List<String> colours = new ArrayList<>(); 044 protected final List<Float> floats = new ArrayList<>(); 045 protected final List<Boolean> flags = new ArrayList<>(); 046 protected final List<String> strings = new ArrayList<>(); 047 048 public ModelDataComponent() { 049 050 } 051 052 public ModelDataComponent(final List<String> colours, final List<Float> floats, final List<Boolean> flags, final List<String> strings) { 053 054 this.colours.addAll(colours); 055 this.floats.addAll(floats); 056 this.flags.addAll(flags); 057 this.strings.addAll(strings); 058 } 059 060 /** 061 * @return the type of component this is. 062 * @since 0.2.0.0 063 */ 064 @Override 065 public String identifier() { 066 return "model-data"; 067 } 068 069 /** 070 * Converts this component's data to a JSON object. 071 * 072 * @return The JSONObject representing this component's data. 073 * @since 0.2.0.0 074 */ 075 @Override 076 public JSONObject toJSON() { 077 final JSONObject json = new JSONObject(); 078 json.put("colours", colours); 079 json.put("floats", floats); 080 json.put("flags", flags); 081 json.put("strings", strings); 082 return json; 083 } 084 085 /** 086 * Reads JSON data and converts it back to this component's data. 087 * 088 * @param json The JSONHelper instance of the json data. 089 * @param platform The ItemPlatform instance. 090 * @since 0.2.0.0 091 */ 092 @Override 093 public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) { 094 colours.clear(); 095 colours.addAll(json.getStringList("colours")); 096 097 floats.clear(); 098 floats.addAll(json.getFloatList("floats")); 099 100 flags.clear(); 101 flags.addAll(json.getBooleanList("flags")); 102 103 strings.clear(); 104 strings.addAll(json.getStringList("strings")); 105 } 106 107 /** 108 * Used to determine if some data is equal to this data. This means that it has to be an exact 109 * copy of this data. 110 * 111 * @param component The component to compare. 112 * @return True if similar, otherwise false. 113 * @since 0.2.0.0 114 */ 115 @Override 116 public boolean similar(final SerialComponent<?, ?> component) { 117 if(!(component instanceof final ModelDataComponent<?, ?> other)) return false; 118 return Objects.equals(this.colours, other.colours) && 119 Objects.equals(this.floats, other.floats) && 120 Objects.equals(this.flags, other.flags) && 121 Objects.equals(this.strings, other.strings); 122 } 123 124 @Override 125 public int hashCode() { 126 return Objects.hash(colours, floats, flags, strings); 127 } 128 129 public List<String> colours() { 130 131 return colours; 132 } 133 134 public void colours(final List<String> colours) { 135 136 this.colours.clear(); 137 this.colours.addAll(colours); 138 } 139 140 public void colours(final String... colours) { 141 142 this.colours.clear(); 143 this.colours.addAll(Arrays.asList(colours)); 144 } 145 146 public List<Float> floats() { 147 148 return floats; 149 } 150 151 public void floats(final List<Float> floats) { 152 153 this.floats.clear(); 154 this.floats.addAll(floats); 155 } 156 157 public void floats(final Float... floats) { 158 159 this.floats.clear(); 160 this.floats.addAll(Arrays.asList(floats)); 161 } 162 163 public List<Boolean> flags() { 164 165 return flags; 166 } 167 168 public void flags(final List<Boolean> flags) { 169 170 this.flags.clear(); 171 this.flags.addAll(flags); 172 } 173 174 public void flags(final Boolean... flags) { 175 176 this.flags.clear(); 177 this.flags.addAll(Arrays.asList(flags)); 178 } 179 180 public List<String> strings() { 181 182 return strings; 183 } 184 185 public void strings(final List<String> strings) { 186 187 this.strings.clear(); 188 this.strings.addAll(strings); 189 } 190 191 public void strings(final String... strings) { 192 193 this.strings.clear(); 194 this.strings.addAll(Arrays.asList(strings)); 195 } 196}