39 private int $foodTickTimer = 0;
41 private bool $enabled =
true;
43 public function __construct(
46 $this->hungerAttr = self::fetchAttribute($entity, Attribute::HUNGER);
47 $this->saturationAttr = self::fetchAttribute($entity, Attribute::SATURATION);
48 $this->exhaustionAttr = self::fetchAttribute($entity, Attribute::EXHAUSTION);
51 private static function fetchAttribute(
Entity $entity,
string $attributeId) :
Attribute{
52 $attribute = AttributeFactory::getInstance()->mustGet($attributeId);
53 $entity->getAttributeMap()->add($attribute);
57 public function getFood() :
float{
58 return $this->hungerAttr->getValue();
67 public function setFood(
float $new) : void{
68 $old = $this->hungerAttr->getValue();
69 $this->hungerAttr->setValue($new);
72 foreach([17, 6, 0] as $bound){
73 if(($old > $bound) !== ($new > $bound)){
74 $this->foodTickTimer = 0;
80 public function getMaxFood() : float{
81 return $this->hungerAttr->getMaxValue();
84 public function addFood(
float $amount) : void{
85 $amount += $this->hungerAttr->getValue();
86 $amount = max(min($amount, $this->hungerAttr->getMaxValue()), $this->hungerAttr->getMinValue());
87 $this->setFood($amount);
94 return $this->getFood() < $this->getMaxFood();
97 public function getSaturation() : float{
98 return $this->saturationAttr->getValue();
108 $this->saturationAttr->setValue($saturation);
111 public function addSaturation(
float $amount) : void{
112 $this->saturationAttr->setValue($this->saturationAttr->getValue() + $amount, true);
115 public function getExhaustion() : float{
116 return $this->exhaustionAttr->getValue();
124 $this->exhaustionAttr->setValue($exhaustion);
132 public function exhaust(
float $amount,
int $cause = PlayerExhaustEvent::CAUSE_CUSTOM) : float{
137 if(PlayerExhaustEvent::hasHandlers()){
140 if($ev->isCancelled()){
143 $evAmount = $ev->getAmount();
146 $exhaustion = $this->getExhaustion();
147 $exhaustion += $evAmount;
149 while($exhaustion >= 4.0){
152 $saturation = $this->getSaturation();
154 $saturation = max(0, $saturation - 1.0);
155 $this->setSaturation($saturation);
157 $food = $this->getFood();
160 $this->setFood(max($food, 0));
164 $this->setExhaustion($exhaustion);
169 public function getFoodTickTimer() : int{
170 return $this->foodTickTimer;
173 public function setFoodTickTimer(
int $foodTickTimer) : void{
174 if($foodTickTimer < 0){
175 throw new \InvalidArgumentException(
"Expected a non-negative value");
177 $this->foodTickTimer = $foodTickTimer;
180 public function tick(
int $tickDiff = 1) : void{
181 if(!$this->entity->isAlive() || !$this->enabled){
184 $food = $this->getFood();
185 $health = $this->entity->getHealth();
186 $difficulty = $this->entity->getWorld()->getDifficulty();
188 $this->foodTickTimer += $tickDiff;
189 if($this->foodTickTimer >= 80){
190 $this->foodTickTimer = 0;
193 if($difficulty === World::DIFFICULTY_PEACEFUL && $this->foodTickTimer % 10 === 0){
194 if($food < $this->getMaxFood()){
196 $food = $this->getFood();
198 if($this->foodTickTimer % 20 === 0 && $health < $this->entity->getMaxHealth()){
199 $this->entity->heal(
new EntityRegainHealthEvent($this->entity, 1, EntityRegainHealthEvent::CAUSE_SATURATION));
203 if($this->foodTickTimer === 0){
205 if($health < $this->entity->getMaxHealth()){
206 $this->entity->heal(
new EntityRegainHealthEvent($this->entity, 1, EntityRegainHealthEvent::CAUSE_SATURATION));
207 $this->exhaust(6.0, PlayerExhaustEvent::CAUSE_HEALTH_REGEN);
210 if(($difficulty === World::DIFFICULTY_EASY && $health > 10) || ($difficulty === World::DIFFICULTY_NORMAL && $health > 1) || $difficulty === World::DIFFICULTY_HARD){
211 $this->entity->attack(
new EntityDamageEvent($this->entity, EntityDamageEvent::CAUSE_STARVATION, 1));
217 $this->entity->setSprinting(
false);
221 public function isEnabled() : bool{
222 return $this->enabled;
225 public function setEnabled(
bool $enabled) : void{
226 $this->enabled = $enabled;