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.ComponentEffect; 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 * DeathProtectionComponent 036 * @see <a href="https://minecraft.wiki/w/Data_component_format#death_protection">Reference</a> 037 * <p> 038 * @author creatorfromhell 039 * @since 0.2.0.0 040 */ 041public abstract class DeathProtectionComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> { 042 043 protected final List<ComponentEffect> deathEffects = new ArrayList<>(); 044 045 @Override 046 public String identifier() { 047 return "death_protection"; 048 } 049 050 @Override 051 public JSONObject toJSON() { 052 final JSONObject json = new JSONObject(); 053 054 final JSONArray effectsArray = new JSONArray(); 055 for (final ComponentEffect effect : deathEffects) { 056 effectsArray.add(effect.toJSON()); 057 } 058 json.put("death_effects", effectsArray); 059 060 return json; 061 } 062 063 @Override 064 public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) { 065 deathEffects.clear(); 066 067 if(json.has("death_effects")) { 068 final JSONArray effectsArray = (JSONArray) json.getObject().get("death_effects"); 069 070 for (final Object obj : effectsArray) { 071 final JSONObject effectJson = (JSONObject) obj; 072 final String type = effectJson.get("type").toString(); 073 074 // Get the effect class from the platform's reviveEffects map 075 final Class<? extends ComponentEffect> effectClass = platform.effects().get(type); 076 077 if(effectClass != null) { 078 try { 079 // Instantiate the effect dynamically 080 final ComponentEffect effect = effectClass.getDeclaredConstructor().newInstance(); 081 effect.readJSON(new JSONHelper(effectJson)); 082 deathEffects.add(effect); 083 } catch (final ReflectiveOperationException e) { 084 throw new RuntimeException("Failed to instantiate ComponentEffect for type: " + type, e); 085 } 086 } 087 } 088 } 089 } 090 091 @Override 092 public boolean similar(final SerialComponent<?, ?> component) { 093 if(!(component instanceof final DeathProtectionComponent<?, ?> other)) return false; 094 095 return Objects.equals(this.deathEffects, other.deathEffects); 096 } 097 098 @Override 099 public int hashCode() { 100 return Objects.hash(deathEffects); 101 } 102 103 /** 104 * Gets the list of death effects. 105 * 106 * @return A list of `ReviveEffect` objects. 107 * @since 0.2.0.0 108 */ 109 public List<ComponentEffect> deathEffects() { 110 return deathEffects; 111 } 112 113 /** 114 * Applies death effects to the component's list of effects. 115 * 116 * @param deathEffects The list of ComponentEffect to apply as death effects. 117 * @since 0.2.0.0 118 */ 119 public void deathEffects(final List<ComponentEffect> deathEffects) { 120 this.deathEffects.clear(); 121 this.deathEffects.addAll(deathEffects); 122 } 123 124 /** 125 * Adds one or more ComponentEffect objects to the list of death effects for this DeathProtectionComponent. 126 * 127 * @param effects One or more ComponentEffect objects to be added as death effects. 128 * @since 0.2.0.0 129 */ 130 public void deathEffect(final ComponentEffect... effects) { 131 this.deathEffects.addAll(Arrays.asList(effects)); 132 } 133}