001package net.tnemc.item.paper.platform.impl.modern; 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 io.papermc.paper.datacomponent.DataComponentTypes; 022import io.papermc.paper.datacomponent.item.ItemContainerContents; 023import net.tnemc.item.AbstractItemStack; 024import net.tnemc.item.component.impl.ContainerComponent; 025import net.tnemc.item.paper.PaperItemStack; 026import net.tnemc.item.paper.platform.PaperItemPlatform; 027import net.tnemc.item.providers.VersionUtil; 028import org.bukkit.Material; 029import org.bukkit.inventory.ItemStack; 030 031import java.util.Map; 032import java.util.Optional; 033 034/** 035 * PaperOldContainerComponent 036 * 037 * @author creatorfromhell 038 * @since 0.2.0.0 039 */ 040public class PaperContainerComponent extends ContainerComponent<PaperItemStack, ItemStack> { 041 042 public PaperContainerComponent() { 043 044 } 045 046 public PaperContainerComponent(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 VersionUtil.isOneTwentyOneFour(version); 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 PaperItemStack serialized, final ItemStack item) { 072 073 final Optional<PaperContainerComponent> componentOptional = serialized.component(identifier()); 074 if(componentOptional.isEmpty()) { 075 return item; 076 } 077 078 final ItemContainerContents.Builder builder = ItemContainerContents.containerContents(); 079 080 componentOptional.get().items().forEach((slot, stack)->builder.add(stack.provider().locale(serialized))); 081 item.setData(DataComponentTypes.CONTAINER, builder); 082 083 return item; 084 } 085 086 /** 087 * @param item the item that we should use to deserialize. 088 * @param serialized the serialized item stack we should use to apply this deserializer to 089 * 090 * @return the updated serialized item. 091 * @since 0.2.0.0 092 */ 093 @Override 094 public PaperItemStack serialize(final ItemStack item, final PaperItemStack serialized) { 095 096 final ItemContainerContents contents = item.getData(DataComponentTypes.CONTAINER); 097 if(contents == null) { 098 return serialized; 099 } 100 101 int i = 0; 102 for(final ItemStack stack : contents.contents()) { 103 104 if(stack == null) { 105 continue; 106 } 107 108 if(stack.getType().equals(Material.AIR)) { 109 continue; 110 } 111 112 final PaperItemStack containerSerial = new PaperItemStack().of(stack); 113 PaperItemPlatform.instance().providerApplies(containerSerial, stack); 114 items.put(i, containerSerial); 115 i++; 116 } 117 118 serialized.applyComponent(this); 119 return serialized; 120 } 121 122 /** 123 * Checks if this component applies to the specified item. 124 * 125 * @param item The item to check against. 126 * 127 * @return True if this component applies to the item, false otherwise. 128 * @since 0.2.0.0 129 */ 130 @Override 131 public boolean appliesTo(final ItemStack item) { 132 133 return true; 134 } 135}