| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import net.minecraft.entity.player.EntityPlayer;
|
| 4 | import net.minecraft.item.ItemFood;
|
| 5 | import net.minecraft.item.ItemStack;
|
| 6 | import net.minecraft.nbt.NBTTagCompound;
|
| 7 | import net.minecraft.world.EnumDifficulty;
|
| 8 |
|
| 9 | public class FoodStats
|
| 10 | {
|
| 11 | /** The player's food level. */
|
| 12 | private int foodLevel = 20;
|
| 13 |
|
| 14 | /** The player's food saturation. */
|
| 15 | private float foodSaturationLevel = 5.0F;
|
| 16 |
|
| 17 | /** The player's food exhaustion. */
|
| 18 | private float foodExhaustionLevel;
|
| 19 |
|
| 20 | /** The player's food timer value. */
|
| 21 | private int foodTimer;
|
| 22 | private int prevFoodLevel = 20;
|
| 23 |
|
| 24 | /**
|
| 25 | * Add food stats.
|
| 26 | */
|
| 27 | public void addStats(int foodLevelIn, float foodSaturationModifier)
|
| 28 | {
|
| 29 | this.foodLevel = Math.min(foodLevelIn + this.foodLevel, 20);
|
| 30 | this.foodSaturationLevel = Math.min(this.foodSaturationLevel + (float)foodLevelIn * foodSaturationModifier * 2.0F, (float)this.foodLevel);
|
| 31 | }
|
| 32 |
|
| 33 | public void addStats(ItemFood foodItem, ItemStack p_151686_2_)
|
| 34 | {
|
| 35 | this.addStats(foodItem.getHealAmount(p_151686_2_), foodItem.getSaturationModifier(p_151686_2_));
|
| 36 | }
|
| 37 |
|
| 38 | /**
|
| 39 | * Handles the food game logic.
|
| 40 | */
|
| 41 | public void onUpdate(EntityPlayer player)
|
| 42 | {
|
| 43 | EnumDifficulty enumdifficulty = player.worldObj.getDifficulty();
|
| 44 | this.prevFoodLevel = this.foodLevel;
|
| 45 |
|
| 46 | if (this.foodExhaustionLevel > 4.0F)
|
| 47 | {
|
| 48 | this.foodExhaustionLevel -= 4.0F;
|
| 49 |
|
| 50 | if (this.foodSaturationLevel > 0.0F)
|
| 51 | {
|
| 52 | this.foodSaturationLevel = Math.max(this.foodSaturationLevel - 1.0F, 0.0F);
|
| 53 | }
|
| 54 | else if (enumdifficulty != EnumDifficulty.PEACEFUL)
|
| 55 | {
|
| 56 | this.foodLevel = Math.max(this.foodLevel - 1, 0);
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | if (player.worldObj.getGameRules().getBoolean("naturalRegeneration") && this.foodLevel >= 18 && player.shouldHeal())
|
| 61 | {
|
| 62 | ++this.foodTimer;
|
| 63 |
|
| 64 | if (this.foodTimer >= 80)
|
| 65 | {
|
| 66 | player.heal(1.0F);
|
| 67 | this.addExhaustion(3.0F);
|
| 68 | this.foodTimer = 0;
|
| 69 | }
|
| 70 | }
|
| 71 | else if (this.foodLevel <= 0)
|
| 72 | {
|
| 73 | ++this.foodTimer;
|
| 74 |
|
| 75 | if (this.foodTimer >= 80)
|
| 76 | {
|
| 77 | if (player.getHealth() > 10.0F || enumdifficulty == EnumDifficulty.HARD || player.getHealth() > 1.0F && enumdifficulty == EnumDifficulty.NORMAL)
|
| 78 | {
|
| 79 | player.attackEntityFrom(DamageSource.starve, 1.0F);
|
| 80 | }
|
| 81 |
|
| 82 | this.foodTimer = 0;
|
| 83 | }
|
| 84 | }
|
| 85 | else
|
| 86 | {
|
| 87 | this.foodTimer = 0;
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | /**
|
| 92 | * Reads the food data for the player.
|
| 93 | */
|
| 94 | public void readNBT(NBTTagCompound p_75112_1_)
|
| 95 | {
|
| 96 | if (p_75112_1_.hasKey("foodLevel", 99))
|
| 97 | {
|
| 98 | this.foodLevel = p_75112_1_.getInteger("foodLevel");
|
| 99 | this.foodTimer = p_75112_1_.getInteger("foodTickTimer");
|
| 100 | this.foodSaturationLevel = p_75112_1_.getFloat("foodSaturationLevel");
|
| 101 | this.foodExhaustionLevel = p_75112_1_.getFloat("foodExhaustionLevel");
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | /**
|
| 106 | * Writes the food data for the player.
|
| 107 | */
|
| 108 | public void writeNBT(NBTTagCompound p_75117_1_)
|
| 109 | {
|
| 110 | p_75117_1_.setInteger("foodLevel", this.foodLevel);
|
| 111 | p_75117_1_.setInteger("foodTickTimer", this.foodTimer);
|
| 112 | p_75117_1_.setFloat("foodSaturationLevel", this.foodSaturationLevel);
|
| 113 | p_75117_1_.setFloat("foodExhaustionLevel", this.foodExhaustionLevel);
|
| 114 | }
|
| 115 |
|
| 116 | /**
|
| 117 | * Get the player's food level.
|
| 118 | */
|
| 119 | public int getFoodLevel()
|
| 120 | {
|
| 121 | return this.foodLevel;
|
| 122 | }
|
| 123 |
|
| 124 | public int getPrevFoodLevel()
|
| 125 | {
|
| 126 | return this.prevFoodLevel;
|
| 127 | }
|
| 128 |
|
| 129 | /**
|
| 130 | * Get whether the player must eat food.
|
| 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 p_75113_1_)
|
| 141 | {
|
| 142 | this.foodExhaustionLevel = Math.min(this.foodExhaustionLevel + p_75113_1_, 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 | public void setFoodLevel(int foodLevelIn)
|
| 154 | {
|
| 155 | this.foodLevel = foodLevelIn;
|
| 156 | }
|
| 157 |
|
| 158 | public void setFoodSaturationLevel(float foodSaturationLevelIn)
|
| 159 | {
|
| 160 | this.foodSaturationLevel = foodSaturationLevelIn;
|
| 161 | }
|
| 162 | }
|
| 163 |
|