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.FoodComponent;
022import net.tnemc.item.paper.PaperItemStack;
023import org.bukkit.inventory.ItemStack;
024import org.bukkit.inventory.meta.ItemMeta;
025
026import java.util.Optional;
027
028/**
029 * PaperOldFoodComponent
030 *
031 * @author creatorfromhell
032 * @since 0.2.0.0
033 */
034public class PaperOldFoodComponent extends FoodComponent<PaperItemStack, ItemStack> {
035
036  /**
037   * @param version the version being used when this check is called.
038   *
039   * @return true if this check is enabled for the version, otherwise false
040   * @since 0.2.0.0
041   */
042  @Override
043  public boolean enabled(final String version) {
044
045    //TODO: What version?
046    return true;
047  }
048
049  /**
050   * @param serialized the serialized item stack to use
051   * @param item       the item that we should use to apply this applicator to.
052   *
053   * @return the updated item.
054   * @since 0.2.0.0
055   */
056  @Override
057  public ItemStack apply(final PaperItemStack serialized, final ItemStack item) {
058
059    final Optional<PaperOldFoodComponent> componentOptional = serialized.component(identifier());
060    componentOptional.ifPresent(component->{
061
062      final ItemMeta meta = item.getItemMeta();
063
064      if(meta != null && meta.hasFood()) {
065
066        final org.bukkit.inventory.meta.components.FoodComponent bukkitComponent = meta.getFood();
067
068        bukkitComponent.setCanAlwaysEat(componentOptional.get().canAlwaysEat);
069        bukkitComponent.setSaturation(componentOptional.get().saturation);
070        bukkitComponent.setNutrition(componentOptional.get().nutrition);
071
072        meta.setFood(bukkitComponent);
073
074        item.setItemMeta(meta);
075      }
076    });
077    return item;
078  }
079
080  /**
081   * @param item       the item that we should use to deserialize.
082   * @param serialized the serialized item stack we should use to apply this deserializer to
083   *
084   * @return the updated serialized item.
085   * @since 0.2.0.0
086   */
087  @Override
088  public PaperItemStack serialize(final ItemStack item, final PaperItemStack serialized) {
089
090    final ItemMeta meta = item.getItemMeta();
091    if(meta != null && meta.hasFood()) {
092
093      this.canAlwaysEat = meta.getFood().canAlwaysEat();
094      this.saturation = meta.getFood().getSaturation();
095      this.nutrition = meta.getFood().getNutrition();
096    }
097
098    serialized.applyComponent(this);
099    return serialized;
100  }
101
102  /**
103   * Checks if this component applies to the specified item.
104   *
105   * @param item The item to check against.
106   *
107   * @return True if this component applies to the item, false otherwise.
108   * @since 0.2.0.0
109   */
110  @Override
111  public boolean appliesTo(final ItemStack item) {
112
113    return item.getItemMeta() != null && item.getItemMeta().hasFood();
114  }
115}