pocketmine\entity\Entity::setHealth PHP Method

setHealth() public method

Sets the health of the Entity. This won't send any update to the players
public setHealth ( integer $amount )
$amount integer
    public function setHealth($amount)
    {
        $amount = (int) $amount;
        if ($amount === $this->health) {
            return;
        }
        if ($amount <= 0) {
            if ($this->isAlive()) {
                $this->kill();
            }
        } elseif ($amount <= $this->getMaxHealth() or $amount < $this->health) {
            $this->health = (int) $amount;
        } else {
            $this->health = $this->getMaxHealth();
        }
    }

Usage Example

示例#1
0
 public function setHealth($amount)
 {
     $wasAlive = $this->isAlive();
     parent::setHealth($amount);
     if ($this->isAlive() and !$wasAlive) {
         $pk = new EntityEventPacket();
         $pk->eid = $this->getId();
         $pk->event = EntityEventPacket::RESPAWN;
         Server::broadcastPacket($this->hasSpawned, $pk);
     }
 }
All Usage Examples Of pocketmine\entity\Entity::setHealth