pocketmine\entity\Living::attack PHP Method

attack() public method

public attack ( $damage, EntityDamageEvent $source )
$source pocketmine\event\entity\EntityDamageEvent
    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 and !$e instanceof Player) {
                $this->setOnFire(2 * $this->server->getDifficulty());
            }
            $deltaX = $this->x - $e->x;
            $deltaZ = $this->z - $e->z;
            $this->knockBack($e, $damage, $deltaX, $deltaZ, $source->getKnockBack());
            if ($e instanceof Husk) {
                $this->addEffect(Effect::getEffect(Effect::HUNGER)->setDuration(7 * 20 * $this->server->getDifficulty()));
            }
        }
        $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
    }

Usage Example

Ejemplo n.º 1
0
 public function attack($damage, EntityDamageEvent $source)
 {
     parent::attack($damage, $source);
     if (!$source->isCancelled() and $source->getCause() == EntityDamageEvent::CAUSE_ENTITY_ATTACK) {
         $this->attackingTick = 20;
     }
 }