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