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