PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
HungerManager.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\entity;
25
30use function max;
31use function min;
32
34
35 private Attribute $hungerAttr;
36 private Attribute $saturationAttr;
37 private Attribute $exhaustionAttr;
38
39 private int $foodTickTimer = 0;
40
41 private bool $enabled = true;
42
43 public function __construct(
44 private Human $entity
45 ){
46 $this->hungerAttr = self::fetchAttribute($entity, Attribute::HUNGER);
47 $this->saturationAttr = self::fetchAttribute($entity, Attribute::SATURATION);
48 $this->exhaustionAttr = self::fetchAttribute($entity, Attribute::EXHAUSTION);
49 }
50
51 private static function fetchAttribute(Entity $entity, string $attributeId) : Attribute{
52 $attribute = AttributeFactory::getInstance()->mustGet($attributeId);
53 $entity->getAttributeMap()->add($attribute);
54 return $attribute;
55 }
56
57 public function getFood() : float{
58 return $this->hungerAttr->getValue();
59 }
60
67 public function setFood(float $new) : void{
68 $old = $this->hungerAttr->getValue();
69 $this->hungerAttr->setValue($new);
70
71 // ranges: 18-20 (regen), 7-17 (none), 1-6 (no sprint), 0 (health depletion)
72 foreach([17, 6, 0] as $bound){
73 if(($old > $bound) !== ($new > $bound)){
74 $this->foodTickTimer = 0;
75 break;
76 }
77 }
78 }
79
80 public function getMaxFood() : float{
81 return $this->hungerAttr->getMaxValue();
82 }
83
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);
88 }
89
94 public function isHungry() : bool{
95 return $this->getFood() < $this->getMaxFood();
96 }
97
98 public function getSaturation() : float{
99 return $this->saturationAttr->getValue();
100 }
101
108 public function setSaturation(float $saturation) : void{
109 $this->saturationAttr->setValue($saturation);
110 }
111
112 public function addSaturation(float $amount) : void{
113 $this->saturationAttr->setValue($this->saturationAttr->getValue() + $amount, true);
114 }
115
116 public function getExhaustion() : float{
117 return $this->exhaustionAttr->getValue();
118 }
119
124 public function setExhaustion(float $exhaustion) : void{
125 $this->exhaustionAttr->setValue($exhaustion);
126 }
127
133 public function exhaust(float $amount, int $cause = PlayerExhaustEvent::CAUSE_CUSTOM) : float{
134 if(!$this->enabled){
135 return 0;
136 }
137 $evAmount = $amount;
138 if(PlayerExhaustEvent::hasHandlers()){
139 $ev = new PlayerExhaustEvent($this->entity, $amount, $cause);
140 $ev->call();
141 if($ev->isCancelled()){
142 return 0.0;
143 }
144 $evAmount = $ev->getAmount();
145 }
146
147 $exhaustion = $this->getExhaustion();
148 $exhaustion += $evAmount;
149
150 while($exhaustion >= 4.0){
151 $exhaustion -= 4.0;
152
153 $saturation = $this->getSaturation();
154 if($saturation > 0){
155 $saturation = max(0, $saturation - 1.0);
156 $this->setSaturation($saturation);
157 }else{
158 $food = $this->getFood();
159 if($food > 0){
160 $food--;
161 $this->setFood(max($food, 0));
162 }
163 }
164 }
165 $this->setExhaustion($exhaustion);
166
167 return $evAmount;
168 }
169
170 public function getFoodTickTimer() : int{
171 return $this->foodTickTimer;
172 }
173
174 public function setFoodTickTimer(int $foodTickTimer) : void{
175 if($foodTickTimer < 0){
176 throw new \InvalidArgumentException("Expected a non-negative value");
177 }
178 $this->foodTickTimer = $foodTickTimer;
179 }
180
181 public function tick(int $tickDiff = 1) : void{
182 if(!$this->entity->isAlive() || !$this->enabled){
183 return;
184 }
185 $food = $this->getFood();
186 $health = $this->entity->getHealth();
187 $difficulty = $this->entity->getWorld()->getDifficulty();
188
189 $this->foodTickTimer += $tickDiff;
190 if($this->foodTickTimer >= 80){
191 $this->foodTickTimer = 0;
192 }
193
194 if($difficulty === World::DIFFICULTY_PEACEFUL && $this->foodTickTimer % 10 === 0){
195 if($food < $this->getMaxFood()){
196 $this->addFood(1.0);
197 $food = $this->getFood();
198 }
199 if($this->foodTickTimer % 20 === 0 && $health < $this->entity->getMaxHealth()){
200 $this->entity->heal(new EntityRegainHealthEvent($this->entity, 1, EntityRegainHealthEvent::CAUSE_SATURATION));
201 }
202 }
203
204 if($this->foodTickTimer === 0){
205 if($food >= 18){
206 if($health < $this->entity->getMaxHealth()){
207 $this->entity->heal(new EntityRegainHealthEvent($this->entity, 1, EntityRegainHealthEvent::CAUSE_SATURATION));
208 $this->exhaust(6.0, PlayerExhaustEvent::CAUSE_HEALTH_REGEN);
209 }
210 }elseif($food <= 0){
211 if(($difficulty === World::DIFFICULTY_EASY && $health > 10) || ($difficulty === World::DIFFICULTY_NORMAL && $health > 1) || $difficulty === World::DIFFICULTY_HARD){
212 $this->entity->attack(new EntityDamageEvent($this->entity, EntityDamageEvent::CAUSE_STARVATION, 1));
213 }
214 }
215 }
216
217 if($food <= 6){
218 $this->entity->setSprinting(false);
219 }
220 }
221
222 public function isEnabled() : bool{
223 return $this->enabled;
224 }
225
226 public function setEnabled(bool $enabled) : void{
227 $this->enabled = $enabled;
228 }
229}
exhaust(float $amount, int $cause=PlayerExhaustEvent::CAUSE_CUSTOM)