Implemented Absorption effect

This is a little buggy due to a client-sided bug. https://bugs.mojang.com/browse/MCPE-20520
TODO: add attribute save/restore
This commit is contained in:
Dylan K. Taylor 2017-03-21 13:23:57 +00:00
parent c21768df26
commit 940b20c191
3 changed files with 46 additions and 1 deletions

View File

@ -427,6 +427,7 @@ class Effect{
$speed *= (1 - 0.15 * $this->amplifier); $speed *= (1 - 0.15 * $this->amplifier);
$attr->setValue($speed, true); $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){ if($ev->willModify() and $oldEffect !== null){
@ -438,6 +439,17 @@ class Effect{
$max += (4 * ($this->amplifier + 1)); $max += (4 * ($this->amplifier + 1));
$attr->setMaxValue($max); $attr->setMaxValue($max);
break; break;
case Effect::ABSORPTION:
if($ev->willModify() and $oldEffect !== null){
$value = $entity->getAbsorption() - (4 * ($oldEffect->getAmplifier() + 1));
}else{
$value = $entity->getAbsorption();
}
$value += (4 * ($this->amplifier + 1));
$entity->setAbsorption($value);
break;
} }
} }
@ -477,6 +489,9 @@ class Effect{
$attr = $entity->getAttributeMap()->getAttribute(Attribute::HEALTH); $attr = $entity->getAttributeMap()->getAttribute(Attribute::HEALTH);
$attr->setMaxValue($attr->getMaxValue() - (4 * ($this->amplifier + 1))); $attr->setMaxValue($attr->getMaxValue() - (4 * ($this->amplifier + 1)));
break; break;
case Effect::ABSORPTION:
$entity->setAbsorption($entity->getAbsorption() - (4 * ($this->amplifier + 1)));
break;
} }
} }
} }

View File

@ -735,7 +735,21 @@ abstract class Entity extends Location implements Metadatable{
$this->setLastDamageCause($source); $this->setLastDamageCause($source);
$this->setHealth($this->getHealth() - $source->getFinalDamage()); $damage = $source->getFinalDamage();
$absorption = $this->getAbsorption();
if($absorption > 0){
if($absorption > $damage){
//Use absorption health before normal health.
$this->setAbsorption($absorption - $damage);
$damage = 0;
}else{
$this->setAbsorption(0);
$damage -= $absorption;
}
}
$this->setHealth($this->getHealth() - $damage);
} }
/** /**
@ -785,6 +799,14 @@ abstract class Entity extends Location implements Metadatable{
} }
} }
public function getAbsorption() : int{
return 0;
}
public function setAbsorption(int $absorption){
}
/** /**
* @param EntityDamageEvent $type * @param EntityDamageEvent $type
*/ */

View File

@ -85,6 +85,14 @@ abstract class Living extends Entity implements Damageable{
$this->attributeMap->getAttribute(Attribute::HEALTH)->setMaxValue($amount); $this->attributeMap->getAttribute(Attribute::HEALTH)->setMaxValue($amount);
} }
public function getAbsorption() : int{
return (int) $this->attributeMap->getAttribute(Attribute::ABSORPTION)->getValue();
}
public function setAbsorption(int $absorption){
$this->attributeMap->getAttribute(Attribute::ABSORPTION)->setValue($absorption);
}
public function saveNBT(){ public function saveNBT(){
parent::saveNBT(); parent::saveNBT();
$this->namedtag->Health = new ShortTag("Health", $this->getHealth()); $this->namedtag->Health = new ShortTag("Health", $this->getHealth());