Refactor of health int -> float and fixed armor being useless when

computed damage reduction is less than 1
This commit is contained in:
Dylan K. Taylor 2017-08-30 11:03:07 +01:00
parent ea5bd0348a
commit fd52022065
8 changed files with 61 additions and 71 deletions

View File

@ -2276,7 +2276,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
$points += $armorItem->getDefensePoints(); $points += $armorItem->getDefensePoints();
} }
$damage[EntityDamageEvent::MODIFIER_ARMOR] = -floor($damage[EntityDamageEvent::MODIFIER_BASE] * $points * 0.04); $damage[EntityDamageEvent::MODIFIER_ARMOR] = -($damage[EntityDamageEvent::MODIFIER_BASE] * $points * 0.04);
} }
$ev = new EntityDamageByEntityEvent($this, $target, EntityDamageEvent::CAUSE_ENTITY_ATTACK, $damage); $ev = new EntityDamageByEntityEvent($this, $target, EntityDamageEvent::CAUSE_ENTITY_ATTACK, $damage);

View File

@ -300,8 +300,8 @@ abstract class Entity extends Location implements Metadatable{
/** @var float */ /** @var float */
protected $baseOffset = 0.0; protected $baseOffset = 0.0;
/** @var int */ /** @var float */
private $health = 20; private $health = 20.0;
private $maxHealth = 20; private $maxHealth = 20;
/** @var float */ /** @var float */
@ -921,9 +921,9 @@ abstract class Entity extends Location implements Metadatable{
} }
/** /**
* @return int * @return float
*/ */
public function getHealth(){ public function getHealth() : float{
return $this->health; return $this->health;
} }
@ -934,11 +934,10 @@ abstract class Entity extends Location implements Metadatable{
/** /**
* Sets the health of the Entity. This won't send any update to the players * Sets the health of the Entity. This won't send any update to the players
* *
* @param int $amount * @param float $amount
*/ */
public function setHealth($amount){ public function setHealth(float $amount){
$amount = (int) $amount; if($amount == $this->health){
if($amount === $this->health){
return; return;
} }
@ -947,7 +946,7 @@ abstract class Entity extends Location implements Metadatable{
$this->kill(); $this->kill();
} }
}elseif($amount <= $this->getMaxHealth() or $amount < $this->health){ }elseif($amount <= $this->getMaxHealth() or $amount < $this->health){
$this->health = (int) $amount; $this->health = $amount;
}else{ }else{
$this->health = $this->getMaxHealth(); $this->health = $this->getMaxHealth();
} }
@ -982,15 +981,15 @@ abstract class Entity extends Location implements Metadatable{
/** /**
* @return int * @return int
*/ */
public function getMaxHealth(){ public function getMaxHealth() : int{
return $this->maxHealth; return $this->maxHealth;
} }
/** /**
* @param int $amount * @param int $amount
*/ */
public function setMaxHealth($amount){ public function setMaxHealth(int $amount){
$this->maxHealth = (int) $amount; $this->maxHealth = $amount;
} }
public function canCollideWith(Entity $entity) : bool{ public function canCollideWith(Entity $entity) : bool{

View File

@ -58,7 +58,7 @@ class Item extends Entity{
parent::initEntity(); parent::initEntity();
$this->setMaxHealth(5); $this->setMaxHealth(5);
$this->setHealth($this->namedtag["Health"]); $this->setHealth((int) $this->namedtag["Health"]);
if(isset($this->namedtag->Age)){ if(isset($this->namedtag->Age)){
$this->age = $this->namedtag["Age"]; $this->age = $this->namedtag["Age"];
} }
@ -139,7 +139,7 @@ class Item extends Entity{
public function saveNBT(){ public function saveNBT(){
parent::saveNBT(); parent::saveNBT();
$this->namedtag->Item = $this->item->nbtSerialize(-1, "Item"); $this->namedtag->Item = $this->item->nbtSerialize(-1, "Item");
$this->namedtag->Health = new ShortTag("Health", $this->getHealth()); $this->namedtag->Health = new ShortTag("Health", (int) $this->getHealth());
$this->namedtag->Age = new ShortTag("Age", $this->age); $this->namedtag->Age = new ShortTag("Age", $this->age);
$this->namedtag->PickupDelay = new ShortTag("PickupDelay", $this->pickupDelay); $this->namedtag->PickupDelay = new ShortTag("PickupDelay", $this->pickupDelay);
if($this->owner !== null){ if($this->owner !== null){

View File

@ -73,7 +73,7 @@ abstract class Living extends Entity implements Damageable{
$this->namedtag->Health = new FloatTag("Health", (float) $this->getMaxHealth()); $this->namedtag->Health = new FloatTag("Health", (float) $this->getMaxHealth());
} }
$this->setHealth($this->namedtag["Health"]); $this->setHealth((float) $this->namedtag["Health"]);
if(isset($this->namedtag->ActiveEffects)){ if(isset($this->namedtag->ActiveEffects)){
foreach($this->namedtag->ActiveEffects->getValue() as $e){ foreach($this->namedtag->ActiveEffects->getValue() as $e){
@ -100,10 +100,10 @@ abstract class Living extends Entity implements Damageable{
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::ABSORPTION)); $this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::ABSORPTION));
} }
public function setHealth($amount){ public function setHealth(float $amount){
$wasAlive = $this->isAlive(); $wasAlive = $this->isAlive();
parent::setHealth($amount); parent::setHealth($amount);
$this->attributeMap->getAttribute(Attribute::HEALTH)->setValue($this->getHealth(), true); $this->attributeMap->getAttribute(Attribute::HEALTH)->setValue(ceil($this->getHealth()), true);
if($this->isAlive() and !$wasAlive){ if($this->isAlive() and !$wasAlive){
$pk = new EntityEventPacket(); $pk = new EntityEventPacket();
$pk->entityRuntimeId = $this->getId(); $pk->entityRuntimeId = $this->getId();
@ -112,11 +112,11 @@ abstract class Living extends Entity implements Damageable{
} }
} }
public function getMaxHealth(){ public function getMaxHealth() : int{
return $this->attributeMap->getAttribute(Attribute::HEALTH)->getMaxValue(); return (int) $this->attributeMap->getAttribute(Attribute::HEALTH)->getMaxValue();
} }
public function setMaxHealth($amount){ public function setMaxHealth(int $amount){
$this->attributeMap->getAttribute(Attribute::HEALTH)->setMaxValue($amount); $this->attributeMap->getAttribute(Attribute::HEALTH)->setMaxValue($amount);
} }

View File

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

View File

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

View File

@ -33,19 +33,19 @@ use pocketmine\entity\Living;
class EntityDamageByEntityEvent extends EntityDamageEvent{ class EntityDamageByEntityEvent extends EntityDamageEvent{
/** @var int */ /** @var int */
private $damagerEid; private $damagerEntityId;
/** @var float */ /** @var float */
private $knockBack; private $knockBack;
/** /**
* @param Entity $damager * @param Entity $damager
* @param Entity $entity * @param Entity $entity
* @param int $cause * @param int $cause
* @param int|int[] $damage * @param float|float[] $damage
* @param float $knockBack * @param float $knockBack
*/ */
public function __construct(Entity $damager, Entity $entity, $cause, $damage, $knockBack = 0.4){ public function __construct(Entity $damager, Entity $entity, int $cause, $damage, float $knockBack = 0.4){
$this->damagerEid = $damager->getId(); $this->damagerEntityId = $damager->getId();
$this->knockBack = $knockBack; $this->knockBack = $knockBack;
parent::__construct($entity, $cause, $damage); parent::__construct($entity, $cause, $damage);
$this->addAttackerModifiers($damager); $this->addAttackerModifiers($damager);
@ -69,20 +69,20 @@ class EntityDamageByEntityEvent extends EntityDamageEvent{
* @return Entity|null * @return Entity|null
*/ */
public function getDamager(){ public function getDamager(){
return $this->getEntity()->getLevel()->getServer()->findEntity($this->damagerEid, $this->getEntity()->getLevel()); return $this->getEntity()->getLevel()->getServer()->findEntity($this->damagerEntityId, $this->getEntity()->getLevel());
} }
/** /**
* @return float * @return float
*/ */
public function getKnockBack(){ public function getKnockBack() : float{
return $this->knockBack; return $this->knockBack;
} }
/** /**
* @param float $knockBack * @param float $knockBack
*/ */
public function setKnockBack($knockBack){ public function setKnockBack(float $knockBack){
$this->knockBack = $knockBack; $this->knockBack = $knockBack;
} }
} }

View File

@ -55,21 +55,20 @@ class EntityDamageEvent extends EntityEvent implements Cancellable{
const CAUSE_CUSTOM = 14; const CAUSE_CUSTOM = 14;
const CAUSE_STARVATION = 15; const CAUSE_STARVATION = 15;
/** @var int */
private $cause; private $cause;
/** @var array */ /** @var float[] */
private $modifiers; private $modifiers;
/** @var float[] */
private $originals; private $originals;
/** /**
* @param Entity $entity * @param Entity $entity
* @param int $cause * @param int $cause
* @param int|int[] $damage * @param float|float[] $damage
*
* @throws \Exception
*/ */
public function __construct(Entity $entity, $cause, $damage){ public function __construct(Entity $entity, int $cause, $damage){
$this->entity = $entity; $this->entity = $entity;
$this->cause = $cause; $this->cause = $cause;
if(is_array($damage)){ if(is_array($damage)){
@ -90,43 +89,41 @@ class EntityDamageEvent extends EntityEvent implements Cancellable{
/** /**
* @return int * @return int
*/ */
public function getCause(){ public function getCause() : int{
return $this->cause; return $this->cause;
} }
/** /**
* @param int $type * @param int $type
* *
* @return int * @return float
*/ */
public function getOriginalDamage($type = self::MODIFIER_BASE){ public function getOriginalDamage(int $type = self::MODIFIER_BASE) : float{
if(isset($this->originals[$type])){ if(isset($this->originals[$type])){
return $this->originals[$type]; return $this->originals[$type];
} }
return 0; return 0.0;
} }
/** /**
* @param int $type * @param int $type
* *
* @return int * @return float
*/ */
public function getDamage($type = self::MODIFIER_BASE){ public function getDamage(int $type = self::MODIFIER_BASE) : float{
if(isset($this->modifiers[$type])){ if(isset($this->modifiers[$type])){
return $this->modifiers[$type]; return $this->modifiers[$type];
} }
return 0; return 0.0;
} }
/** /**
* @param float $damage * @param float $damage
* @param int $type * @param int $type
*
* @throws \UnexpectedValueException
*/ */
public function setDamage($damage, $type = self::MODIFIER_BASE){ public function setDamage(float $damage, int $type = self::MODIFIER_BASE){
$this->modifiers[$type] = $damage; $this->modifiers[$type] = $damage;
} }
@ -135,20 +132,15 @@ class EntityDamageEvent extends EntityEvent implements Cancellable{
* *
* @return bool * @return bool
*/ */
public function isApplicable($type){ public function isApplicable(int $type){
return isset($this->modifiers[$type]); return isset($this->modifiers[$type]);
} }
/** /**
* @return int * @return float
*/ */
public function getFinalDamage(){ public function getFinalDamage() : float{
$damage = 0; return array_sum($this->modifiers);
foreach($this->modifiers as $type => $d){
$damage += $d;
}
return $damage;
} }
} }