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 net.tnemc.item.providers.SkullProfile; 026import org.json.simple.JSONObject; 027 028import java.util.Objects; 029import java.util.UUID; 030 031/** 032 * ProfileComponent 033 * 034 * @author creatorfromhell 035 * @see <a href="https://minecraft.wiki/w/Data_component_format#">Reference</a> 036 * <p> 037 * @since 0.2.0.0 038 */ 039public abstract class ProfileComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> { 040 041 protected SkullProfile profile; 042 043 public ProfileComponent() { 044 045 } 046 047 public ProfileComponent(final SkullProfile profile) { 048 049 this.profile = profile; 050 } 051 052 /** 053 * @return the type of component this is. 054 * @since 0.2.0.0 055 */ 056 @Override 057 public String identifier() { 058 059 return "profile"; 060 } 061 062 /** 063 * Converts the {@link SerialComponent} to a JSON object. 064 * 065 * @return The JSONObject representing this {@link SerialComponent}. 066 * @since 0.2.0.0 067 */ 068 @Override 069 public JSONObject toJSON() { 070 071 final JSONObject json = new JSONObject(); 072 if(profile != null) { 073 if(profile.getName() != null) json.put("name", profile.getName()); 074 if(profile.getUuid() != null) json.put("uuid", profile.getUuid()); 075 if(profile.getTexture() != null) json.put("texture", profile.getTexture()); 076 } 077 return json; 078 } 079 080 /** 081 * Reads JSON data and converts it back to a {@link SerialComponent} object. 082 * 083 * @param json The JSONHelper instance of the json data. 084 * @since 0.2.0.0 085 */ 086 @Override 087 public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) { 088 089 profile = new SkullProfile(); 090 if(json.has("name")) profile.setName(json.getString("name")); 091 092 try { 093 094 if(json.has("uuid")) profile.setUuid(UUID.fromString(json.getString("uuid"))); 095 096 } catch(final Exception ignore) { } 097 098 if(json.has("texture")) profile.setTexture(json.getString("texture")); 099 } 100 101 /** 102 * Used to determine if some data is equal to this data. This means that it has to be an exact 103 * copy of this data. For instance, book copies will return false when compared to the original. 104 * 105 * @param component The component to compare. 106 * 107 * @return True if similar, otherwise false. 108 * @since 0.2.0.0 109 */ 110 @Override 111 public boolean similar(final SerialComponent<?, ?> component) { 112 113 if(!(component instanceof final ProfileComponent<?, ?> other)) return false; 114 if(profile != null) { 115 116 if(other.profile == null) return false; 117 118 return profile.equals(other.profile); 119 } 120 121 return other.profile == null; 122 } 123 124 @Override 125 public int hashCode() { 126 127 return Objects.hash(false); 128 } 129 130 public SkullProfile profile() { 131 132 return profile; 133 } 134 135 public void profile(final SkullProfile profile) { 136 137 this.profile = profile; 138 } 139 140 public void profile(final String name, final UUID uuid) { 141 142 this.profile = new SkullProfile(name, uuid); 143 } 144 145 public void profile(final String name, final UUID uuid, final String texture) { 146 147 this.profile = new SkullProfile(name, uuid, texture); 148 } 149}