mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-27 13:49:55 +00:00
Improved Entity attack()/heal() event firing
This commit is contained in:
parent
d1760d9bb8
commit
d19631226f
@ -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()){
|
||||
$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($this->isCreative()
|
||||
and $source->getCause() !== EntityDamageEvent::CAUSE_MAGIC
|
||||
and $source->getCause() !== EntityDamageEvent::CAUSE_SUICIDE
|
||||
and $source->getCause() !== EntityDamageEvent::CAUSE_VOID
|
||||
){
|
||||
if($source instanceof EntityDamageEvent){
|
||||
$source->setCancelled();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
|
@ -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()){
|
||||
|
||||
$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{
|
||||
|
@ -514,17 +514,41 @@ abstract class Entity extends Location implements Metadatable{
|
||||
|
||||
/**
|
||||
* @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 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
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
|
@ -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", [
|
||||
|
@ -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){
|
||||
if($lastCause !== null and $lastCause->getDamage() >= $damage){
|
||||
$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){
|
||||
$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;
|
||||
|
@ -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));
|
||||
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user