From 8807617480702b2d27d123d9e430b5d047c9acc6 Mon Sep 17 00:00:00 2001 From: PEMapModder Date: Thu, 11 Feb 2016 18:45:58 +0800 Subject: [PATCH] Fixed attriutes not sending --- src/pocketmine/Player.php | 15 ++++++++++----- src/pocketmine/entity/Attribute.php | 4 ++-- src/pocketmine/entity/AttributeMap.php | 3 +++ src/pocketmine/entity/Human.php | 10 ++++++++-- src/pocketmine/entity/Living.php | 5 +++++ src/pocketmine/item/SpiderEye.php | 2 +- 6 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index c657cfd0e..a1c2f810f 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -34,7 +34,6 @@ use pocketmine\event\block\SignChangeEvent; use pocketmine\event\entity\EntityDamageByBlockEvent; use pocketmine\event\entity\EntityDamageByEntityEvent; use pocketmine\event\entity\EntityDamageEvent; -use pocketmine\event\entity\EntityRegainHealthEvent; use pocketmine\event\entity\EntityShootBowEvent; use pocketmine\event\entity\ProjectileLaunchEvent; use pocketmine\event\inventory\CraftItemEvent; @@ -1205,14 +1204,20 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade } public function entityBaseTick($tickDiff = 1){ - parent::entityBaseTick($tickDiff); + $hasUpdate = parent::entityBaseTick($tickDiff); + $entries = $this->attributeMap->needSend(); if(count($entries) > 0){ $pk = new UpdateAttributesPacket(); $pk->entityId = 0; $pk->entries = $entries; $this->dataPacket($pk); + foreach($entries as $entry){ + $entry->markSynchronized(); + } } + + return $hasUpdate; } protected function checkGroundState($movX, $movY, $movZ, $dx, $dy, $dz){ @@ -3189,9 +3194,9 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade public function setHealth($amount){ parent::setHealth($amount); if($this->spawned === true){ - $pk = new SetHealthPacket(); - $pk->health = $this->getHealth(); - $this->dataPacket($pk); +// $pk = new SetHealthPacket(); +// $pk->health = $this->getHealth(); +// $this->dataPacket($pk); } } diff --git a/src/pocketmine/entity/Attribute.php b/src/pocketmine/entity/Attribute.php index 4e48411bc..62b9b6916 100644 --- a/src/pocketmine/entity/Attribute.php +++ b/src/pocketmine/entity/Attribute.php @@ -73,7 +73,7 @@ class Attribute{ * * @return Attribute */ - public static function addAttribute($id, $name, $minValue, $maxValue, $defaultValue, $shouldSend = false){ + public static function addAttribute($id, $name, $minValue, $maxValue, $defaultValue, $shouldSend = true){ if($minValue > $maxValue or $defaultValue > $maxValue or $defaultValue < $minValue){ throw new \InvalidArgumentException("Invalid ranges: min value: $minValue, max value: $maxValue, $defaultValue: $defaultValue"); } @@ -105,7 +105,7 @@ class Attribute{ return null; } - private function __construct($id, $name, $minValue, $maxValue, $defaultValue, $shouldSend = false){ + private function __construct($id, $name, $minValue, $maxValue, $defaultValue, $shouldSend = true){ $this->id = (int) $id; $this->name = (string) $name; $this->minValue = (float) $minValue; diff --git a/src/pocketmine/entity/AttributeMap.php b/src/pocketmine/entity/AttributeMap.php index fe44474a5..3e060cc1d 100644 --- a/src/pocketmine/entity/AttributeMap.php +++ b/src/pocketmine/entity/AttributeMap.php @@ -38,6 +38,9 @@ class AttributeMap{ return $this->attributes[$id] ?? null; } + /** + * @return Attribute[] + */ public function needSend() : array{ return array_filter($this->attributes, function (Attribute $attribute){ return $attribute->isSyncable() and $attribute->isDesynchronized(); diff --git a/src/pocketmine/entity/Human.php b/src/pocketmine/entity/Human.php index 312bf2968..4f0471570 100644 --- a/src/pocketmine/entity/Human.php +++ b/src/pocketmine/entity/Human.php @@ -126,6 +126,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ public function addFood(float $amount){ $attr = $this->attributeMap->getAttribute(Attribute::HUNGER); + $amount += $attr->getValue(); $amount = max(min($amount, $attr->getMaxValue()), $attr->getMinValue()); $this->setFood($amount); } @@ -147,7 +148,8 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ } public function addSaturation(float $amount){ - $this->attributeMap->getAttribute(Attribute::SATURATION)->setValue($amount, true); + $attr = $this->attributeMap->getAttribute(Attribute::SATURATION); + $attr->setValue($attr->getValue() + $amount, true); } public function getExhaustion() : float{ @@ -175,7 +177,6 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ while($exhaustion >= 4.0){ $exhaustion -= 4.0; - $this->setExhaustion($exhaustion); $saturation = $this->getSaturation(); if($saturation > 0){ @@ -189,6 +190,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ } } } + $this->setExhaustion($exhaustion); } public function getInventory(){ @@ -230,6 +232,10 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ } parent::initEntity(); + } + + protected function addAttributes(){ + parent::addAttributes(); $this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::SATURATION)); $this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::EXHAUSTION)); diff --git a/src/pocketmine/entity/Living.php b/src/pocketmine/entity/Living.php index ded684ab8..27658e80d 100644 --- a/src/pocketmine/entity/Living.php +++ b/src/pocketmine/entity/Living.php @@ -32,6 +32,7 @@ use pocketmine\item\Item as ItemItem; use pocketmine\math\Vector3; use pocketmine\nbt\tag\ShortTag; use pocketmine\network\protocol\EntityEventPacket; +use pocketmine\Player; use pocketmine\Server; use pocketmine\utils\BlockIterator; @@ -47,6 +48,8 @@ abstract class Living extends Entity implements Damageable{ protected function initEntity(){ parent::initEntity(); + $this->addAttributes(); + if(isset($this->namedtag->HealF)){ $this->namedtag->Health = new ShortTag("Health", (int) $this->namedtag["HealF"]); unset($this->namedtag->HealF); @@ -57,7 +60,9 @@ abstract class Living extends Entity implements Damageable{ } $this->setHealth($this->namedtag["Health"]); + } + protected function addAttributes(){ $this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::ABSORPTION)); $this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::HEALTH)); $this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::MOVEMENT_SPEED)); diff --git a/src/pocketmine/item/SpiderEye.php b/src/pocketmine/item/SpiderEye.php index 2644e3cd5..3d8d67296 100644 --- a/src/pocketmine/item/SpiderEye.php +++ b/src/pocketmine/item/SpiderEye.php @@ -37,6 +37,6 @@ class SpiderEye extends Food{ } public function getAdditionEffects() : array{ - return Effect::getEffect(Effect::POISON)->setDuration(80); + return [Effect::getEffect(Effect::POISON)->setDuration(80)]; } }