pocketmine\entity\Entity::attack PHP Method

attack() public method

public attack ( float $damage, EntityDamageEvent $source ) : boolean
$damage float
$source pocketmine\event\entity\EntityDamageEvent
return boolean
    public function attack($damage, EntityDamageEvent $source)
    {
        if ($this->hasEffect(Effect::FIRE_RESISTANCE) and ($source->getCause() === EntityDamageEvent::CAUSE_FIRE or $source->getCause() === EntityDamageEvent::CAUSE_FIRE_TICK or $source->getCause() === EntityDamageEvent::CAUSE_LAVA)) {
            $source->setCancelled();
        }
        $this->server->getPluginManager()->callEvent($source);
        if ($source->isCancelled()) {
            return false;
        }
        $this->setLastDamageCause($source);
        if ($this instanceof Human) {
            $damage = round($source->getFinalDamage());
            if ($this->getAbsorption() > 0) {
                $absorption = $this->getAbsorption() - $damage;
                $this->setAbsorption($absorption <= 0 ? 0 : $absorption);
                $this->setHealth($this->getHealth() + $absorption);
            } else {
                $this->setHealth($this->getHealth() - $damage);
            }
        } else {
            $this->setHealth($this->getHealth() - round($source->getFinalDamage()));
        }
        return true;
    }

Usage Example

Example #1
1
 public function attack($damage, EntityDamageEvent $source)
 {
     if ($this->attackTime > 0 or $this->noDamageTicks > 0) {
         $lastCause = $this->getLastDamageCause();
         if ($lastCause !== null and $lastCause->getDamage() >= $damage) {
             $source->setCancelled();
         }
     }
     parent::attack($damage, $source);
     if ($source->isCancelled()) {
         return;
     }
     if ($source instanceof EntityDamageByEntityEvent) {
         $e = $source->getDamager();
         if ($source instanceof EntityDamageByChildEntityEvent) {
             $e = $source->getChild();
         }
         if ($e->isOnFire() > 0) {
             $this->setOnFire(2 * $this->server->getDifficulty());
         }
         $deltaX = $this->x - $e->x;
         $deltaZ = $this->z - $e->z;
         $this->knockBack($e, $damage, $deltaX, $deltaZ, $source->getKnockBack());
     }
     $pk = new EntityEventPacket();
     $pk->eid = $this->getId();
     $pk->event = $this->getHealth() <= 0 ? EntityEventPacket::DEATH_ANIMATION : EntityEventPacket::HURT_ANIMATION;
     // Ouch!
     Server::broadcastPacket($this->hasSpawned, $pk);
     $this->attackTime = 10;
     //0.5 seconds cooldown
 }
All Usage Examples Of pocketmine\entity\Entity::attack