package net.minecraft.world.food;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.Difficulty;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Item;

public class FoodData {
    private int foodLevel;
    private float saturationLevel;
    private float exhaustionLevel;
    private int tickTimer;
    private int lastFoodLevel;
    
    public FoodData() {
        this.foodLevel = 20;
        this.lastFoodLevel = 20;
        this.saturationLevel = 5.0f;
    }
    
    public void eat(final int integer, final float float2) {
        this.foodLevel = Math.min(integer + this.foodLevel, 20);
        this.saturationLevel = Math.min(this.saturationLevel + integer * float2 * 2.0f, (float)this.foodLevel);
    }
    
    public void eat(final Item bei, final ItemStack ben) {
        if (bei.isEdible()) {
            final FoodProperties bak4 = bei.getFoodProperties();
            this.eat(bak4.getNutrition(), bak4.getSaturationModifier());
        }
    }
    
    public void tick(final Player ayj) {
        final Difficulty aji3 = ayj.level.getDifficulty();
        this.lastFoodLevel = this.foodLevel;
        if (this.exhaustionLevel > 4.0f) {
            this.exhaustionLevel -= 4.0f;
            if (this.saturationLevel > 0.0f) {
                this.saturationLevel = Math.max(this.saturationLevel - 1.0f, 0.0f);
            }
            else if (aji3 != Difficulty.PEACEFUL) {
                this.foodLevel = Math.max(this.foodLevel - 1, 0);
            }
        }
        final boolean boolean4 = ayj.level.getGameRules().getBoolean(GameRules.RULE_NATURAL_REGENERATION);
        if (boolean4 && this.saturationLevel > 0.0f && ayj.isHurt() && this.foodLevel >= 20) {
            ++this.tickTimer;
            if (this.tickTimer >= 10) {
                final float float5 = Math.min(this.saturationLevel, 6.0f);
                ayj.heal(float5 / 6.0f);
                this.addExhaustion(float5);
                this.tickTimer = 0;
            }
        }
        else if (boolean4 && this.foodLevel >= 18 && ayj.isHurt()) {
            ++this.tickTimer;
            if (this.tickTimer >= 80) {
                ayj.heal(1.0f);
                this.addExhaustion(6.0f);
                this.tickTimer = 0;
            }
        }
        else if (this.foodLevel <= 0) {
            ++this.tickTimer;
            if (this.tickTimer >= 80) {
                if (ayj.getHealth() > 10.0f || aji3 == Difficulty.HARD || (ayj.getHealth() > 1.0f && aji3 == Difficulty.NORMAL)) {
                    ayj.hurt(DamageSource.STARVE, 1.0f);
                }
                this.tickTimer = 0;
            }
        }
        else {
            this.tickTimer = 0;
        }
    }
    
    public void readAdditionalSaveData(final CompoundTag jt) {
        if (jt.contains("foodLevel", 99)) {
            this.foodLevel = jt.getInt("foodLevel");
            this.tickTimer = jt.getInt("foodTickTimer");
            this.saturationLevel = jt.getFloat("foodSaturationLevel");
            this.exhaustionLevel = jt.getFloat("foodExhaustionLevel");
        }
    }
    
    public void addAdditionalSaveData(final CompoundTag jt) {
        jt.putInt("foodLevel", this.foodLevel);
        jt.putInt("foodTickTimer", this.tickTimer);
        jt.putFloat("foodSaturationLevel", this.saturationLevel);
        jt.putFloat("foodExhaustionLevel", this.exhaustionLevel);
    }
    
    public int getFoodLevel() {
        return this.foodLevel;
    }
    
    public boolean needsFood() {
        return this.foodLevel < 20;
    }
    
    public void addExhaustion(final float float1) {
        this.exhaustionLevel = Math.min(this.exhaustionLevel + float1, 40.0f);
    }
    
    public float getSaturationLevel() {
        return this.saturationLevel;
    }
    
    public void setFoodLevel(final int integer) {
        this.foodLevel = integer;
    }
    
    public void setSaturation(final float float1) {
        this.saturationLevel = float1;
    }
}
