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.JSONArray; 026import org.json.simple.JSONObject; 027 028import java.util.HashMap; 029import java.util.Map; 030import java.util.Objects; 031 032/** 033 * EnchantmentsComponent - Can contain either the following fields, or key-value pairs of levels of enchantments. 034 * 035 * @see <a href="https://minecraft.wiki/w/Data_component_format#enchantments">Reference</a> 036 * @author creatorfromhell 037 * @since 0.2.0.0 038 */ 039public abstract class EnchantmentsComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> { 040 041 protected final Map<String, Integer> levels = new HashMap<>(); 042 043 public EnchantmentsComponent() { 044 045 } 046 047 public EnchantmentsComponent(final Map<String, Integer> levels) { 048 049 this.levels.putAll(levels); 050 } 051 052 @Override 053 public String identifier() { 054 return "enchantments"; 055 } 056 057 @Override 058 public JSONObject toJSON() { 059 final JSONObject json = new JSONObject(); 060 061 final JSONArray enchantmentsArray = new JSONArray(); 062 for(final Map.Entry<String, Integer> entry : levels.entrySet()) { 063 064 final JSONObject enchantmentJson = new JSONObject(); 065 enchantmentJson.put("id", entry.getKey()); 066 enchantmentJson.put("level", entry.getValue()); 067 enchantmentsArray.add(enchantmentJson); 068 } 069 json.put("enchantments", enchantmentsArray); 070 071 return json; 072 } 073 074 @Override 075 public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) { 076 levels.clear(); 077 078 final JSONArray enchantmentsArray = (JSONArray) json.getObject().get("enchantments"); 079 if(enchantmentsArray != null) { 080 for(final Object obj : enchantmentsArray) { 081 final JSONObject enchantmentJson = (JSONObject) obj; 082 083 final String id = enchantmentJson.get("id").toString(); 084 final int level = Integer.parseInt(enchantmentJson.get("level").toString()); 085 levels.put(id, level); 086 } 087 } 088 } 089 090 @Override 091 public boolean similar(final SerialComponent<?, ?> component) { 092 if(!(component instanceof final EnchantmentsComponent<?, ?> other)) return false; 093 094 return Objects.equals(this.levels, other.levels); 095 } 096 097 @Override 098 public int hashCode() { 099 return Objects.hash(levels); 100 } 101 102 public Map<String, Integer> levels() { 103 104 return levels; 105 } 106 107 public void levels(final Map<String, Integer> levels) { 108 109 this.levels.clear(); 110 this.levels.putAll(levels); 111 } 112}