EntityDamageEvent and children now only fire if the attack is possible, moved event trigger to Entity->attack()

This commit is contained in:
Shoghi Cervantes 2014-10-29 16:02:40 +01:00
parent 289bc56b4b
commit 8a768cea33
10 changed files with 33 additions and 66 deletions

View File

@ -1901,7 +1901,9 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if($cancelled){
$ev->setCancelled();
}
$this->server->getPluginManager()->callEvent($ev);
$target->attack($ev->getFinalDamage(), $ev);
if($ev->isCancelled()){
if($item->isTool() and $this->isSurvival()){
$this->inventory->sendContents($this);
@ -1909,8 +1911,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
break;
}
$target->attack($ev->getFinalDamage(), $ev);
if($item->isTool() and $this->isSurvival()){
if($item->useOn($target) and $item->getDamage() >= $item->getMaxDurability()){
$this->inventory->setItemInHand(Item::get(Item::AIR, 0, 1), $this);
@ -2517,7 +2517,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if($this->dead === true){
return;
}
if(($this->getGamemode() & 0x01) === 1){
if($this->isCreative() === 1){
if($source instanceof EntityDamageEvent){
$cause = $source->getCause();
}else{
@ -2529,6 +2529,9 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
and $cause !== EntityDamageEvent::CAUSE_SUICIDE
and $cause !== EntityDamageEvent::CAUSE_VOID
){
if($source instanceof EntityDamageEvent){
$source->setCancelled();
}
return;
}
}
@ -2536,6 +2539,10 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
parent::attack($damage, $source);
if($source instanceof EntityDamageEvent and $source->isCancelled()){
return;
}
if($this->getLastDamageCause() === $source){
$pk = EntityEventPacket::getFromPool();
$pk->eid = 0;

View File

@ -56,10 +56,7 @@ class Cactus extends Transparent{
public function onEntityCollide(Entity $entity){
$ev = EntityDamageByBlockEvent::createEvent($this, $entity, EntityDamageEvent::CAUSE_CONTACT, 1);
Server::getInstance()->getPluginManager()->callEvent($ev);
if(!$ev->isCancelled()){
$entity->attack($ev->getFinalDamage(), $ev);
}
$entity->attack($ev->getFinalDamage(), $ev);
}
public function onUpdate($type){

View File

@ -47,10 +47,7 @@ class Fire extends Flowable{
public function onEntityCollide(Entity $entity){
$ev = EntityDamageByBlockEvent::createEvent($this, $entity, EntityDamageEvent::CAUSE_FIRE, 1);
Server::getInstance()->getPluginManager()->callEvent($ev);
if(!$ev->isCancelled()){
$entity->attack($ev->getFinalDamage(), $ev);
}
$entity->attack($ev->getFinalDamage(), $ev);
$ev = EntityCombustByBlockEvent::createEvent($this, $entity, 8);
Server::getInstance()->getPluginManager()->callEvent($ev);

View File

@ -39,10 +39,7 @@ class Lava extends Liquid{
public function onEntityCollide(Entity $entity){
$entity->fallDistance *= 0.5;
$ev = EntityDamageByBlockEvent::createEvent($this, $entity, EntityDamageEvent::CAUSE_LAVA, 4);
Server::getInstance()->getPluginManager()->callEvent($ev);
if(!$ev->isCancelled()){
$entity->attack($ev->getFinalDamage(), $ev);
}
$entity->attack($ev->getFinalDamage(), $ev);
$ev = EntityCombustByBlockEvent::createEvent($this, $entity, 15);
Server::getInstance()->getPluginManager()->callEvent($ev);

View File

@ -384,7 +384,7 @@ abstract class Entity extends Location implements Metadatable{
* @param int|EntityDamageEvent $type
*/
public function setLastDamageCause($type){
$this->lastDamageCause = $type;
$this->lastDamageCause = $type instanceof EntityDamageEvent ? clone $type : $type;
}
/**
@ -522,10 +522,8 @@ abstract class Entity extends Location implements Metadatable{
$this->checkBlockCollision();
if($this->y < 0 and $this->dead !== true){
$this->server->getPluginManager()->callEvent($ev = EntityDamageEvent::createEvent($this, EntityDamageEvent::CAUSE_VOID, 10));
if(!$ev->isCancelled()){
$this->attack($ev->getFinalDamage(), $ev);
}
$ev = EntityDamageEvent::createEvent($this, EntityDamageEvent::CAUSE_VOID, 10);
$this->attack($ev->getFinalDamage(), $ev);
$hasUpdate = true;
}
@ -538,10 +536,7 @@ abstract class Entity extends Location implements Metadatable{
}else{
if(($this->fireTicks % 20) === 0 or $tickDiff > 20){
$ev = EntityDamageEvent::createEvent($this, EntityDamageEvent::CAUSE_FIRE_TICK, 1);
$this->server->getPluginManager()->callEvent($ev);
if(!$ev->isCancelled()){
$this->attack($ev->getFinalDamage(), $ev);
}
$this->attack($ev->getFinalDamage(), $ev);
}
$this->fireTicks -= $tickDiff;
}
@ -714,10 +709,7 @@ abstract class Entity extends Location implements Metadatable{
public function fall($fallDistance){
$damage = floor($fallDistance - 3);
if($damage > 0){
$this->server->getPluginManager()->callEvent($ev = EntityDamageEvent::createEvent($this, EntityDamageEvent::CAUSE_FALL, $damage));
if($ev->isCancelled()){
return;
}
$ev = EntityDamageEvent::createEvent($this, EntityDamageEvent::CAUSE_FALL, $damage);
$this->attack($ev->getFinalDamage(), $ev);
}
}

View File

@ -208,21 +208,6 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
17 => ["type" => 6, "value" => [0, 0, 0]],
];
/*if($this->class === ENTITY_MOB and $this->type === MOB_SHEEP){
if(!isset($this->data["Sheared"])){
$this->data["Sheared"] = 0;
$this->data["Color"] = mt_rand(0,15);
}
$d[16]["value"] = (($this->data["Sheared"] == 1 ? 1:0) << 4) | ($this->data["Color"] & 0x0F);
}elseif($this->type === OBJECT_PRIMEDTNT){
$d[16]["value"] = (int) max(0, $this->data["fuse"] - (microtime(true) - $this->spawntime) * 20);
}elseif($this->class === ENTITY_PLAYER){
if($this->player->sleeping !== false){
$d[16]["value"] = 2;
$d[17]["value"] = array($this->player->sleeping->x, $this->player->sleeping->y, $this->player->sleeping->z);
}
}*/
return $d;
}

View File

@ -131,6 +131,13 @@ class Item extends Entity{
}
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);
}

View File

@ -69,8 +69,6 @@ abstract class Living extends Entity implements Damageable{
}
public function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC){
if($this->attackTime > 0){
$lastCause = $this->getLastDamageCause();
if($lastCause instanceof EntityDamageEvent and $lastCause->getDamage() >= $damage){
@ -152,10 +150,8 @@ abstract class Living extends Entity implements Damageable{
parent::entityBaseTick();
if($this->dead !== true and $this->isInsideOfSolid()){
$this->server->getPluginManager()->callEvent($ev = EntityDamageEvent::createEvent($this, EntityDamageEvent::CAUSE_SUFFOCATION, 1));
if(!$ev->isCancelled()){
$this->attack($ev->getFinalDamage(), $ev);
}
$ev = EntityDamageEvent::createEvent($this, EntityDamageEvent::CAUSE_SUFFOCATION, 1);
$this->attack($ev->getFinalDamage(), $ev);
}
if($this->dead !== true and $this->isInsideOfWater()){
@ -163,10 +159,8 @@ abstract class Living extends Entity implements Damageable{
if($this->airTicks <= -20){
$this->airTicks = 0;
$this->server->getPluginManager()->callEvent($ev = EntityDamageEvent::createEvent($this, EntityDamageEvent::CAUSE_DROWNING, 2));
if(!$ev->isCancelled()){
$this->attack($ev->getFinalDamage(), $ev);
}
$ev = EntityDamageEvent::createEvent($this, EntityDamageEvent::CAUSE_DROWNING, 2);
$this->attack($ev->getFinalDamage(), $ev);
}
}else{
$this->airTicks = 300;

View File

@ -136,12 +136,8 @@ abstract class Projectile extends Entity{
$ev = EntityDamageByEntityEvent::createEvent($this->shootingEntity === null ? $this : $this->shootingEntity, $movingObjectPosition->entityHit, EntityDamageEvent::CAUSE_PROJECTILE, $damage);
$movingObjectPosition->entityHit->attack($ev->getFinalDamage(), $ev);
$this->server->getPluginManager()->callEvent($ev);
if(!$ev->isCancelled()){
$movingObjectPosition->entityHit->attack($ev->getFinalDamage(), $ev);
}
if($this->fireTicks > 0){
$ev = EntityCombustByEntityEvent::createEvent($this, $movingObjectPosition->entityHit, 5);

View File

@ -172,13 +172,8 @@ class Explosion{
$ev = EntityDamageEvent::createEvent($entity, EntityDamageEvent::CAUSE_BLOCK_EXPLOSION, $damage);
}
$this->level->getServer()->getPluginManager()->callEvent($ev);
if(!$ev->isCancelled()){
$entity->attack($ev->getFinalDamage(), $ev);
$entity->setMotion($motion->multiply($impact));
}
$entity->attack($ev->getFinalDamage(), $ev);
$entity->setMotion($motion->multiply($impact));
}
}