Allow eating in creative & peaceful

closes #5923
closes #6056
This commit is contained in:
Dylan K. Taylor 2024-12-01 17:42:26 +00:00
parent 93a9007f3c
commit 12214792b3
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
5 changed files with 18 additions and 3 deletions

View File

@ -34,6 +34,7 @@ interface FoodSource extends Consumable{
/**
* Returns whether a Human eating this FoodSource must have a non-full hunger bar.
* This is ignored in creative mode and in peaceful difficulty.
*/
public function requiresHunger() : bool;
}

View File

@ -68,6 +68,7 @@ use pocketmine\network\mcpe\protocol\types\PlayerPermissions;
use pocketmine\network\mcpe\protocol\UpdateAbilitiesPacket;
use pocketmine\player\Player;
use pocketmine\world\sound\TotemUseSound;
use pocketmine\world\World;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use function array_fill;
@ -189,8 +190,16 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
return $this->hungerManager;
}
/**
* Returns whether the Human can eat food. This may return a different result than {@link HungerManager::isHungry()},
* as HungerManager only handles the hunger bar.
*/
public function canEat() : bool{
return $this->hungerManager->isHungry() || $this->getWorld()->getDifficulty() === World::DIFFICULTY_PEACEFUL;
}
public function consumeObject(Consumable $consumable) : bool{
if($consumable instanceof FoodSource && $consumable->requiresHunger() && !$this->hungerManager->isHungry()){
if($consumable instanceof FoodSource && $consumable->requiresHunger() && !$this->canEat()){
return false;
}

View File

@ -88,7 +88,8 @@ class HungerManager{
}
/**
* Returns whether this Human may consume objects requiring hunger.
* Returns whether the food level is below the maximum.
* This doesn't decide if the entity can eat food. Use {@link Human::canEat()} for that.
*/
public function isHungry() : bool{
return $this->getFood() < $this->getMaxFood();

View File

@ -44,6 +44,6 @@ abstract class Food extends Item implements FoodSourceItem{
}
public function canStartUsingItem(Player $player) : bool{
return !$this->requiresHunger() || $player->getHungerManager()->isHungry();
return !$this->requiresHunger() || $player->canEat();
}
}

View File

@ -1471,6 +1471,10 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
return true;
}
public function canEat() : bool{
return $this->isCreative() || parent::canEat();
}
public function canBreathe() : bool{
return $this->isCreative() || parent::canBreathe();
}