diff --git a/src/pocketmine/entity/Effect.php b/src/pocketmine/entity/Effect.php index 1f4bbc8fa..3b97c1405 100644 --- a/src/pocketmine/entity/Effect.php +++ b/src/pocketmine/entity/Effect.php @@ -22,6 +22,8 @@ namespace pocketmine\entity; use pocketmine\event\entity\EntityDamageEvent; +use pocketmine\event\entity\EntityEffectAddEvent; +use pocketmine\event\entity\EntityEffectRemoveEvent; use pocketmine\event\entity\EntityRegainHealthEvent; use pocketmine\event\player\PlayerExhaustEvent; use pocketmine\network\protocol\MobEffectPacket; @@ -251,6 +253,10 @@ class Effect{ } public function add(Entity $entity, $modify = false, Effect $oldEffect = null){ + $entity->getLevel()->getServer()->getPluginManager()->callEvent($ev = new EntityEffectAddEvent($entity, $this, $modify, $oldEffect)); + if($ev->isCancelled()){ + return; + } if($entity instanceof Player){ $pk = new MobEffectPacket(); $pk->eid = 0; @@ -258,7 +264,7 @@ class Effect{ $pk->amplifier = $this->getAmplifier(); $pk->particles = $this->isVisible(); $pk->duration = $this->getDuration(); - if($modify){ + if($ev->willModify()){ $pk->eventId = MobEffectPacket::EVENT_MODIFY; }else{ $pk->eventId = MobEffectPacket::EVENT_ADD; @@ -272,7 +278,7 @@ class Effect{ $entity->setNameTagVisible(false); }elseif($this->id === Effect::SPEED){ $attr = $entity->getAttributeMap()->getAttribute(Attribute::MOVEMENT_SPEED); - if($modify and $oldEffect !== null){ + if($ev->willModify() and $oldEffect !== null){ $speed = $attr->getValue() / (1 + 0.2 * $oldEffect->getAmplifier()); }else{ $speed = $attr->getValue(); @@ -281,7 +287,7 @@ class Effect{ $attr->setValue($speed); }elseif($this->id === Effect::SLOWNESS){ $attr = $entity->getAttributeMap()->getAttribute(Attribute::MOVEMENT_SPEED); - if($modify and $oldEffect !== null){ + if($ev->willModify() and $oldEffect !== null){ $speed = $attr->getValue() / (1 - 0.15 * $oldEffect->getAmplifier()); }else{ $speed = $attr->getValue(); @@ -292,6 +298,10 @@ class Effect{ } public function remove(Entity $entity){ + $entity->getLevel()->getServer()->getPluginManager()->callEvent($ev = new EntityEffectRemoveEvent($entity, $this)); + if($ev->isCancelled()){ + return; + } if($entity instanceof Player){ $pk = new MobEffectPacket(); $pk->eid = 0; diff --git a/src/pocketmine/event/entity/EntityEffectAddEvent.php b/src/pocketmine/event/entity/EntityEffectAddEvent.php new file mode 100644 index 000000000..715a1df1f --- /dev/null +++ b/src/pocketmine/event/entity/EntityEffectAddEvent.php @@ -0,0 +1,61 @@ +modify = $modify; + $this->oldEffect = $oldEffect; + } + + public function willModify() : bool{ + return $this->modify; + } + + public function setWillModify(bool $modify){ + $this->modify = $modify; + } + + public function hasOldEffect() : bool{ + return $this->oldEffect instanceof Effect; + } + + /** + * @return Effect|null + */ + public function getOldEffect(){ + return $this->oldEffect; + } + + +} \ No newline at end of file diff --git a/src/pocketmine/event/entity/EntityEffectEvent.php b/src/pocketmine/event/entity/EntityEffectEvent.php new file mode 100644 index 000000000..55fb1b895 --- /dev/null +++ b/src/pocketmine/event/entity/EntityEffectEvent.php @@ -0,0 +1,41 @@ +entity = $entity; + $this->effect = $effect; + } + + public function getEffect() : Effect{ + return $this->effect; + } +} \ No newline at end of file diff --git a/src/pocketmine/event/entity/EntityEffectRemoveEvent.php b/src/pocketmine/event/entity/EntityEffectRemoveEvent.php new file mode 100644 index 000000000..9b0c84475 --- /dev/null +++ b/src/pocketmine/event/entity/EntityEffectRemoveEvent.php @@ -0,0 +1,27 @@ +