| 1 | package net.minecraft.world.food;
|
| 2 |
|
| 3 | import net.minecraft.server.level.ServerLevel;
|
| 4 | import net.minecraft.server.level.ServerPlayer;
|
| 5 | import net.minecraft.util.Mth;
|
| 6 | import net.minecraft.world.Difficulty;
|
| 7 | import net.minecraft.world.level.gamerules.GameRules;
|
| 8 | import net.minecraft.world.level.storage.ValueInput;
|
| 9 | import net.minecraft.world.level.storage.ValueOutput;
|
| 10 |
|
| 11 | public class FoodData {
|
| 12 | private static final int DEFAULT_TICK_TIMER = 0;
|
| 13 | private static final float DEFAULT_EXHAUSTION_LEVEL = 0.0F;
|
| 14 | private int foodLevel = 20;
|
| 15 | private float saturationLevel = 5.0F;
|
| 16 | private float exhaustionLevel;
|
| 17 | private int tickTimer;
|
| 18 |
|
| 19 | private void add(final int food, final float saturation) {
|
| 20 | this.foodLevel = Mth.clamp(food + this.foodLevel, 0, 20);
|
| 21 | this.saturationLevel = Mth.clamp(saturation + this.saturationLevel, 0.0F, this.foodLevel);
|
| 22 | }
|
| 23 |
|
| 24 | public void eat(final int food, final float saturationModifier) {
|
| 25 | this.add(food, FoodConstants.saturationByModifier(food, saturationModifier));
|
| 26 | }
|
| 27 |
|
| 28 | public void eat(final FoodProperties foodProperties) {
|
| 29 | this.add(foodProperties.nutrition(), foodProperties.saturation());
|
| 30 | }
|
| 31 |
|
| 32 | public void tick(final ServerPlayer player) {
|
| 33 | ServerLevel level = player.level();
|
| 34 | Difficulty difficulty = level.getDifficulty();
|
| 35 | if (this.exhaustionLevel > 4.0F) {
|
| 36 | this.exhaustionLevel -= 4.0F;
|
| 37 | if (this.saturationLevel > 0.0F) {
|
| 38 | this.saturationLevel = Math.max(this.saturationLevel - 1.0F, 0.0F);
|
| 39 | } else if (difficulty != Difficulty.PEACEFUL) {
|
| 40 | this.foodLevel = Math.max(this.foodLevel - 1, 0);
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | boolean naturalRegen = level.getGameRules().get(GameRules.NATURAL_HEALTH_REGENERATION);
|
| 45 | if (naturalRegen && this.saturationLevel > 0.0F && player.isHurt() && this.foodLevel >= 20) {
|
| 46 | this.tickTimer++;
|
| 47 | if (this.tickTimer >= 10) {
|
| 48 | float saturationSpent = Math.min(this.saturationLevel, 6.0F);
|
| 49 | player.heal(saturationSpent / 6.0F);
|
| 50 | this.addExhaustion(saturationSpent);
|
| 51 | this.tickTimer = 0;
|
| 52 | }
|
| 53 | } else if (naturalRegen && this.foodLevel >= 18 && player.isHurt()) {
|
| 54 | this.tickTimer++;
|
| 55 | if (this.tickTimer >= 80) {
|
| 56 | player.heal(1.0F);
|
| 57 | this.addExhaustion(6.0F);
|
| 58 | this.tickTimer = 0;
|
| 59 | }
|
| 60 | } else if (this.foodLevel <= 0) {
|
| 61 | this.tickTimer++;
|
| 62 | if (this.tickTimer >= 80) {
|
| 63 | if (player.getHealth() > 10.0F || difficulty == Difficulty.HARD || player.getHealth() > 1.0F && difficulty == Difficulty.NORMAL) {
|
| 64 | player.hurtServer(level, player.damageSources().starve(), 1.0F);
|
| 65 | }
|
| 66 |
|
| 67 | this.tickTimer = 0;
|
| 68 | }
|
| 69 | } else {
|
| 70 | this.tickTimer = 0;
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | public void readAdditionalSaveData(final ValueInput input) {
|
| 75 | this.foodLevel = input.getIntOr("foodLevel", 20);
|
| 76 | this.tickTimer = input.getIntOr("foodTickTimer", 0);
|
| 77 | this.saturationLevel = input.getFloatOr("foodSaturationLevel", 5.0F);
|
| 78 | this.exhaustionLevel = input.getFloatOr("foodExhaustionLevel", 0.0F);
|
| 79 | }
|
| 80 |
|
| 81 | public void addAdditionalSaveData(final ValueOutput output) {
|
| 82 | output.putInt("foodLevel", this.foodLevel);
|
| 83 | output.putInt("foodTickTimer", this.tickTimer);
|
| 84 | output.putFloat("foodSaturationLevel", this.saturationLevel);
|
| 85 | output.putFloat("foodExhaustionLevel", this.exhaustionLevel);
|
| 86 | }
|
| 87 |
|
| 88 | public int getFoodLevel() {
|
| 89 | return this.foodLevel;
|
| 90 | }
|
| 91 |
|
| 92 | public boolean hasEnoughFood() {
|
| 93 | return this.getFoodLevel() > 6.0F;
|
| 94 | }
|
| 95 |
|
| 96 | public boolean needsFood() {
|
| 97 | return this.foodLevel < 20;
|
| 98 | }
|
| 99 |
|
| 100 | public void addExhaustion(final float amount) {
|
| 101 | this.exhaustionLevel = Math.min(this.exhaustionLevel + amount, 40.0F);
|
| 102 | }
|
| 103 |
|
| 104 | public float getSaturationLevel() {
|
| 105 | return this.saturationLevel;
|
| 106 | }
|
| 107 |
|
| 108 | public void setFoodLevel(final int food) {
|
| 109 | this.foodLevel = food;
|
| 110 | }
|
| 111 |
|
| 112 | public void setSaturation(final float saturation) {
|
| 113 | this.saturationLevel = saturation;
|
| 114 | }
|
| 115 | }
|
| 116 |
|