1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#include "stdafx.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.h"
#include "net.minecraft.world.damagesource.h"
#include "com.mojang.nbt.h"
#include "FoodConstants.h"
#include "FoodData.h"
FoodData::FoodData()
{
exhaustionLevel = 0;
tickTimer = 0;
foodLevel = FoodConstants::MAX_FOOD;
lastFoodLevel = FoodConstants::MAX_FOOD;
saturationLevel = FoodConstants::START_SATURATION;
}
void FoodData::eat(int food, float saturationModifier)
{
foodLevel = min(food + foodLevel, FoodConstants::MAX_FOOD);
saturationLevel = min(saturationLevel + (float) food * saturationModifier * 2.0f, (float)foodLevel);
}
void FoodData::eat(FoodItem *item)
{
eat(item->getNutrition(), item->getSaturationModifier());
}
void FoodData::tick(shared_ptr<Player> player)
{
int difficulty = player->level->difficulty;
lastFoodLevel = foodLevel;
if (exhaustionLevel > FoodConstants::EXHAUSTION_DROP)
{
exhaustionLevel -= FoodConstants::EXHAUSTION_DROP;
if (saturationLevel > 0)
{
saturationLevel = max(saturationLevel - 1, 0.0f);
}
else if (difficulty > Difficulty::PEACEFUL)
{
foodLevel = max(foodLevel - 1, 0);
}
}
// 4J: Added - Allow host to disable using hunger. We don't deplete the hunger bar due to exhaustion
// but I think we should deplete it to heal. Don't heal if natural regen is disabled
if(player->isAllowedToIgnoreExhaustion() && player->level->getGameRules()->getBoolean(GameRules::RULE_NATURAL_REGENERATION))
{
if(foodLevel > 0 && player->isHurt())
{
tickTimer++;
if (tickTimer >= FoodConstants::HEALTH_TICK_COUNT)
{
player->heal(1);
--foodLevel;
tickTimer = 0;
}
}
}
else if (player->level->getGameRules()->getBoolean(GameRules::RULE_NATURAL_REGENERATION) && foodLevel >= FoodConstants::HEAL_LEVEL && player->isHurt())
{
tickTimer++;
if (tickTimer >= FoodConstants::HEALTH_TICK_COUNT)
{
player->heal(1);
addExhaustion(FoodConstants::EXHAUSTION_HEAL);
tickTimer = 0;
}
}
else if (foodLevel <= FoodConstants::STARVE_LEVEL)
{
tickTimer++;
if (tickTimer >= FoodConstants::HEALTH_TICK_COUNT)
{
if (player->getHealth() > 10 || difficulty >= Difficulty::HARD || (player->getHealth() > 1 && difficulty >= Difficulty::NORMAL))
{
player->hurt(DamageSource::starve, 1);
}
tickTimer = 0;
}
}
else
{
tickTimer = 0;
}
}
void FoodData::readAdditionalSaveData(CompoundTag *entityTag)
{
if (entityTag->contains(L"foodLevel"))
{
foodLevel = entityTag->getInt(L"foodLevel");
tickTimer = entityTag->getInt(L"foodTickTimer");
saturationLevel = entityTag->getFloat(L"foodSaturationLevel");
exhaustionLevel = entityTag->getFloat(L"foodExhaustionLevel");
}
}
void FoodData::addAdditonalSaveData(CompoundTag *entityTag)
{
entityTag->putInt(L"foodLevel", foodLevel);
entityTag->putInt(L"foodTickTimer", tickTimer);
entityTag->putFloat(L"foodSaturationLevel", saturationLevel);
entityTag->putFloat(L"foodExhaustionLevel", exhaustionLevel);
}
int FoodData::getFoodLevel()
{
return foodLevel;
}
int FoodData::getLastFoodLevel()
{
return lastFoodLevel;
}
bool FoodData::needsFood()
{
return foodLevel < FoodConstants::MAX_FOOD;
}
void FoodData::addExhaustion(float amount)
{
exhaustionLevel = min(exhaustionLevel + amount, FoodConstants::MAX_SATURATION * 2);
}
float FoodData::getExhaustionLevel()
{
return exhaustionLevel;
}
float FoodData::getSaturationLevel()
{
return saturationLevel;
}
void FoodData::setFoodLevel(int food)
{
foodLevel = food;
}
void FoodData::setSaturation(float saturation)
{
saturationLevel = saturation;
}
void FoodData::setExhaustion(float exhaustion)
{
exhaustionLevel = exhaustion;
}
|