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.
*
* @param Entity $entity
* @param bool $modify
* @param Effect|null $oldEffect
*/
public function add(Entity $entity, bool $modify = false, Effect $oldEffect = null){
$entity->getLevel()->getServer()->getPluginManager()->callEvent($ev = new EntityEffectAddEvent($entity, $this, $modify, $oldEffect));
public function add(Entity $entity, Effect $oldEffect = null){
$entity->getLevel()->getServer()->getPluginManager()->callEvent($ev = new EntityEffectAddEvent($entity, $this, $oldEffect));
if($ev->isCancelled()){
return;
}
@ -411,7 +410,7 @@ class Effect{
$pk->amplifier = $this->getAmplifier();
$pk->particles = $this->isVisible();
$pk->duration = $this->getDuration();
if($ev->willModify()){
if($oldEffect !== null){
$pk->eventId = MobEffectPacket::EVENT_MODIFY;
}else{
$pk->eventId = MobEffectPacket::EVENT_ADD;
@ -420,6 +419,10 @@ class Effect{
$entity->dataPacket($pk);
}
if($oldEffect !== null){
$oldEffect->remove($entity, false);
}
switch($this->id){
case Effect::INVISIBILITY:
$entity->setGenericFlag(Entity::DATA_FLAG_INVISIBLE, true);
@ -427,35 +430,16 @@ class Effect{
break;
case Effect::SPEED:
$attr = $entity->getAttributeMap()->getAttribute(Attribute::MOVEMENT_SPEED);
if($ev->willModify() and $oldEffect !== null){
$speed = $attr->getValue() / (1 + 0.2 * $oldEffect->getEffectLevel());
}else{
$speed = $attr->getValue();
}
$speed *= (1 + 0.2 * $this->getEffectLevel());
$attr->setValue($speed);
$attr->setValue($attr->getValue() * (1 + 0.2 * $this->getEffectLevel()));
break;
case Effect::SLOWNESS:
$attr = $entity->getAttributeMap()->getAttribute(Attribute::MOVEMENT_SPEED);
if($ev->willModify() and $oldEffect !== null){
$speed = $attr->getValue() / (1 - 0.15 * $oldEffect->getEffectLevel());
}else{
$speed = $attr->getValue();
}
$speed *= (1 - 0.15 * $this->getEffectLevel());
$attr->setValue($speed, true);
$attr->setValue($attr->getValue() * (1 - 0.15 * $this->getEffectLevel()), true);
break;
case Effect::HEALTH_BOOST:
$attr = $entity->getAttributeMap()->getAttribute(Attribute::HEALTH);
if($ev->willModify() and $oldEffect !== null){
$max = $attr->getMaxValue() - (4 * $oldEffect->getEffectLevel());
}else{
$max = $attr->getMaxValue();
}
$max += (4 * $this->getEffectLevel());
$attr->setMaxValue($max);
$attr->setMaxValue($attr->getMaxValue() + 4 * $this->getEffectLevel());
break;
case Effect::ABSORPTION:
$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.
*
* @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));
if($ev->isCancelled()){
return;
}
if($entity instanceof Player){
if($send and $entity instanceof Player){
$pk = new MobEffectPacket();
$pk->entityRuntimeId = $entity->getId();
$pk->eventId = MobEffectPacket::EVENT_REMOVE;

View File

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

View File

@ -32,32 +32,26 @@ use pocketmine\entity\Entity;
class EntityEffectAddEvent extends EntityEffectEvent{
public static $handlerList = null;
/** @var bool */
private $modify;
/** @var Effect|null */
private $oldEffect;
/**
* @param Entity $entity
* @param Effect $effect
* @param bool $modify
* @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);
$this->modify = $modify;
$this->oldEffect = $oldEffect;
}
/**
* 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
*/
public function willModify() : bool{
return $this->modify;
return $this->hasOldEffect();
}
/**
@ -74,5 +68,4 @@ class EntityEffectAddEvent extends EntityEffectEvent{
return $this->oldEffect;
}
}