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