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

@ -300,8 +300,8 @@ abstract class Entity extends Location implements Metadatable{
/** @var float */
protected $baseOffset = 0.0;
/** @var int */
private $health = 20;
/** @var float */
private $health = 20.0;
private $maxHealth = 20;
/** @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;
}
@ -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
*
* @param int $amount
* @param float $amount
*/
public function setHealth($amount){
$amount = (int) $amount;
if($amount === $this->health){
public function setHealth(float $amount){
if($amount == $this->health){
return;
}
@ -947,7 +946,7 @@ abstract class Entity extends Location implements Metadatable{
$this->kill();
}
}elseif($amount <= $this->getMaxHealth() or $amount < $this->health){
$this->health = (int) $amount;
$this->health = $amount;
}else{
$this->health = $this->getMaxHealth();
}
@ -982,15 +981,15 @@ abstract class Entity extends Location implements Metadatable{
/**
* @return int
*/
public function getMaxHealth(){
public function getMaxHealth() : int{
return $this->maxHealth;
}
/**
* @param int $amount
*/
public function setMaxHealth($amount){
$this->maxHealth = (int) $amount;
public function setMaxHealth(int $amount){
$this->maxHealth = $amount;
}
public function canCollideWith(Entity $entity) : bool{

View File

@ -58,7 +58,7 @@ class Item extends Entity{
parent::initEntity();
$this->setMaxHealth(5);
$this->setHealth($this->namedtag["Health"]);
$this->setHealth((int) $this->namedtag["Health"]);
if(isset($this->namedtag->Age)){
$this->age = $this->namedtag["Age"];
}
@ -139,7 +139,7 @@ class Item extends Entity{
public function saveNBT(){
parent::saveNBT();
$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->PickupDelay = new ShortTag("PickupDelay", $this->pickupDelay);
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->setHealth($this->namedtag["Health"]);
$this->setHealth((float) $this->namedtag["Health"]);
if(isset($this->namedtag->ActiveEffects)){
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));
}
public function setHealth($amount){
public function setHealth(float $amount){
$wasAlive = $this->isAlive();
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){
$pk = new EntityEventPacket();
$pk->entityRuntimeId = $this->getId();
@ -112,11 +112,11 @@ abstract class Living extends Entity implements Damageable{
}
}
public function getMaxHealth(){
return $this->attributeMap->getAttribute(Attribute::HEALTH)->getMaxValue();
public function getMaxHealth() : int{
return (int) $this->attributeMap->getAttribute(Attribute::HEALTH)->getMaxValue();
}
public function setMaxHealth($amount){
public function setMaxHealth(int $amount){
$this->attributeMap->getAttribute(Attribute::HEALTH)->setMaxValue($amount);
}