Cleaned up Effect handling and replacement

This commit is contained in:
Dylan K. Taylor 2017-08-30 12:53:41 +01:00
parent bde6d7db8d
commit 21f09d5fdb
3 changed files with 18 additions and 39 deletions

View File

@ -396,11 +396,10 @@ class Effect{
* Adds this effect to the Entity, performing effect overriding as specified. * Adds this effect to the Entity, performing effect overriding as specified.
* *
* @param Entity $entity * @param Entity $entity
* @param bool $modify
* @param Effect|null $oldEffect * @param Effect|null $oldEffect
*/ */
public function add(Entity $entity, bool $modify = false, Effect $oldEffect = null){ public function add(Entity $entity, Effect $oldEffect = null){
$entity->getLevel()->getServer()->getPluginManager()->callEvent($ev = new EntityEffectAddEvent($entity, $this, $modify, $oldEffect)); $entity->getLevel()->getServer()->getPluginManager()->callEvent($ev = new EntityEffectAddEvent($entity, $this, $oldEffect));
if($ev->isCancelled()){ if($ev->isCancelled()){
return; return;
} }
@ -411,7 +410,7 @@ class Effect{
$pk->amplifier = $this->getAmplifier(); $pk->amplifier = $this->getAmplifier();
$pk->particles = $this->isVisible(); $pk->particles = $this->isVisible();
$pk->duration = $this->getDuration(); $pk->duration = $this->getDuration();
if($ev->willModify()){ if($oldEffect !== null){
$pk->eventId = MobEffectPacket::EVENT_MODIFY; $pk->eventId = MobEffectPacket::EVENT_MODIFY;
}else{ }else{
$pk->eventId = MobEffectPacket::EVENT_ADD; $pk->eventId = MobEffectPacket::EVENT_ADD;
@ -420,6 +419,10 @@ class Effect{
$entity->dataPacket($pk); $entity->dataPacket($pk);
} }
if($oldEffect !== null){
$oldEffect->remove($entity, false);
}
switch($this->id){ switch($this->id){
case Effect::INVISIBILITY: case Effect::INVISIBILITY:
$entity->setGenericFlag(Entity::DATA_FLAG_INVISIBLE, true); $entity->setGenericFlag(Entity::DATA_FLAG_INVISIBLE, true);
@ -427,35 +430,16 @@ class Effect{
break; break;
case Effect::SPEED: case Effect::SPEED:
$attr = $entity->getAttributeMap()->getAttribute(Attribute::MOVEMENT_SPEED); $attr = $entity->getAttributeMap()->getAttribute(Attribute::MOVEMENT_SPEED);
if($ev->willModify() and $oldEffect !== null){ $attr->setValue($attr->getValue() * (1 + 0.2 * $this->getEffectLevel()));
$speed = $attr->getValue() / (1 + 0.2 * $oldEffect->getEffectLevel());
}else{
$speed = $attr->getValue();
}
$speed *= (1 + 0.2 * $this->getEffectLevel());
$attr->setValue($speed);
break; break;
case Effect::SLOWNESS: case Effect::SLOWNESS:
$attr = $entity->getAttributeMap()->getAttribute(Attribute::MOVEMENT_SPEED); $attr = $entity->getAttributeMap()->getAttribute(Attribute::MOVEMENT_SPEED);
if($ev->willModify() and $oldEffect !== null){ $attr->setValue($attr->getValue() * (1 - 0.15 * $this->getEffectLevel()), true);
$speed = $attr->getValue() / (1 - 0.15 * $oldEffect->getEffectLevel());
}else{
$speed = $attr->getValue();
}
$speed *= (1 - 0.15 * $this->getEffectLevel());
$attr->setValue($speed, true);
break; break;
case Effect::HEALTH_BOOST: case Effect::HEALTH_BOOST:
$attr = $entity->getAttributeMap()->getAttribute(Attribute::HEALTH); $attr = $entity->getAttributeMap()->getAttribute(Attribute::HEALTH);
if($ev->willModify() and $oldEffect !== null){ $attr->setMaxValue($attr->getMaxValue() + 4 * $this->getEffectLevel());
$max = $attr->getMaxValue() - (4 * $oldEffect->getEffectLevel());
}else{
$max = $attr->getMaxValue();
}
$max += (4 * $this->getEffectLevel());
$attr->setMaxValue($max);
break; break;
case Effect::ABSORPTION: case Effect::ABSORPTION:
$new = (4 * $this->getEffectLevel()); $new = (4 * $this->getEffectLevel());
@ -470,13 +454,15 @@ class Effect{
* Removes the effect from the entity, resetting any changed values back to their original defaults. * Removes the effect from the entity, resetting any changed values back to their original defaults.
* *
* @param Entity $entity * @param Entity $entity
* @param bool $send
*/ */
public function remove(Entity $entity){ public function remove(Entity $entity, bool $send = true){
$entity->getLevel()->getServer()->getPluginManager()->callEvent($ev = new EntityEffectRemoveEvent($entity, $this)); $entity->getLevel()->getServer()->getPluginManager()->callEvent($ev = new EntityEffectRemoveEvent($entity, $this));
if($ev->isCancelled()){ if($ev->isCancelled()){
return; return;
} }
if($entity instanceof Player){
if($send and $entity instanceof Player){
$pk = new MobEffectPacket(); $pk = new MobEffectPacket();
$pk->entityRuntimeId = $entity->getId(); $pk->entityRuntimeId = $entity->getId();
$pk->eventId = MobEffectPacket::EVENT_REMOVE; $pk->eventId = MobEffectPacket::EVENT_REMOVE;

View File

@ -237,9 +237,9 @@ abstract class Living extends Entity implements Damageable{
){ ){
return; return;
} }
$effect->add($this, true, $oldEffect); $effect->add($this, $oldEffect);
}else{ }else{
$effect->add($this, false); $effect->add($this);
} }
$this->effects[$effect->getId()] = $effect; $this->effects[$effect->getId()] = $effect;

View File

@ -32,32 +32,26 @@ use pocketmine\entity\Entity;
class EntityEffectAddEvent extends EntityEffectEvent{ class EntityEffectAddEvent extends EntityEffectEvent{
public static $handlerList = null; public static $handlerList = null;
/** @var bool */
private $modify;
/** @var Effect|null */ /** @var Effect|null */
private $oldEffect; private $oldEffect;
/** /**
* @param Entity $entity * @param Entity $entity
* @param Effect $effect * @param Effect $effect
* @param bool $modify
* @param Effect|null $oldEffect * @param Effect|null $oldEffect
*/ */
public function __construct(Entity $entity, Effect $effect, bool $modify, Effect $oldEffect = null){ public function __construct(Entity $entity, Effect $effect, Effect $oldEffect = null){
parent::__construct($entity, $effect); parent::__construct($entity, $effect);
$this->modify = $modify;
$this->oldEffect = $oldEffect; $this->oldEffect = $oldEffect;
} }
/** /**
* Returns whether the effect addition will replace an existing effect already applied to the entity. * Returns whether the effect addition will replace an existing effect already applied to the entity.
* *
* TODO: isn't this pointless? An effect will only modify an existing effect if oldEffect is non-null anyway...
*
* @return bool * @return bool
*/ */
public function willModify() : bool{ public function willModify() : bool{
return $this->modify; return $this->hasOldEffect();
} }
/** /**
@ -74,5 +68,4 @@ class EntityEffectAddEvent extends EntityEffectEvent{
return $this->oldEffect; return $this->oldEffect;
} }
} }