001package net.tnemc.item.bukkit.platform.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.bukkit.BukkitItemStack; 023import net.tnemc.item.bukkit.platform.BukkitItemPlatform; 024import net.tnemc.item.component.impl.ContainerComponent; 025import org.bukkit.Material; 026import org.bukkit.block.Container; 027import org.bukkit.inventory.Inventory; 028import org.bukkit.inventory.ItemStack; 029import org.bukkit.inventory.meta.BlockStateMeta; 030 031import java.util.Map; 032import java.util.Optional; 033 034/** 035 * BukkitContainerComponent 036 * 037 * @author creatorfromhell 038 * @since 0.2.0.0 039 */ 040public class BukkitContainerComponent extends ContainerComponent<BukkitItemStack, ItemStack> { 041 042 public BukkitContainerComponent() { 043 044 } 045 046 public BukkitContainerComponent(final Map<Integer, AbstractItemStack<ItemStack>> items) { 047 048 super(items); 049 } 050 051 /** 052 * @param version the version being used when this check is called. 053 * 054 * @return true if this check is enabled for the version, otherwise false 055 * @since 0.2.0.0 056 */ 057 @Override 058 public boolean enabled(final String version) { 059 060 return true; 061 } 062 063 /** 064 * @param serialized the serialized item stack to use 065 * @param item the item that we should use to apply this applicator to. 066 * 067 * @return the updated item. 068 * @since 0.2.0.0 069 */ 070 @Override 071 public ItemStack apply(final BukkitItemStack serialized, final ItemStack item) { 072 073 final Optional<BukkitContainerComponent> componentOptional = serialized.component(identifier()); 074 componentOptional.ifPresent(component->{ 075 076 if(item.hasItemMeta() && item.getItemMeta() instanceof final BlockStateMeta meta 077 && meta.hasBlockState() && meta.getBlockState() instanceof final Container container) { 078 079 componentOptional.get().items.forEach((slot, stack)->container.getInventory().setItem(slot, stack.provider().locale(stack))); 080 container.update(true); 081 meta.setBlockState(container); 082 083 item.setItemMeta(meta); 084 } 085 }); 086 return item; 087 } 088 089 /** 090 * @param item the item that we should use to deserialize. 091 * @param serialized the serialized item stack we should use to apply this deserializer to 092 * 093 * @return the updated serialized item. 094 * @since 0.2.0.0 095 */ 096 @Override 097 public BukkitItemStack serialize(final ItemStack item, final BukkitItemStack serialized) { 098 099 if(item.hasItemMeta() && item.getItemMeta() instanceof final BlockStateMeta meta 100 && meta.hasBlockState() && meta.getBlockState() instanceof final Container container) { 101 102 final Inventory inventory = container.getInventory(); 103 for(int i = 0; i < inventory.getSize(); i++) { 104 105 final ItemStack stack = inventory.getItem(i); 106 if(stack == null) { 107 continue; 108 } 109 110 if(stack.getType().equals(Material.AIR)) { 111 continue; 112 } 113 114 final BukkitItemStack containerSerial = new BukkitItemStack().of(stack); 115 BukkitItemPlatform.instance().providerApplies(containerSerial, stack); 116 items.put(i, containerSerial); 117 } 118 } 119 120 serialized.applyComponent(this); 121 return serialized; 122 } 123 124 /** 125 * Checks if this component applies to the specified item. 126 * 127 * @param item The item to check against. 128 * 129 * @return True if this component applies to the item, false otherwise. 130 * @since 0.2.0.0 131 */ 132 @Override 133 public boolean appliesTo(final ItemStack item) { 134 135 return item.hasItemMeta() && item.getItemMeta() instanceof final BlockStateMeta meta 136 && meta.hasBlockState() && meta.getBlockState() instanceof Container; 137 } 138}