From 12214792b32a63a0668a2ecbc175f148bd7fe222 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 1 Dec 2024 17:42:26 +0000 Subject: [PATCH] Allow eating in creative & peaceful closes #5923 closes #6056 --- src/entity/FoodSource.php | 1 + src/entity/Human.php | 11 ++++++++++- src/entity/HungerManager.php | 3 ++- src/item/Food.php | 2 +- src/player/Player.php | 4 ++++ 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/entity/FoodSource.php b/src/entity/FoodSource.php index 98478b4a1..028c76783 100644 --- a/src/entity/FoodSource.php +++ b/src/entity/FoodSource.php @@ -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; } diff --git a/src/entity/Human.php b/src/entity/Human.php index f2c4c7a74..833196651 100644 --- a/src/entity/Human.php +++ b/src/entity/Human.php @@ -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; } diff --git a/src/entity/HungerManager.php b/src/entity/HungerManager.php index a31855891..7e3b40e74 100644 --- a/src/entity/HungerManager.php +++ b/src/entity/HungerManager.php @@ -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(); diff --git a/src/item/Food.php b/src/item/Food.php index 1950c4b14..d01ce9e18 100644 --- a/src/item/Food.php +++ b/src/item/Food.php @@ -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(); } } diff --git a/src/player/Player.php b/src/player/Player.php index e4c74c4ef..f20945b9a 100644 --- a/src/player/Player.php +++ b/src/player/Player.php @@ -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(); }