001 package net.minecraft.src;
002
003 import cpw.mods.fml.common.Side;
004 import cpw.mods.fml.common.asm.SideOnly;
005
006 public class FoodStats
007 {
008 /** The player's food level. */
009 private int foodLevel = 20;
010
011 /** The player's food saturation. */
012 private float foodSaturationLevel = 5.0F;
013
014 /** The player's food exhaustion. */
015 private float foodExhaustionLevel;
016
017 /** The player's food timer value. */
018 private int foodTimer = 0;
019 private int prevFoodLevel = 20;
020
021 /**
022 * Args: int foodLevel, float foodSaturationModifier
023 */
024 public void addStats(int par1, float par2)
025 {
026 this.foodLevel = Math.min(par1 + this.foodLevel, 20);
027 this.foodSaturationLevel = Math.min(this.foodSaturationLevel + (float)par1 * par2 * 2.0F, (float)this.foodLevel);
028 }
029
030 /**
031 * Eat some food.
032 */
033 public void addStats(ItemFood par1ItemFood)
034 {
035 this.addStats(par1ItemFood.getHealAmount(), par1ItemFood.getSaturationModifier());
036 }
037
038 /**
039 * Handles the food game logic.
040 */
041 public void onUpdate(EntityPlayer par1EntityPlayer)
042 {
043 int var2 = par1EntityPlayer.worldObj.difficultySetting;
044 this.prevFoodLevel = this.foodLevel;
045
046 if (this.foodExhaustionLevel > 4.0F)
047 {
048 this.foodExhaustionLevel -= 4.0F;
049
050 if (this.foodSaturationLevel > 0.0F)
051 {
052 this.foodSaturationLevel = Math.max(this.foodSaturationLevel - 1.0F, 0.0F);
053 }
054 else if (var2 > 0)
055 {
056 this.foodLevel = Math.max(this.foodLevel - 1, 0);
057 }
058 }
059
060 if (this.foodLevel >= 18 && par1EntityPlayer.shouldHeal())
061 {
062 ++this.foodTimer;
063
064 if (this.foodTimer >= 80)
065 {
066 par1EntityPlayer.heal(1);
067 this.foodTimer = 0;
068 }
069 }
070 else if (this.foodLevel <= 0)
071 {
072 ++this.foodTimer;
073
074 if (this.foodTimer >= 80)
075 {
076 if (par1EntityPlayer.getHealth() > 10 || var2 >= 3 || par1EntityPlayer.getHealth() > 1 && var2 >= 2)
077 {
078 par1EntityPlayer.attackEntityFrom(DamageSource.starve, 1);
079 }
080
081 this.foodTimer = 0;
082 }
083 }
084 else
085 {
086 this.foodTimer = 0;
087 }
088 }
089
090 /**
091 * Reads food stats from an NBT object.
092 */
093 public void readNBT(NBTTagCompound par1NBTTagCompound)
094 {
095 if (par1NBTTagCompound.hasKey("foodLevel"))
096 {
097 this.foodLevel = par1NBTTagCompound.getInteger("foodLevel");
098 this.foodTimer = par1NBTTagCompound.getInteger("foodTickTimer");
099 this.foodSaturationLevel = par1NBTTagCompound.getFloat("foodSaturationLevel");
100 this.foodExhaustionLevel = par1NBTTagCompound.getFloat("foodExhaustionLevel");
101 }
102 }
103
104 /**
105 * Writes food stats to an NBT object.
106 */
107 public void writeNBT(NBTTagCompound par1NBTTagCompound)
108 {
109 par1NBTTagCompound.setInteger("foodLevel", this.foodLevel);
110 par1NBTTagCompound.setInteger("foodTickTimer", this.foodTimer);
111 par1NBTTagCompound.setFloat("foodSaturationLevel", this.foodSaturationLevel);
112 par1NBTTagCompound.setFloat("foodExhaustionLevel", this.foodExhaustionLevel);
113 }
114
115 /**
116 * Get the player's food level.
117 */
118 public int getFoodLevel()
119 {
120 return this.foodLevel;
121 }
122
123 @SideOnly(Side.CLIENT)
124 public int getPrevFoodLevel()
125 {
126 return this.prevFoodLevel;
127 }
128
129 /**
130 * If foodLevel is not max.
131 */
132 public boolean needFood()
133 {
134 return this.foodLevel < 20;
135 }
136
137 /**
138 * adds input to foodExhaustionLevel to a max of 40
139 */
140 public void addExhaustion(float par1)
141 {
142 this.foodExhaustionLevel = Math.min(this.foodExhaustionLevel + par1, 40.0F);
143 }
144
145 /**
146 * Get the player's food saturation level.
147 */
148 public float getSaturationLevel()
149 {
150 return this.foodSaturationLevel;
151 }
152
153 @SideOnly(Side.CLIENT)
154 public void setFoodLevel(int par1)
155 {
156 this.foodLevel = par1;
157 }
158
159 @SideOnly(Side.CLIENT)
160 public void setFoodSaturationLevel(float par1)
161 {
162 this.foodSaturationLevel = par1;
163 }
164 }