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