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 * EntityVariantComponent - Holds information about the entity variant that should be carried over 031 * for items such as mob bucket, spawn egg, etc. Added in MC 1.21.5 032 * 033 * @author creatorfromhell 034 * @see <a href="https://minecraft.wiki/w/Data_component_format#">Reference</a> 035 * <p> 036 * @since 0.2.0.0 037 */ 038public abstract class EntityVariantComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> { 039 040 protected String entity = ""; 041 protected String variant = ""; 042 043 public EntityVariantComponent() { 044 045 } 046 047 public EntityVariantComponent(final String entity, final String variant) { 048 049 this.entity = entity; 050 this.variant = variant; 051 } 052 053 /** 054 * @return the type of component this is. 055 * @since 0.2.0.0 056 */ 057 @Override 058 public String identifier() { 059 060 return "entity_variant"; 061 } 062 063 /** 064 * Converts the {@link SerialComponent} to a JSON object. 065 * 066 * @return The JSONObject representing this {@link SerialComponent}. 067 * @since 0.2.0.0 068 */ 069 @Override 070 public JSONObject toJSON() { 071 072 final JSONObject json = new JSONObject(); 073 074 json.put("entity", entity); 075 json.put("variant", variant); 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 this.entity = json.getString("entity"); 090 this.variant = json.getString("variant"); 091 } 092 093 /** 094 * Used to determine if some data is equal to this data. This means that it has to be an exact 095 * copy of this data. For instance, book copies will return false when compared to the original. 096 * 097 * @param component The component to compare. 098 * 099 * @return True if similar, otherwise false. 100 * @since 0.2.0.0 101 */ 102 @Override 103 public boolean similar(final SerialComponent<?, ?> component) { 104 105 if(!(component instanceof final EntityVariantComponent<?, ?> other)) return false; 106 107 return entity.equals(other.entity) && variant.equals(other.variant); 108 } 109 110 @Override 111 public int hashCode() { 112 113 return Objects.hash(entity, variant); 114 } 115 116 public String entity() { 117 118 return entity; 119 } 120 121 public void entity(final String entity) { 122 123 this.entity = entity; 124 } 125 126 public String variant() { 127 128 return variant; 129 } 130 131 public void variant(final String variant) { 132 133 this.variant = variant; 134 } 135}