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.helper.effect.EffectInstance;
022import net.tnemc.item.component.impl.SuspiciousStewEffectsComponent;
023import net.tnemc.item.paper.PaperItemStack;
024import net.tnemc.item.paper.platform.PaperItemPlatform;
025import net.tnemc.item.providers.VersionUtil;
026import org.bukkit.inventory.ItemStack;
027import org.bukkit.inventory.meta.SuspiciousStewMeta;
028import org.bukkit.potion.PotionEffect;
029import org.bukkit.potion.PotionEffectType;
030
031import java.util.Optional;
032
033/**
034 * PaperOldSuspiciousStewEffectsComponent
035 *
036 * @author creatorfromhell
037 * @since 0.2.0.0
038 */
039public class PaperOldSuspiciousStewEffectsComponent extends SuspiciousStewEffectsComponent<PaperItemStack, ItemStack> {
040
041  /**
042   * @param version the version being used when this check is called.
043   *
044   * @return true if this check is enabled for the version, otherwise false
045   * @since 0.2.0.0
046   */
047  @Override
048  public boolean enabled(final String version) {
049
050    return VersionUtil.isOneFifteen(version);
051  }
052
053  /**
054   * @param serialized the serialized item stack to use
055   * @param item       the item that we should use to apply this applicator to.
056   *
057   * @return the updated item.
058   * @since 0.2.0.0
059   */
060  @Override
061  public ItemStack apply(final PaperItemStack serialized, final ItemStack item) {
062
063    final Optional<PaperOldSuspiciousStewEffectsComponent> componentOptional = serialized.component(identifier());
064    componentOptional.ifPresent(component->{
065
066      if(item.hasItemMeta() && item.getItemMeta() instanceof final SuspiciousStewMeta meta) {
067
068        effects.forEach((effect)->{
069
070          try {
071
072            final PotionEffectType effectType = PaperItemPlatform.instance().converter().convert(effect.id(), PotionEffectType.class);
073            if(effectType != null) {
074
075              meta.addCustomEffect(new PotionEffect(effectType,
076                                                    effect.duration(),
077                                                    effect.amplifier(),
078                                                    effect.ambient(),
079                                                    effect.showParticles(),
080                                                    effect.showIcon()), true);
081            }
082          } catch(final Exception ignore) {}
083        });
084
085        item.setItemMeta(meta);
086      }
087    });
088    return item;
089  }
090
091  /**
092   * @param item       the item that we should use to deserialize.
093   * @param serialized the serialized item stack we should use to apply this deserializer to
094   *
095   * @return the updated serialized item.
096   * @since 0.2.0.0
097   */
098  @Override
099  public PaperItemStack serialize(final ItemStack item, final PaperItemStack serialized) {
100
101    if(item.hasItemMeta() && item.getItemMeta() instanceof final SuspiciousStewMeta meta) {
102
103      for(final PotionEffect effect : meta.getCustomEffects()) {
104
105        try {
106          final String id = PaperItemPlatform.instance().converter().convert(effect.getType(), String.class);
107          if(id != null) {
108
109            effects.add(new EffectInstance(id,
110                                           effect.getAmplifier(),
111                                           effect.getDuration(),
112                                           effect.hasParticles(),
113                                           effect.isAmbient(),
114                                           effect.hasIcon()));
115
116          }
117        } catch(final Exception ignore) {}
118      }
119    }
120
121    serialized.applyComponent(this);
122    return serialized;
123  }
124
125  /**
126   * Checks if this component applies to the specified item.
127   *
128   * @param item The item to check against.
129   *
130   * @return True if this component applies to the item, false otherwise.
131   * @since 0.2.0.0
132   */
133  @Override
134  public boolean appliesTo(final ItemStack item) {
135
136    return item.hasItemMeta() && item.getItemMeta() instanceof SuspiciousStewMeta;
137  }
138}