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