Fixed attriutes not sending

This commit is contained in:
PEMapModder 2016-02-11 18:45:58 +08:00
parent 28967ca495
commit 8807617480
6 changed files with 29 additions and 10 deletions

View File

@ -34,7 +34,6 @@ use pocketmine\event\block\SignChangeEvent;
use pocketmine\event\entity\EntityDamageByBlockEvent; use pocketmine\event\entity\EntityDamageByBlockEvent;
use pocketmine\event\entity\EntityDamageByEntityEvent; use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\event\entity\EntityDamageEvent; use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\entity\EntityRegainHealthEvent;
use pocketmine\event\entity\EntityShootBowEvent; use pocketmine\event\entity\EntityShootBowEvent;
use pocketmine\event\entity\ProjectileLaunchEvent; use pocketmine\event\entity\ProjectileLaunchEvent;
use pocketmine\event\inventory\CraftItemEvent; use pocketmine\event\inventory\CraftItemEvent;
@ -1205,14 +1204,20 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
} }
public function entityBaseTick($tickDiff = 1){ public function entityBaseTick($tickDiff = 1){
parent::entityBaseTick($tickDiff); $hasUpdate = parent::entityBaseTick($tickDiff);
$entries = $this->attributeMap->needSend(); $entries = $this->attributeMap->needSend();
if(count($entries) > 0){ if(count($entries) > 0){
$pk = new UpdateAttributesPacket(); $pk = new UpdateAttributesPacket();
$pk->entityId = 0; $pk->entityId = 0;
$pk->entries = $entries; $pk->entries = $entries;
$this->dataPacket($pk); $this->dataPacket($pk);
foreach($entries as $entry){
$entry->markSynchronized();
}
} }
return $hasUpdate;
} }
protected function checkGroundState($movX, $movY, $movZ, $dx, $dy, $dz){ 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){ public function setHealth($amount){
parent::setHealth($amount); parent::setHealth($amount);
if($this->spawned === true){ if($this->spawned === true){
$pk = new SetHealthPacket(); // $pk = new SetHealthPacket();
$pk->health = $this->getHealth(); // $pk->health = $this->getHealth();
$this->dataPacket($pk); // $this->dataPacket($pk);
} }
} }

View File

@ -73,7 +73,7 @@ class Attribute{
* *
* @return 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){ if($minValue > $maxValue or $defaultValue > $maxValue or $defaultValue < $minValue){
throw new \InvalidArgumentException("Invalid ranges: min value: $minValue, max value: $maxValue, $defaultValue: $defaultValue"); throw new \InvalidArgumentException("Invalid ranges: min value: $minValue, max value: $maxValue, $defaultValue: $defaultValue");
} }
@ -105,7 +105,7 @@ class Attribute{
return null; 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->id = (int) $id;
$this->name = (string) $name; $this->name = (string) $name;
$this->minValue = (float) $minValue; $this->minValue = (float) $minValue;

View File

@ -38,6 +38,9 @@ class AttributeMap{
return $this->attributes[$id] ?? null; return $this->attributes[$id] ?? null;
} }
/**
* @return Attribute[]
*/
public function needSend() : array{ public function needSend() : array{
return array_filter($this->attributes, function (Attribute $attribute){ return array_filter($this->attributes, function (Attribute $attribute){
return $attribute->isSyncable() and $attribute->isDesynchronized(); return $attribute->isSyncable() and $attribute->isDesynchronized();

View File

@ -126,6 +126,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
public function addFood(float $amount){ public function addFood(float $amount){
$attr = $this->attributeMap->getAttribute(Attribute::HUNGER); $attr = $this->attributeMap->getAttribute(Attribute::HUNGER);
$amount += $attr->getValue();
$amount = max(min($amount, $attr->getMaxValue()), $attr->getMinValue()); $amount = max(min($amount, $attr->getMaxValue()), $attr->getMinValue());
$this->setFood($amount); $this->setFood($amount);
} }
@ -147,7 +148,8 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
} }
public function addSaturation(float $amount){ 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{ public function getExhaustion() : float{
@ -175,7 +177,6 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
while($exhaustion >= 4.0){ while($exhaustion >= 4.0){
$exhaustion -= 4.0; $exhaustion -= 4.0;
$this->setExhaustion($exhaustion);
$saturation = $this->getSaturation(); $saturation = $this->getSaturation();
if($saturation > 0){ if($saturation > 0){
@ -189,6 +190,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
} }
} }
} }
$this->setExhaustion($exhaustion);
} }
public function getInventory(){ public function getInventory(){
@ -230,6 +232,10 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
} }
parent::initEntity(); parent::initEntity();
}
protected function addAttributes(){
parent::addAttributes();
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::SATURATION)); $this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::SATURATION));
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::EXHAUSTION)); $this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::EXHAUSTION));

View File

@ -32,6 +32,7 @@ use pocketmine\item\Item as ItemItem;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\nbt\tag\ShortTag; use pocketmine\nbt\tag\ShortTag;
use pocketmine\network\protocol\EntityEventPacket; use pocketmine\network\protocol\EntityEventPacket;
use pocketmine\Player;
use pocketmine\Server; use pocketmine\Server;
use pocketmine\utils\BlockIterator; use pocketmine\utils\BlockIterator;
@ -47,6 +48,8 @@ abstract class Living extends Entity implements Damageable{
protected function initEntity(){ protected function initEntity(){
parent::initEntity(); parent::initEntity();
$this->addAttributes();
if(isset($this->namedtag->HealF)){ if(isset($this->namedtag->HealF)){
$this->namedtag->Health = new ShortTag("Health", (int) $this->namedtag["HealF"]); $this->namedtag->Health = new ShortTag("Health", (int) $this->namedtag["HealF"]);
unset($this->namedtag->HealF); unset($this->namedtag->HealF);
@ -57,7 +60,9 @@ abstract class Living extends Entity implements Damageable{
} }
$this->setHealth($this->namedtag["Health"]); $this->setHealth($this->namedtag["Health"]);
}
protected function addAttributes(){
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::ABSORPTION)); $this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::ABSORPTION));
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::HEALTH)); $this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::HEALTH));
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::MOVEMENT_SPEED)); $this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::MOVEMENT_SPEED));

View File

@ -37,6 +37,6 @@ class SpiderEye extends Food{
} }
public function getAdditionEffects() : array{ public function getAdditionEffects() : array{
return Effect::getEffect(Effect::POISON)->setDuration(80); return [Effect::getEffect(Effect::POISON)->setDuration(80)];
} }
} }