EntityDamageEvent: break API

The general purpose of this is to split up base damage from modifiers.

- Added methods getBaseDamage(), setBaseDamage(), getOriginalBaseDamage(), getModifiers(), getOriginalModifiers()
- setDamage() renamed to setModifier() and type is now mandatory
- getDamage() renamed to getModifier() and type is now mandatory
- getOriginalDamage() renamed to getOriginalModifier() and type is now mandatory
- Removed MODIFIER_BASE constant
- Constructors now accept: float baseDamage, float[] modifiers instead of just float[] modifiers
This commit is contained in:
Dylan K. Taylor 2018-05-22 19:05:25 +01:00
parent c9ed517063
commit faa88a55e4
6 changed files with 94 additions and 52 deletions

View File

@ -2506,7 +2506,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
}
if(!$this->isSprinting() and !$this->isFlying() and $this->fallDistance > 0 and !$this->hasEffect(Effect::BLINDNESS) and !$this->isUnderwater()){
$ev->setDamage($ev->getFinalDamage() / 2, EntityDamageEvent::MODIFIER_CRITICAL);
$ev->setModifier($ev->getFinalDamage() / 2, EntityDamageEvent::MODIFIER_CRITICAL);
}
$target->attack($ev);
@ -2518,7 +2518,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
return true;
}
if($ev->getDamage(EntityDamageEvent::MODIFIER_CRITICAL) > 0){
if($ev->getModifier(EntityDamageEvent::MODIFIER_CRITICAL) > 0){
$pk = new AnimatePacket();
$pk->action = AnimatePacket::ACTION_CRITICAL_HIT;
$pk->entityRuntimeId = $target->getId();

View File

@ -440,12 +440,12 @@ abstract class Living extends Entity implements Damageable{
public function applyDamageModifiers(EntityDamageEvent $source) : void{
if($source->canBeReducedByArmor()){
//MCPE uses the same system as PC did pre-1.9
$source->setDamage(-$source->getFinalDamage() * $this->getArmorPoints() * 0.04, EntityDamageEvent::MODIFIER_ARMOR);
$source->setModifier(-$source->getFinalDamage() * $this->getArmorPoints() * 0.04, EntityDamageEvent::MODIFIER_ARMOR);
}
$cause = $source->getCause();
if($this->hasEffect(Effect::DAMAGE_RESISTANCE) and $cause !== EntityDamageEvent::CAUSE_VOID and $cause !== EntityDamageEvent::CAUSE_SUICIDE){
$source->setDamage(-$source->getFinalDamage() * min(1, 0.2 * $this->getEffect(Effect::DAMAGE_RESISTANCE)->getEffectLevel()), EntityDamageEvent::MODIFIER_RESISTANCE);
$source->setModifier(-$source->getFinalDamage() * min(1, 0.2 * $this->getEffect(Effect::DAMAGE_RESISTANCE)->getEffectLevel()), EntityDamageEvent::MODIFIER_RESISTANCE);
}
$totalEpf = 0;
@ -454,9 +454,9 @@ abstract class Living extends Entity implements Damageable{
$totalEpf += $item->getEnchantmentProtectionFactor($source);
}
}
$source->setDamage(-$source->getFinalDamage() * min(ceil(min($totalEpf, 25) * (mt_rand(50, 100) / 100)), 20) * 0.04, EntityDamageEvent::MODIFIER_ARMOR_ENCHANTMENTS);
$source->setModifier(-$source->getFinalDamage() * min(ceil(min($totalEpf, 25) * (mt_rand(50, 100) / 100)), 20) * 0.04, EntityDamageEvent::MODIFIER_ARMOR_ENCHANTMENTS);
$source->setDamage(-min($this->getAbsorption(), $source->getFinalDamage()), EntityDamageEvent::MODIFIER_ABSORPTION);
$source->setModifier(-min($this->getAbsorption(), $source->getFinalDamage()), EntityDamageEvent::MODIFIER_ABSORPTION);
}
/**
@ -466,8 +466,8 @@ abstract class Living extends Entity implements Damageable{
* @param EntityDamageEvent $source
*/
protected function applyPostDamageEffects(EntityDamageEvent $source) : void{
$this->setAbsorption(max(0, $this->getAbsorption() + $source->getDamage(EntityDamageEvent::MODIFIER_ABSORPTION)));
$this->damageArmor($source->getDamage(EntityDamageEvent::MODIFIER_BASE));
$this->setAbsorption(max(0, $this->getAbsorption() + $source->getModifier(EntityDamageEvent::MODIFIER_ABSORPTION)));
$this->damageArmor($source->getBaseDamage());
}
/**
@ -495,7 +495,7 @@ abstract class Living extends Entity implements Damageable{
public function attack(EntityDamageEvent $source) : void{
if($this->attackTime > 0 or $this->noDamageTicks > 0){
$lastCause = $this->getLastDamageCause();
if($lastCause !== null and $lastCause->getDamage() >= $source->getDamage()){
if($lastCause !== null and $lastCause->getBaseDamage() >= $source->getBaseDamage()){
$source->setCancelled();
}
}
@ -540,7 +540,7 @@ abstract class Living extends Entity implements Damageable{
$deltaX = $this->x - $e->x;
$deltaZ = $this->z - $e->z;
$this->knockBack($e, $source->getDamage(), $deltaX, $deltaZ, $source->getKnockBack());
$this->knockBack($e, $source->getBaseDamage(), $deltaX, $deltaZ, $source->getKnockBack());
}
}

View File

@ -34,14 +34,15 @@ class EntityDamageByBlockEvent extends EntityDamageEvent{
private $damager;
/**
* @param Block $damager
* @param Entity $entity
* @param int $cause
* @param float|float[] $damage
* @param Block $damager
* @param Entity $entity
* @param int $cause
* @param float $damage
* @param float[] $modifiers
*/
public function __construct(Block $damager, Entity $entity, int $cause, $damage){
public function __construct(Block $damager, Entity $entity, int $cause, float $damage, array $modifiers = []){
$this->damager = $damager;
parent::__construct($entity, $cause, $damage);
parent::__construct($entity, $cause, $damage, $modifiers);
}
/**

View File

@ -33,15 +33,16 @@ class EntityDamageByChildEntityEvent extends EntityDamageByEntityEvent{
private $childEntityEid;
/**
* @param Entity $damager
* @param Entity $childEntity
* @param Entity $entity
* @param int $cause
* @param float|float[] $damage
* @param Entity $damager
* @param Entity $childEntity
* @param Entity $entity
* @param int $cause
* @param float $damage
* @param float[] $modifiers
*/
public function __construct(Entity $damager, Entity $childEntity, Entity $entity, int $cause, $damage){
public function __construct(Entity $damager, Entity $childEntity, Entity $entity, int $cause, float $damage, array $modifiers = []){
$this->childEntityEid = $childEntity->getId();
parent::__construct($damager, $entity, $cause, $damage);
parent::__construct($damager, $entity, $cause, $damage, $modifiers);
}
/**

View File

@ -37,27 +37,28 @@ class EntityDamageByEntityEvent extends EntityDamageEvent{
private $knockBack;
/**
* @param Entity $damager
* @param Entity $entity
* @param int $cause
* @param float|float[] $damage
* @param float $knockBack
* @param Entity $damager
* @param Entity $entity
* @param int $cause
* @param float $damage
* @param float[] $modifiers
* @param float $knockBack
*/
public function __construct(Entity $damager, Entity $entity, int $cause, $damage, float $knockBack = 0.4){
public function __construct(Entity $damager, Entity $entity, int $cause, float $damage, array $modifiers = [], float $knockBack = 0.4){
$this->damagerEntityId = $damager->getId();
$this->knockBack = $knockBack;
parent::__construct($entity, $cause, $damage);
parent::__construct($entity, $cause, $damage, $modifiers);
$this->addAttackerModifiers($damager);
}
protected function addAttackerModifiers(Entity $damager){
if($damager instanceof Living){ //TODO: move this to entity classes
if($damager->hasEffect(Effect::STRENGTH)){
$this->setDamage($this->getDamage(self::MODIFIER_BASE) * 0.3 * $damager->getEffect(Effect::STRENGTH)->getEffectLevel(), self::MODIFIER_STRENGTH);
$this->setModifier($this->getBaseDamage() * 0.3 * $damager->getEffect(Effect::STRENGTH)->getEffectLevel(), self::MODIFIER_STRENGTH);
}
if($damager->hasEffect(Effect::WEAKNESS)){
$this->setDamage(-($this->getDamage(self::MODIFIER_BASE) * 0.2 * $damager->getEffect(Effect::WEAKNESS)->getEffectLevel()), self::MODIFIER_WEAKNESS);
$this->setModifier(-($this->getBaseDamage() * 0.2 * $damager->getEffect(Effect::WEAKNESS)->getEffectLevel()), self::MODIFIER_WEAKNESS);
}
}
}

View File

@ -30,7 +30,6 @@ use pocketmine\event\Cancellable;
* Called when an entity takes damage.
*/
class EntityDamageEvent extends EntityEvent implements Cancellable{
public const MODIFIER_BASE = 0;
public const MODIFIER_ARMOR = 1;
public const MODIFIER_STRENGTH = 2;
public const MODIFIER_WEAKNESS = 3;
@ -58,6 +57,11 @@ class EntityDamageEvent extends EntityEvent implements Cancellable{
/** @var int */
private $cause;
/** @var float */
private $baseDamage;
/** @var float */
private $originalBase;
/** @var float[] */
private $modifiers;
/** @var float[] */
@ -67,24 +71,16 @@ class EntityDamageEvent extends EntityEvent implements Cancellable{
/**
* @param Entity $entity
* @param int $cause
* @param float|float[] $damage
* @param float $damage
* @param float|float[] $modifiers
*/
public function __construct(Entity $entity, int $cause, $damage){
public function __construct(Entity $entity, int $cause, float $damage, array $modifiers = []){
$this->entity = $entity;
$this->cause = $cause;
if(is_array($damage)){
$this->modifiers = $damage;
}else{
$this->modifiers = [
self::MODIFIER_BASE => $damage
];
}
$this->baseDamage = $this->originalBase = $damage;
$this->modifiers = $modifiers;
$this->originals = $this->modifiers;
if(!isset($this->modifiers[self::MODIFIER_BASE])){
throw new \InvalidArgumentException("BASE Damage modifier missing");
}
}
/**
@ -95,12 +91,39 @@ class EntityDamageEvent extends EntityEvent implements Cancellable{
}
/**
* @param int $type
* Returns the base amount of damage applied, before modifiers.
*
* @return float
*/
public function getOriginalDamage(int $type = self::MODIFIER_BASE) : float{
return $this->originals[$type] ?? 0.0;
public function getBaseDamage() : float{
return $this->baseDamage;
}
/**
* Sets the base amount of damage applied, optionally recalculating modifiers.
*
* TODO: add ability to recalculate modifiers when this is set
*
* @param float $damage
*/
public function setBaseDamage(float $damage) : void{
$this->baseDamage = $damage;
}
/**
* Returns the original base amount of damage applied, before alterations by plugins.
*
* @return float
*/
public function getOriginalBaseDamage() : float{
return $this->originalBase;
}
/**
* @return float[]
*/
public function getOriginalModifiers() : array{
return $this->originals;
}
/**
@ -108,7 +131,23 @@ class EntityDamageEvent extends EntityEvent implements Cancellable{
*
* @return float
*/
public function getDamage(int $type = self::MODIFIER_BASE) : float{
public function getOriginalModifier(int $type) : float{
return $this->originals[$type] ?? 0.0;
}
/**
* @return float[]
*/
public function getModifiers() : array{
return $this->modifiers;
}
/**
* @param int $type
*
* @return float
*/
public function getModifier(int $type) : float{
return $this->modifiers[$type] ?? 0.0;
}
@ -116,7 +155,7 @@ class EntityDamageEvent extends EntityEvent implements Cancellable{
* @param float $damage
* @param int $type
*/
public function setDamage(float $damage, int $type = self::MODIFIER_BASE){
public function setModifier(float $damage, int $type){
$this->modifiers[$type] = $damage;
}
@ -133,7 +172,7 @@ class EntityDamageEvent extends EntityEvent implements Cancellable{
* @return float
*/
public function getFinalDamage() : float{
return array_sum($this->modifiers);
return $this->baseDamage + array_sum($this->modifiers);
}
/**