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.HashMap; 028import java.util.Map; 029import java.util.Objects; 030import java.util.Optional; 031 032/** 033 * ContainerComponent - The items contained in this container. 034 * 035 * @author creatorfromhell 036 * @since 0.2.0.0 037 */ 038public abstract class ContainerComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> { 039 040 protected final Map<Integer, AbstractItemStack<T>> items = new HashMap<>(); 041 042 public ContainerComponent() { 043 044 } 045 046 public ContainerComponent(final Map<Integer, AbstractItemStack<T>> items) { 047 048 this.items.putAll(items); 049 } 050 051 @Override 052 public String identifier() { 053 return "container"; 054 } 055 056 @Override 057 public JSONObject toJSON() { 058 final JSONObject itemsObj = new JSONObject(); 059 060 items.forEach((slot, item)->{ 061 itemsObj.put(slot, item.toJSON()); 062 }); 063 return itemsObj; 064 } 065 066 @Override 067 public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) { 068 069 items.clear(); 070 json.getJSON("items").forEach((key, value)->{ 071 final int slot = Integer.parseInt(String.valueOf(key)); 072 073 final Optional<I> serialized = platform.initSerialized((JSONObject)value); 074 serialized.ifPresent(i->items.put(slot, i)); 075 }); 076 } 077 078 @Override 079 public boolean similar(final SerialComponent<?, ?> component) { 080 if(component instanceof final ContainerComponent<?, ?> compare) { 081 082 if(items.size() != compare.items.size()) return false; 083 084 for(final Map.Entry<Integer, AbstractItemStack<T>> entry : items.entrySet()) { 085 086 if(!compare.items.containsKey(entry.getKey())) return false; 087 088 final AbstractItemStack<? extends T> item = (AbstractItemStack<? extends T>)compare.items.get(entry.getKey()); 089 090 if(entry.getValue().amount() != item.amount()) return false; 091 092 if(!entry.getValue().provider().similar(entry.getValue(), item)) return false; 093 } 094 return true; 095 } 096 return false; 097 } 098 099 @Override 100 public int hashCode() { 101 return Objects.hash(items); 102 } 103 104 public Map<Integer, AbstractItemStack<T>> items() { 105 106 return items; 107 } 108}