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\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);
}
}

View File

@ -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;

View File

@ -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();

View File

@ -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));

View File

@ -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));

View File

@ -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)];
}
}