mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-23 00:55:57 +00:00
Refactor of health int -> float and fixed armor being useless when
computed damage reduction is less than 1
This commit is contained in:
parent
ea5bd0348a
commit
fd52022065
@ -2276,7 +2276,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
|
||||
$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);
|
||||
|
@ -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{
|
||||
|
@ -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){
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -36,12 +36,12 @@ class EntityDamageByBlockEvent extends EntityDamageEvent{
|
||||
|
||||
|
||||
/**
|
||||
* @param Block $damager
|
||||
* @param Entity $entity
|
||||
* @param int $cause
|
||||
* @param int|int[] $damage
|
||||
* @param Block $damager
|
||||
* @param Entity $entity
|
||||
* @param int $cause
|
||||
* @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;
|
||||
parent::__construct($entity, $cause, $damage);
|
||||
}
|
||||
|
@ -33,15 +33,14 @@ class EntityDamageByChildEntityEvent extends EntityDamageByEntityEvent{
|
||||
/** @var int */
|
||||
private $childEntityEid;
|
||||
|
||||
|
||||
/**
|
||||
* @param Entity $damager
|
||||
* @param Entity $childEntity
|
||||
* @param Entity $entity
|
||||
* @param int $cause
|
||||
* @param int|int[] $damage
|
||||
* @param Entity $damager
|
||||
* @param Entity $childEntity
|
||||
* @param Entity $entity
|
||||
* @param int $cause
|
||||
* @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();
|
||||
parent::__construct($damager, $entity, $cause, $damage);
|
||||
}
|
||||
|
@ -33,19 +33,19 @@ use pocketmine\entity\Living;
|
||||
class EntityDamageByEntityEvent extends EntityDamageEvent{
|
||||
|
||||
/** @var int */
|
||||
private $damagerEid;
|
||||
private $damagerEntityId;
|
||||
/** @var float */
|
||||
private $knockBack;
|
||||
|
||||
/**
|
||||
* @param Entity $damager
|
||||
* @param Entity $entity
|
||||
* @param int $cause
|
||||
* @param int|int[] $damage
|
||||
* @param float $knockBack
|
||||
* @param Entity $damager
|
||||
* @param Entity $entity
|
||||
* @param int $cause
|
||||
* @param float|float[] $damage
|
||||
* @param float $knockBack
|
||||
*/
|
||||
public function __construct(Entity $damager, Entity $entity, $cause, $damage, $knockBack = 0.4){
|
||||
$this->damagerEid = $damager->getId();
|
||||
public function __construct(Entity $damager, Entity $entity, int $cause, $damage, float $knockBack = 0.4){
|
||||
$this->damagerEntityId = $damager->getId();
|
||||
$this->knockBack = $knockBack;
|
||||
parent::__construct($entity, $cause, $damage);
|
||||
$this->addAttackerModifiers($damager);
|
||||
@ -69,20 +69,20 @@ class EntityDamageByEntityEvent extends EntityDamageEvent{
|
||||
* @return Entity|null
|
||||
*/
|
||||
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
|
||||
*/
|
||||
public function getKnockBack(){
|
||||
public function getKnockBack() : float{
|
||||
return $this->knockBack;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $knockBack
|
||||
*/
|
||||
public function setKnockBack($knockBack){
|
||||
public function setKnockBack(float $knockBack){
|
||||
$this->knockBack = $knockBack;
|
||||
}
|
||||
}
|
||||
|
@ -55,21 +55,20 @@ class EntityDamageEvent extends EntityEvent implements Cancellable{
|
||||
const CAUSE_CUSTOM = 14;
|
||||
const CAUSE_STARVATION = 15;
|
||||
|
||||
|
||||
/** @var int */
|
||||
private $cause;
|
||||
/** @var array */
|
||||
/** @var float[] */
|
||||
private $modifiers;
|
||||
/** @var float[] */
|
||||
private $originals;
|
||||
|
||||
|
||||
/**
|
||||
* @param Entity $entity
|
||||
* @param int $cause
|
||||
* @param int|int[] $damage
|
||||
*
|
||||
* @throws \Exception
|
||||
* @param Entity $entity
|
||||
* @param int $cause
|
||||
* @param float|float[] $damage
|
||||
*/
|
||||
public function __construct(Entity $entity, $cause, $damage){
|
||||
public function __construct(Entity $entity, int $cause, $damage){
|
||||
$this->entity = $entity;
|
||||
$this->cause = $cause;
|
||||
if(is_array($damage)){
|
||||
@ -90,43 +89,41 @@ class EntityDamageEvent extends EntityEvent implements Cancellable{
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getCause(){
|
||||
public function getCause() : int{
|
||||
return $this->cause;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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])){
|
||||
return $this->originals[$type];
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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])){
|
||||
return $this->modifiers[$type];
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $damage
|
||||
* @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;
|
||||
}
|
||||
|
||||
@ -135,20 +132,15 @@ class EntityDamageEvent extends EntityEvent implements Cancellable{
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isApplicable($type){
|
||||
public function isApplicable(int $type){
|
||||
return isset($this->modifiers[$type]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
* @return float
|
||||
*/
|
||||
public function getFinalDamage(){
|
||||
$damage = 0;
|
||||
foreach($this->modifiers as $type => $d){
|
||||
$damage += $d;
|
||||
}
|
||||
|
||||
return $damage;
|
||||
public function getFinalDamage() : float{
|
||||
return array_sum($this->modifiers);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user