Improved Entity attack()/heal() event firing

This commit is contained in:
Shoghi Cervantes 2015-03-25 09:57:11 +01:00
parent d1760d9bb8
commit d19631226f
8 changed files with 57 additions and 112 deletions

View File

@ -2123,10 +2123,8 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
Server::broadcastPacket($this->getViewers(), $pk); Server::broadcastPacket($this->getViewers(), $pk);
$amount = $items[$slot->getId()]; $amount = $items[$slot->getId()];
$this->server->getPluginManager()->callEvent($ev = new EntityRegainHealthEvent($this, $amount, EntityRegainHealthEvent::CAUSE_EATING)); $ev = new EntityRegainHealthEvent($this, $amount, EntityRegainHealthEvent::CAUSE_EATING);
if(!$ev->isCancelled()){ $this->heal($ev->getAmount(), $ev);
$this->heal($ev->getAmount(), $ev);
}
--$slot->count; --$slot->count;
$this->inventory->setItemInHand($slot, $this); $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){ if($this->dead === true){
return; return;
} }
if($this->isCreative()){ if($this->isCreative()
if($source instanceof EntityDamageEvent){ and $source->getCause() !== EntityDamageEvent::CAUSE_MAGIC
$cause = $source->getCause(); and $source->getCause() !== EntityDamageEvent::CAUSE_SUICIDE
}else{ and $source->getCause() !== EntityDamageEvent::CAUSE_VOID
$cause = $source; ){
} $source->setCancelled();
}
if(
$cause !== EntityDamageEvent::CAUSE_MAGIC
and $cause !== EntityDamageEvent::CAUSE_SUICIDE
and $cause !== EntityDamageEvent::CAUSE_VOID
){
if($source instanceof EntityDamageEvent){
$source->setCancelled();
}
return;
}
}
parent::attack($damage, $source); parent::attack($damage, $source);
if($source instanceof EntityDamageEvent and $source->isCancelled()){ if($source->isCancelled()){
return; return;
} }elseif($this->getLastDamageCause() === $source){
if($this->getLastDamageCause() === $source){
$pk = new EntityEventPacket(); $pk = new EntityEventPacket();
$pk->eid = $this->getId(); $pk->eid = $this->getId();
$pk->event = 2; $pk->event = 2;

View File

@ -92,10 +92,10 @@ class Cake extends Transparent{
public function onActivate(Item $item, Player $player = null){ public function onActivate(Item $item, Player $player = null){
if($player instanceof Player and $player->getHealth() < 20){ if($player instanceof Player and $player->getHealth() < 20){
++$this->meta; ++$this->meta;
Server::getInstance()->getPluginManager()->callEvent($ev = new EntityRegainHealthEvent($player, 3, EntityRegainHealthEvent::CAUSE_EATING));
if(!$ev->isCancelled()){ $ev = new EntityRegainHealthEvent($player, 3, EntityRegainHealthEvent::CAUSE_EATING);
$player->heal($ev->getAmount(), $ev); $player->heal($ev->getAmount(), $ev);
}
if($this->meta >= 0x06){ if($this->meta >= 0x06){
$this->getLevel()->setBlock($this, new Air(), true); $this->getLevel()->setBlock($this, new Air(), true);
}else{ }else{

View File

@ -513,18 +513,42 @@ abstract class Entity extends Location implements Metadatable{
} }
/** /**
* @param float $damage * @param float $damage
* @param int|EntityDamageEvent $source * @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 float $amount
* @param int|EntityRegainHealthEvent $source * @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 * @return int
@ -556,10 +580,10 @@ abstract class Entity extends Location implements Metadatable{
} }
/** /**
* @param int|EntityDamageEvent $type * @param EntityDamageEvent $type
*/ */
public function setLastDamageCause($type){ public function setLastDamageCause(EntityDamageEvent $type){
$this->lastDamageCause = $type instanceof EntityDamageEvent ? $type : $type; $this->lastDamageCause = $type;
} }
/** /**

View File

@ -144,14 +144,6 @@ class FallingSand extends Entity{
$this->namedtag->Data = new Byte("Data", $this->damage); $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){ public function spawnTo(Player $player){
$pk = new AddEntityPacket(); $pk = new AddEntityPacket();
$pk->type = FallingSand::NETWORK_ID; $pk->type = FallingSand::NETWORK_ID;

View File

@ -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; 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(){ public function saveNBT(){
parent::saveNBT(); parent::saveNBT();
$this->namedtag->Item = new Compound("Item", [ $this->namedtag->Item = new Compound("Item", [

View File

@ -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; //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){ public function attack($damage, EntityDamageEvent $source){
if($this->hasEffect(Effect::FIRE_RESISTANCE) and $source){
return;
}
if($this->attackTime > 0 or $this->noDamageTicks > 0){ if($this->attackTime > 0 or $this->noDamageTicks > 0){
$lastCause = $this->getLastDamageCause(); $lastCause = $this->getLastDamageCause();
if($lastCause instanceof EntityDamageEvent and $lastCause->getDamage() >= $damage){ if($lastCause !== null and $lastCause->getDamage() >= $damage){
if($source instanceof EntityDamageEvent){ $source->setCancelled();
$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;
} }
} }
$this->setLastDamageCause($source); parent::attack($damage, $source);
if($source->isCancelled()){
return;
}
if($source instanceof EntityDamageByEntityEvent){ if($source instanceof EntityDamageByEntityEvent){
$e = $source->getDamager(); $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->knockBack($e, $damage, sin($yaw), cos($yaw), $source->getKnockBack());
} }
$this->setHealth($this->getHealth() - $damage);
$pk = new EntityEventPacket(); $pk = new EntityEventPacket();
$pk->eid = $this->getId(); $pk->eid = $this->getId();
$pk->event = $this->getHealth() <= 0 ? 3 : 2; //Ouch! $pk->event = $this->getHealth() <= 0 ? 3 : 2; //Ouch!
@ -141,10 +122,6 @@ abstract class Living extends Entity implements Damageable{
$this->setMotion($motion); $this->setMotion($motion);
} }
public function heal($amount, $source = EntityRegainHealthEvent::CAUSE_MAGIC){
$this->setHealth($this->getHealth() + $amount);
}
public function kill(){ public function kill(){
if($this->dead){ if($this->dead){
return; return;

View File

@ -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; 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(){ public function explode(){
$this->server->getPluginManager()->callEvent($ev = new ExplosionPrimeEvent($this, 4)); $this->server->getPluginManager()->callEvent($ev = new ExplosionPrimeEvent($this, 4));

View File

@ -59,14 +59,6 @@ abstract class Projectile extends Entity{
$this->namedtag->Age = new Short("Age", $this->age); $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){ public function onUpdate($currentTick){
if($this->closed){ if($this->closed){
return false; return false;