001package net.tnemc.item.component.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.AbstractItemStack; 022import net.tnemc.item.JSONHelper; 023import net.tnemc.item.component.SerialComponent; 024import net.tnemc.item.platform.ItemPlatform; 025import org.json.simple.JSONObject; 026 027/** 028 * FoodComponent - The food stats for this consumable item. Has no effect unless the item can be 029 * consumed (i.e. the item has the consumable component). 030 * 031 * @author creatorfromhell 032 * @since 0.0.1.0 033 */ 034public abstract class FoodComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> { 035 036 protected boolean canAlwaysEat = false; 037 protected float saturation; 038 protected int nutrition; 039 040 public FoodComponent() { 041 042 } 043 044 public FoodComponent(final boolean canAlwaysEat) { 045 046 this.canAlwaysEat = canAlwaysEat; 047 } 048 049 public FoodComponent(final boolean canAlwaysEat, final float saturation) { 050 051 this.canAlwaysEat = canAlwaysEat; 052 this.saturation = saturation; 053 } 054 055 public FoodComponent(final boolean canAlwaysEat, final float saturation, final int nutrition) { 056 057 this.canAlwaysEat = canAlwaysEat; 058 this.saturation = saturation; 059 this.nutrition = nutrition; 060 } 061 062 /** 063 * @return the type of component this is. 064 * @since 0.2.0.0 065 */ 066 @Override 067 public String identifier() { 068 069 return "food"; 070 } 071 072 /** 073 * Converts the {@link SerialComponent} to a JSON object. 074 * 075 * @return The JSONObject representing this {@link SerialComponent}. 076 * @since 0.2.0.0 077 */ 078 @Override 079 public JSONObject toJSON() { 080 081 final JSONObject food = new JSONObject(); 082 food.put("name", "food-component"); 083 food.put("canAlwaysEat", canAlwaysEat); 084 food.put("saturation", saturation); 085 food.put("nutrition", nutrition); 086 return food; 087 } 088 089 /** 090 * Reads JSON data and converts it back to a {@link SerialComponent} object. 091 * 092 * @param json The JSONHelper instance of the json data. 093 * @since 0.2.0.0 094 */ 095 @Override 096 public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) { 097 098 canAlwaysEat = json.getBoolean("canAlwaysEat"); 099 saturation = json.getFloat("saturation"); 100 nutrition = json.getInteger("nutrition"); 101 } 102 103 /** 104 * Used to determine if some data is equal to this data. This means that it has to be an exact 105 * copy of this data. For instance, book copies will return false when compared to the original. 106 * 107 * @param component The component to compare. 108 * 109 * @return True if similar, otherwise false. 110 * @since 0.2.0.0 111 */ 112 @Override 113 public boolean similar(final SerialComponent<?, ?> component) { 114 115 if(component instanceof final FoodComponent<?, ?> food) { 116 117 return this.canAlwaysEat == food.canAlwaysEat && 118 Float.compare(this.saturation, food.saturation) == 0 && 119 this.nutrition == food.nutrition; 120 } 121 return false; 122 } 123 124 public boolean canAlwaysEat() { 125 126 return canAlwaysEat; 127 } 128 129 public void canAlwaysEat(final boolean canAlwaysEat) { 130 131 this.canAlwaysEat = canAlwaysEat; 132 } 133 134 public float saturation() { 135 136 return saturation; 137 } 138 139 public void saturation(final float saturation) { 140 141 this.saturation = saturation; 142 } 143 144 public int nutrition() { 145 146 return nutrition; 147 } 148 149 public void nutrition(final int nutrition) { 150 151 this.nutrition = nutrition; 152 } 153}