Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed players to eat in creative mode and difficulty peaceful #5923

Open
wants to merge 12 commits into
base: minor-next
Choose a base branch
from
5 changes: 2 additions & 3 deletions src/entity/Human.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,10 @@ public function getHungerManager() : HungerManager{
return $this->hungerManager;
}

public function consumeObject(Consumable $consumable) : bool{
if($consumable instanceof FoodSource && $consumable->requiresHunger() && !$this->hungerManager->isHungry()){
public function consumeObject(Consumable $consumable, bool $forceEating = false) : bool{
if (($consumable instanceof FoodSource) && !$forceEating && $consumable->requiresHunger() && !$this->getHungerManager()->isHungry()){
NTT1906 marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

ShockedPlot7560 marked this conversation as resolved.
Show resolved Hide resolved
return parent::consumeObject($consumable);
}

Expand Down
2 changes: 1 addition & 1 deletion src/item/Food.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public function onConsume(Living $consumer) : void{
}

public function canStartUsingItem(Player $player) : bool{
return !$this->requiresHunger() || $player->getHungerManager()->isHungry();
return $player->forceAllowEating() || !$this->requiresHunger() || $player->getHungerManager()->isHungry();
}
}
12 changes: 12 additions & 0 deletions src/player/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use pocketmine\entity\animation\ArmSwingAnimation;
use pocketmine\entity\animation\CriticalHitAnimation;
use pocketmine\entity\Attribute;
use pocketmine\entity\Consumable;
use pocketmine\entity\effect\VanillaEffects;
use pocketmine\entity\Entity;
use pocketmine\entity\Human;
Expand Down Expand Up @@ -673,6 +674,13 @@ public function setUsingItem(bool $value) : void{
$this->networkPropertiesDirty = true;
}

/**
* Allow eating
*/
public function forceAllowEating() : bool{
NTT1906 marked this conversation as resolved.
Show resolved Hide resolved
return $this->isCreative() || $this->getWorld()->getDifficulty() === World::DIFFICULTY_PEACEFUL;
}

/**
* Returns how long the player has been using their currently-held item for. Used for determining arrow shoot force
* for bows.
Expand Down Expand Up @@ -2000,6 +2008,10 @@ public function emote(string $emoteId) : void{
}
}

public function consumeObject(Consumable $consumable, bool $forceEating = false) : bool{
return parent::consumeObject($consumable, $this->forceAllowEating() || $forceEating);
NTT1906 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Drops an item on the ground in front of the player.
*/
Expand Down