diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 01711ea4c8..577cf54101 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -2123,10 +2123,8 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ Server::broadcastPacket($this->getViewers(), $pk); $amount = $items[$slot->getId()]; - $this->server->getPluginManager()->callEvent($ev = new EntityRegainHealthEvent($this, $amount, EntityRegainHealthEvent::CAUSE_EATING)); - if(!$ev->isCancelled()){ - $this->heal($ev->getAmount(), $ev); - } + $ev = new EntityRegainHealthEvent($this, $amount, EntityRegainHealthEvent::CAUSE_EATING); + $this->heal($ev->getAmount(), $ev); --$slot->count; $this->inventory->setItemInHand($slot, $this); @@ -2650,38 +2648,24 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } } - public function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC){ + public function attack($damage, EntityDamageEvent $source){ if($this->dead === true){ return; } - if($this->isCreative()){ - if($source instanceof EntityDamageEvent){ - $cause = $source->getCause(); - }else{ - $cause = $source; - } - - if( - $cause !== EntityDamageEvent::CAUSE_MAGIC - and $cause !== EntityDamageEvent::CAUSE_SUICIDE - and $cause !== EntityDamageEvent::CAUSE_VOID - ){ - if($source instanceof EntityDamageEvent){ - $source->setCancelled(); - } - return; - } - } - + if($this->isCreative() + and $source->getCause() !== EntityDamageEvent::CAUSE_MAGIC + and $source->getCause() !== EntityDamageEvent::CAUSE_SUICIDE + and $source->getCause() !== EntityDamageEvent::CAUSE_VOID + ){ + $source->setCancelled(); + } parent::attack($damage, $source); - if($source instanceof EntityDamageEvent and $source->isCancelled()){ + if($source->isCancelled()){ return; - } - - if($this->getLastDamageCause() === $source){ + }elseif($this->getLastDamageCause() === $source){ $pk = new EntityEventPacket(); $pk->eid = $this->getId(); $pk->event = 2; diff --git a/src/pocketmine/block/Cake.php b/src/pocketmine/block/Cake.php index 8373d660c0..b7ab506a69 100644 --- a/src/pocketmine/block/Cake.php +++ b/src/pocketmine/block/Cake.php @@ -92,10 +92,10 @@ class Cake extends Transparent{ public function onActivate(Item $item, Player $player = null){ if($player instanceof Player and $player->getHealth() < 20){ ++$this->meta; - Server::getInstance()->getPluginManager()->callEvent($ev = new EntityRegainHealthEvent($player, 3, EntityRegainHealthEvent::CAUSE_EATING)); - if(!$ev->isCancelled()){ - $player->heal($ev->getAmount(), $ev); - } + + $ev = new EntityRegainHealthEvent($player, 3, EntityRegainHealthEvent::CAUSE_EATING); + $player->heal($ev->getAmount(), $ev); + if($this->meta >= 0x06){ $this->getLevel()->setBlock($this, new Air(), true); }else{ diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 706907bbe1..aa1a270da5 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -513,18 +513,42 @@ abstract class Entity extends Location implements Metadatable{ } /** - * @param float $damage - * @param int|EntityDamageEvent $source + * @param float $damage + * @param EntityDamageEvent $source * */ - abstract function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC); + public function attack($damage, EntityDamageEvent $source){ + if($this->hasEffect(Effect::FIRE_RESISTANCE) + and $source->getCause() === EntityDamageEvent::CAUSE_FIRE + and $source->getCause() === EntityDamageEvent::CAUSE_FIRE_TICK + and $source->getCause() === EntityDamageEvent::CAUSE_LAVA){ + $source->setCancelled(); + } + + $this->server->getPluginManager()->callEvent($source); + if($source->isCancelled()){ + return; + } + + $this->setLastDamageCause($source); + + + $this->setHealth($this->getHealth() - $source->getFinalDamage()); + } /** - * @param float $amount - * @param int|EntityRegainHealthEvent $source + * @param float $amount + * @param EntityRegainHealthEvent $source * */ - abstract function heal($amount, $source = EntityRegainHealthEvent::CAUSE_MAGIC); + public function heal($amount, EntityRegainHealthEvent $source){ + $this->server->getPluginManager()->callEvent($source); + if($source->isCancelled()){ + return; + } + + $this->setHealth($this->getHealth() + $source->getAmount()); + } /** * @return int @@ -556,10 +580,10 @@ abstract class Entity extends Location implements Metadatable{ } /** - * @param int|EntityDamageEvent $type + * @param EntityDamageEvent $type */ - public function setLastDamageCause($type){ - $this->lastDamageCause = $type instanceof EntityDamageEvent ? $type : $type; + public function setLastDamageCause(EntityDamageEvent $type){ + $this->lastDamageCause = $type; } /** diff --git a/src/pocketmine/entity/FallingSand.php b/src/pocketmine/entity/FallingSand.php index fff4229bd6..7b9c8170a4 100644 --- a/src/pocketmine/entity/FallingSand.php +++ b/src/pocketmine/entity/FallingSand.php @@ -144,14 +144,6 @@ class FallingSand extends Entity{ $this->namedtag->Data = new Byte("Data", $this->damage); } - public function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC){ - - } - - public function heal($amount, $source = EntityRegainHealthEvent::CAUSE_MAGIC){ - - } - public function spawnTo(Player $player){ $pk = new AddEntityPacket(); $pk->type = FallingSand::NETWORK_ID; diff --git a/src/pocketmine/entity/Item.php b/src/pocketmine/entity/Item.php index d73f6cc471..8ac5e4efcb 100644 --- a/src/pocketmine/entity/Item.php +++ b/src/pocketmine/entity/Item.php @@ -130,22 +130,6 @@ class Item extends Entity{ return $hasUpdate or !$this->onGround or $this->motionX != 0 or $this->motionY != 0 or $this->motionZ != 0; } - public function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC){ - if($source instanceof EntityDamageEvent){ - $this->server->getPluginManager()->callEvent($source); - $damage = $source->getFinalDamage(); - if($source->isCancelled()){ - return; - } - } - $this->setLastDamageCause($source); - $this->setHealth($this->getHealth() - $damage); - } - - public function heal($amount, $source = EntityRegainHealthEvent::CAUSE_MAGIC){ - - } - public function saveNBT(){ parent::saveNBT(); $this->namedtag->Item = new Compound("Item", [ diff --git a/src/pocketmine/entity/Living.php b/src/pocketmine/entity/Living.php index f71a40d810..b587a5975f 100644 --- a/src/pocketmine/entity/Living.php +++ b/src/pocketmine/entity/Living.php @@ -73,36 +73,19 @@ abstract class Living extends Entity implements Damageable{ //return $this->getLevel()->rayTraceBlocks(Vector3::createVector($this->x, $this->y + $this->height, $this->z), Vector3::createVector($entity->x, $entity->y + $entity->height, $entity->z)) === null; } - public function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC){ - if($this->hasEffect(Effect::FIRE_RESISTANCE) and $source){ - return; - } - + public function attack($damage, EntityDamageEvent $source){ if($this->attackTime > 0 or $this->noDamageTicks > 0){ $lastCause = $this->getLastDamageCause(); - if($lastCause instanceof EntityDamageEvent and $lastCause->getDamage() >= $damage){ - if($source instanceof EntityDamageEvent){ - $source->setCancelled(); - $this->server->getPluginManager()->callEvent($source); - $damage = $source->getFinalDamage(); - if($source->isCancelled()){ - return; - } - }else{ - return; - } - }else{ - return; - } - }elseif($source instanceof EntityDamageEvent){ - $this->server->getPluginManager()->callEvent($source); - $damage = $source->getFinalDamage(); - if($source->isCancelled()){ - return; + if($lastCause !== null and $lastCause->getDamage() >= $damage){ + $source->setCancelled(); } } - $this->setLastDamageCause($source); + parent::attack($damage, $source); + + if($source->isCancelled()){ + return; + } if($source instanceof EntityDamageByEntityEvent){ $e = $source->getDamager(); @@ -112,8 +95,6 @@ abstract class Living extends Entity implements Damageable{ $this->knockBack($e, $damage, sin($yaw), cos($yaw), $source->getKnockBack()); } - $this->setHealth($this->getHealth() - $damage); - $pk = new EntityEventPacket(); $pk->eid = $this->getId(); $pk->event = $this->getHealth() <= 0 ? 3 : 2; //Ouch! @@ -141,10 +122,6 @@ abstract class Living extends Entity implements Damageable{ $this->setMotion($motion); } - public function heal($amount, $source = EntityRegainHealthEvent::CAUSE_MAGIC){ - $this->setHealth($this->getHealth() + $amount); - } - public function kill(){ if($this->dead){ return; diff --git a/src/pocketmine/entity/PrimedTNT.php b/src/pocketmine/entity/PrimedTNT.php index 55f5f97c59..4a135f861c 100644 --- a/src/pocketmine/entity/PrimedTNT.php +++ b/src/pocketmine/entity/PrimedTNT.php @@ -118,14 +118,6 @@ class PrimedTNT extends Entity implements Explosive{ return $hasUpdate or $this->fuse >= 0 or $this->motionX != 0 or $this->motionY != 0 or $this->motionZ != 0; } - public function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC){ - - } - - public function heal($amount, $source = EntityRegainHealthEvent::CAUSE_MAGIC){ - - } - public function explode(){ $this->server->getPluginManager()->callEvent($ev = new ExplosionPrimeEvent($this, 4)); diff --git a/src/pocketmine/entity/Projectile.php b/src/pocketmine/entity/Projectile.php index 7c7547767b..8af3e9c34f 100644 --- a/src/pocketmine/entity/Projectile.php +++ b/src/pocketmine/entity/Projectile.php @@ -59,14 +59,6 @@ abstract class Projectile extends Entity{ $this->namedtag->Age = new Short("Age", $this->age); } - public function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC){ - - } - - public function heal($amount, $source = EntityRegainHealthEvent::CAUSE_MAGIC){ - - } - public function onUpdate($currentTick){ if($this->closed){ return false;