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.component.helper.effect.EffectInstance; 025import net.tnemc.item.platform.ItemPlatform; 026import org.json.simple.JSONArray; 027import org.json.simple.JSONObject; 028 029import java.util.ArrayList; 030import java.util.Arrays; 031import java.util.List; 032import java.util.Objects; 033 034/** 035 * PotionContentsComponent 036 * 037 * @author creatorfromhell 038 * @see <a href="https://minecraft.wiki/w/Data_component_format#">Reference</a> 039 * <p> 040 * @since 0.2.0.0 041 */ 042public abstract class PotionContentsComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> { 043 044 protected final List<EffectInstance> customEffects = new ArrayList<>(); 045 046 protected String potionId; 047 protected int customColor; 048 protected String customName; 049 050 public PotionContentsComponent() { 051 052 } 053 054 public PotionContentsComponent(final String potionId) { 055 056 this.potionId = potionId; 057 } 058 059 public PotionContentsComponent(final String potionId, final int customColor) { 060 061 this.potionId = potionId; 062 this.customColor = customColor; 063 } 064 065 public PotionContentsComponent(final String potionId, final int customColor, final String customName) { 066 067 this.potionId = potionId; 068 this.customColor = customColor; 069 this.customName = customName; 070 } 071 072 public PotionContentsComponent(final String potionId, final int customColor, final String customName, final List<EffectInstance> effects) { 073 074 this.potionId = potionId; 075 this.customColor = customColor; 076 this.customName = customName; 077 078 this.customEffects.addAll(effects); 079 } 080 081 public PotionContentsComponent(final String potionId, final int customColor, final String customName, final EffectInstance... effects) { 082 083 this.potionId = potionId; 084 this.customColor = customColor; 085 this.customName = customName; 086 087 this.customEffects.addAll(Arrays.asList(effects)); 088 } 089 090 @Override 091 public String identifier() { 092 return "potion_contents"; 093 } 094 095 @Override 096 public JSONObject toJSON() { 097 final JSONObject json = new JSONObject(); 098 json.put("potion", potionId); 099 json.put("custom_color", customColor); 100 json.put("custom_name", customName); 101 102 final JSONArray effectsArray = new JSONArray(); 103 for (final EffectInstance effect : customEffects) { 104 effectsArray.add(effect.toJSON()); 105 } 106 json.put("custom_effects", effectsArray); 107 return json; 108 } 109 110 @Override 111 public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) { 112 potionId = json.getString("potion"); 113 customColor = json.getInteger("custom_color"); 114 customName = json.getString("custom_name"); 115 116 customEffects.clear(); 117 final JSONArray effectsArray = (JSONArray) json.getObject().get("custom_effects"); 118 for (final Object obj : effectsArray) { 119 final EffectInstance effect = new EffectInstance(); 120 effect.readJSON(new JSONHelper((JSONObject) obj)); 121 customEffects.add(effect); 122 } 123 } 124 125 @Override 126 public boolean similar(final SerialComponent<?, ?> component) { 127 if(!(component instanceof final PotionContentsComponent<?, ?> other)) return false; 128 return Objects.equals(this.potionId, other.potionId) && 129 this.customColor == other.customColor && 130 Objects.equals(this.customName, other.customName) && 131 Objects.equals(this.customEffects, other.customEffects); 132 } 133 134 @Override 135 public int hashCode() { 136 return Objects.hash(potionId, customColor, customName, customEffects); 137 } 138 139 public String potionId() { 140 141 return potionId; 142 } 143 144 public void potionId(final String potionId) { 145 146 this.potionId = potionId; 147 } 148 149 public int customColor() { 150 151 return customColor; 152 } 153 154 public void customColor(final int customColor) { 155 156 this.customColor = customColor; 157 } 158 159 public String customName() { 160 161 return customName; 162 } 163 164 public void customName(final String customName) { 165 166 this.customName = customName; 167 } 168 169 public List<EffectInstance> customEffects() { 170 171 return customEffects; 172 } 173 174 public void customEffects(final List<EffectInstance> effects) { 175 176 this.customEffects.clear(); 177 this.customEffects.addAll(effects); 178 } 179 180 public void customEffects(final EffectInstance... effects) { 181 182 this.customEffects.clear(); 183 this.customEffects.addAll(Arrays.asList(effects)); 184 } 185}