Example #1
0
 public function setFood($amount)
 {
     if (!$this->server->foodEnabled) {
         $amount = 20;
     }
     if ($amount > 20) {
         $amount = 20;
     }
     if ($amount < 0) {
         $amount = 0;
     }
     $this->server->getPluginManager()->callEvent($ev = new PlayerHungerChangeEvent($this, $amount));
     if ($ev->isCancelled()) {
         return false;
     }
     $amount = $ev->getData();
     if ($amount <= 6 && !($this->getFood() <= 6)) {
         $this->setDataProperty(self::DATA_FLAG_SPRINTING, self::DATA_TYPE_BYTE, false);
     } elseif ($amount > 6 && !($this->getFood() > 6)) {
         $this->setDataProperty(self::DATA_FLAG_SPRINTING, self::DATA_TYPE_BYTE, true);
     }
     $this->food = $amount;
     $this->getAttribute()->getAttribute(AttributeManager::MAX_HUNGER)->setValue($amount);
     return true;
 }
Example #2
0
 public function setFood(float $amount)
 {
     if (!$this->server->foodEnabled) {
         $amount = 20;
     }
     $amount = min(20, max(0, $amount));
     $this->server->getPluginManager()->callEvent($ev = new PlayerHungerChangeEvent($this, $amount));
     if ($ev->isCancelled()) {
         return false;
     }
     $amount = $ev->getData();
     if ($amount <= 6 && !($this->getFood() <= 6)) {
         $this->setDataProperty(self::DATA_FLAG_SPRINTING, self::DATA_TYPE_BYTE, false);
     } elseif ($amount > 6 && !($this->getFood() > 6)) {
         $this->setDataProperty(self::DATA_FLAG_SPRINTING, self::DATA_TYPE_BYTE, true);
     }
     $this->food = $amount;
     if ($this->getAttributeMap() instanceof AttributeMap) {
         $this->getAttributeMap()->getAttribute(Attribute::HUNGER)->setValue($amount);
     } else {
         //Sometimes this function is called before the attribute map is initialized, causing a crash. Not sure how that is even possible. Maybe a plugin?
     }
     return true;
 }