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.BundleContents;
023import net.tnemc.item.AbstractItemStack;
024import net.tnemc.item.component.impl.BundleComponent;
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 * PaperOldBundleComponent
036 *
037 * @author creatorfromhell
038 * @since 0.2.0.0
039 */
040public class PaperBundleComponent extends BundleComponent<PaperItemStack, ItemStack> {
041
042  public PaperBundleComponent() {
043
044  }
045
046  public PaperBundleComponent(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<PaperBundleComponent> componentOptional = serialized.component(identifier());
074    if(componentOptional.isEmpty()) {
075      return item;
076    }
077
078    final BundleContents.Builder builder = BundleContents.bundleContents();
079
080    componentOptional.get().items.forEach((slot, stack)->builder.add(stack.provider().locale(serialized)));
081    item.setData(DataComponentTypes.BUNDLE_CONTENTS, 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 BundleContents contents = item.getData(DataComponentTypes.BUNDLE_CONTENTS);
097    if(contents == null) {
098      return serialized;
099    }
100
101    int i = 0;
102    for(final ItemStack stack : contents.contents()) {
103
104      if(stack.getType().equals(Material.AIR)) {
105        continue;
106      }
107
108      final PaperItemStack containerSerial = new PaperItemStack().of(stack);
109      PaperItemPlatform.instance().providerApplies(containerSerial, stack);
110      items.put(i, containerSerial);
111      i++;
112    }
113
114    serialized.applyComponent(this);
115    return serialized;
116  }
117
118  /**
119   * Checks if this component applies to the specified item.
120   *
121   * @param item The item to check against.
122   *
123   * @return True if this component applies to the item, false otherwise.
124   * @since 0.2.0.0
125   */
126  @Override
127  public boolean appliesTo(final ItemStack item) {
128
129    return true;
130  }
131}