Entity: make networkProperties private

this reduces the temptation to use it in high-level code, as well as making syncNetworkData() more useful (now it can export to many data collections, which means we can start to think about having a property cache per network session, which is more flexible)
This commit is contained in:
Dylan K. Taylor 2020-05-21 20:29:06 +01:00
parent 1aa92bd6a8
commit 6257f717b1
11 changed files with 71 additions and 61 deletions

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\entity; namespace pocketmine\entity;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags;
abstract class Animal extends Living implements Ageable{ abstract class Animal extends Living implements Ageable{
@ -33,8 +34,8 @@ abstract class Animal extends Living implements Ageable{
return $this->baby; return $this->baby;
} }
protected function syncNetworkData() : void{ protected function syncNetworkData(EntityMetadataCollection $properties) : void{
parent::syncNetworkData(); parent::syncNetworkData($properties);
$this->networkProperties->setGenericFlag(EntityMetadataFlags::BABY, $this->baby); $properties->setGenericFlag(EntityMetadataFlags::BABY, $this->baby);
} }
} }

View File

@ -87,7 +87,7 @@ abstract class Entity{
protected $id; protected $id;
/** @var EntityMetadataCollection */ /** @var EntityMetadataCollection */
protected $networkProperties; private $networkProperties;
/** @var Chunk|null */ /** @var Chunk|null */
public $chunk; public $chunk;
@ -1650,30 +1650,30 @@ abstract class Entity{
* @return MetadataProperty[] * @return MetadataProperty[]
*/ */
final protected function getSyncedNetworkData(bool $dirtyOnly) : array{ final protected function getSyncedNetworkData(bool $dirtyOnly) : array{
$this->syncNetworkData(); $this->syncNetworkData($this->networkProperties);
return $dirtyOnly ? $this->networkProperties->getDirty() : $this->networkProperties->getAll(); return $dirtyOnly ? $this->networkProperties->getDirty() : $this->networkProperties->getAll();
} }
protected function syncNetworkData() : void{ protected function syncNetworkData(EntityMetadataCollection $properties) : void{
$this->networkProperties->setByte(EntityMetadataProperties::ALWAYS_SHOW_NAMETAG, $this->alwaysShowNameTag ? 1 : 0); $properties->setByte(EntityMetadataProperties::ALWAYS_SHOW_NAMETAG, $this->alwaysShowNameTag ? 1 : 0);
$this->networkProperties->setFloat(EntityMetadataProperties::BOUNDING_BOX_HEIGHT, $this->height); $properties->setFloat(EntityMetadataProperties::BOUNDING_BOX_HEIGHT, $this->height);
$this->networkProperties->setFloat(EntityMetadataProperties::BOUNDING_BOX_WIDTH, $this->width); $properties->setFloat(EntityMetadataProperties::BOUNDING_BOX_WIDTH, $this->width);
$this->networkProperties->setFloat(EntityMetadataProperties::SCALE, $this->scale); $properties->setFloat(EntityMetadataProperties::SCALE, $this->scale);
$this->networkProperties->setLong(EntityMetadataProperties::LEAD_HOLDER_EID, -1); $properties->setLong(EntityMetadataProperties::LEAD_HOLDER_EID, -1);
$this->networkProperties->setLong(EntityMetadataProperties::OWNER_EID, $this->ownerId ?? -1); $properties->setLong(EntityMetadataProperties::OWNER_EID, $this->ownerId ?? -1);
$this->networkProperties->setLong(EntityMetadataProperties::TARGET_EID, $this->targetId ?? 0); $properties->setLong(EntityMetadataProperties::TARGET_EID, $this->targetId ?? 0);
$this->networkProperties->setString(EntityMetadataProperties::NAMETAG, $this->nameTag); $properties->setString(EntityMetadataProperties::NAMETAG, $this->nameTag);
$this->networkProperties->setString(EntityMetadataProperties::SCORE_TAG, $this->scoreTag); $properties->setString(EntityMetadataProperties::SCORE_TAG, $this->scoreTag);
$this->networkProperties->setGenericFlag(EntityMetadataFlags::AFFECTED_BY_GRAVITY, true); $properties->setGenericFlag(EntityMetadataFlags::AFFECTED_BY_GRAVITY, true);
$this->networkProperties->setGenericFlag(EntityMetadataFlags::CAN_CLIMB, $this->canClimb); $properties->setGenericFlag(EntityMetadataFlags::CAN_CLIMB, $this->canClimb);
$this->networkProperties->setGenericFlag(EntityMetadataFlags::CAN_SHOW_NAMETAG, $this->nameTagVisible); $properties->setGenericFlag(EntityMetadataFlags::CAN_SHOW_NAMETAG, $this->nameTagVisible);
$this->networkProperties->setGenericFlag(EntityMetadataFlags::HAS_COLLISION, true); $properties->setGenericFlag(EntityMetadataFlags::HAS_COLLISION, true);
$this->networkProperties->setGenericFlag(EntityMetadataFlags::IMMOBILE, $this->immobile); $properties->setGenericFlag(EntityMetadataFlags::IMMOBILE, $this->immobile);
$this->networkProperties->setGenericFlag(EntityMetadataFlags::INVISIBLE, $this->invisible); $properties->setGenericFlag(EntityMetadataFlags::INVISIBLE, $this->invisible);
$this->networkProperties->setGenericFlag(EntityMetadataFlags::ONFIRE, $this->isOnFire()); $properties->setGenericFlag(EntityMetadataFlags::ONFIRE, $this->isOnFire());
$this->networkProperties->setGenericFlag(EntityMetadataFlags::WALLCLIMBING, $this->canClimbWalls); $properties->setGenericFlag(EntityMetadataFlags::WALLCLIMBING, $this->canClimbWalls);
} }
/** /**

View File

@ -49,6 +49,7 @@ use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\FloatTag; use pocketmine\nbt\tag\FloatTag;
use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\ShortTag; use pocketmine\nbt\tag\ShortTag;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties;
use pocketmine\player\Player; use pocketmine\player\Player;
@ -766,17 +767,17 @@ abstract class Living extends Entity{
$player->getNetworkSession()->onMobArmorChange($this); $player->getNetworkSession()->onMobArmorChange($this);
} }
protected function syncNetworkData() : void{ protected function syncNetworkData(EntityMetadataCollection $properties) : void{
parent::syncNetworkData(); parent::syncNetworkData($properties);
$this->networkProperties->setByte(EntityMetadataProperties::POTION_AMBIENT, $this->effectManager->hasOnlyAmbientEffects() ? 1 : 0); $properties->setByte(EntityMetadataProperties::POTION_AMBIENT, $this->effectManager->hasOnlyAmbientEffects() ? 1 : 0);
$this->networkProperties->setInt(EntityMetadataProperties::POTION_COLOR, Binary::signInt($this->effectManager->getBubbleColor()->toARGB())); $properties->setInt(EntityMetadataProperties::POTION_COLOR, Binary::signInt($this->effectManager->getBubbleColor()->toARGB()));
$this->networkProperties->setShort(EntityMetadataProperties::AIR, $this->breathTicks); $properties->setShort(EntityMetadataProperties::AIR, $this->breathTicks);
$this->networkProperties->setShort(EntityMetadataProperties::MAX_AIR, $this->maxBreathTicks); $properties->setShort(EntityMetadataProperties::MAX_AIR, $this->maxBreathTicks);
$this->networkProperties->setGenericFlag(EntityMetadataFlags::BREATHING, $this->breathing); $properties->setGenericFlag(EntityMetadataFlags::BREATHING, $this->breathing);
$this->networkProperties->setGenericFlag(EntityMetadataFlags::SNEAKING, $this->sneaking); $properties->setGenericFlag(EntityMetadataFlags::SNEAKING, $this->sneaking);
$this->networkProperties->setGenericFlag(EntityMetadataFlags::SPRINTING, $this->sprinting); $properties->setGenericFlag(EntityMetadataFlags::SPRINTING, $this->sprinting);
} }
protected function onDispose() : void{ protected function onDispose() : void{

View File

@ -25,6 +25,7 @@ namespace pocketmine\entity;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds; use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties;
@ -84,10 +85,10 @@ class Villager extends Living implements Ageable{
return $this->baby; return $this->baby;
} }
protected function syncNetworkData() : void{ protected function syncNetworkData(EntityMetadataCollection $properties) : void{
parent::syncNetworkData(); parent::syncNetworkData($properties);
$this->networkProperties->setGenericFlag(EntityMetadataFlags::BABY, $this->baby); $properties->setGenericFlag(EntityMetadataFlags::BABY, $this->baby);
$this->networkProperties->setInt(EntityMetadataProperties::VARIANT, $this->profession); $properties->setInt(EntityMetadataProperties::VARIANT, $this->profession);
} }
} }

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\entity; namespace pocketmine\entity;
use pocketmine\event\entity\EntityDamageEvent; use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags;
abstract class WaterAnimal extends Living implements Ageable{ abstract class WaterAnimal extends Living implements Ageable{
@ -43,8 +44,8 @@ abstract class WaterAnimal extends Living implements Ageable{
$this->attack($ev); $this->attack($ev);
} }
protected function syncNetworkData() : void{ protected function syncNetworkData(EntityMetadataCollection $properties) : void{
parent::syncNetworkData(); parent::syncNetworkData($properties);
$this->networkProperties->setGenericFlag(EntityMetadataFlags::BABY, $this->baby); $properties->setGenericFlag(EntityMetadataFlags::BABY, $this->baby);
} }
} }

View File

@ -29,6 +29,7 @@ use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ShortTag; use pocketmine\nbt\tag\ShortTag;
use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds; use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties;
use pocketmine\player\Player; use pocketmine\player\Player;
use function sqrt; use function sqrt;
@ -220,9 +221,9 @@ class ExperienceOrb extends Entity{
return false; return false;
} }
protected function syncNetworkData() : void{ protected function syncNetworkData(EntityMetadataCollection $properties) : void{
parent::syncNetworkData(); parent::syncNetworkData($properties);
$this->networkProperties->setInt(EntityMetadataProperties::EXPERIENCE_VALUE, $this->xpValue); $properties->setInt(EntityMetadataProperties::EXPERIENCE_VALUE, $this->xpValue);
} }
} }

View File

@ -33,6 +33,7 @@ use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\IntTag;
use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds; use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties;
use function abs; use function abs;
use function get_class; use function get_class;
@ -140,9 +141,9 @@ class FallingBlock extends Entity{
return $nbt; return $nbt;
} }
protected function syncNetworkData() : void{ protected function syncNetworkData(EntityMetadataCollection $properties) : void{
parent::syncNetworkData(); parent::syncNetworkData($properties);
$this->networkProperties->setInt(EntityMetadataProperties::VARIANT, $this->block->getRuntimeId()); $properties->setInt(EntityMetadataProperties::VARIANT, $this->block->getRuntimeId());
} }
} }

View File

@ -29,6 +29,7 @@ use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\entity\ExplosionPrimeEvent; use pocketmine\event\entity\ExplosionPrimeEvent;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds; use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties;
use pocketmine\world\Explosion; use pocketmine\world\Explosion;
@ -108,10 +109,10 @@ class PrimedTNT extends Entity implements Explosive{
} }
} }
protected function syncNetworkData() : void{ protected function syncNetworkData(EntityMetadataCollection $properties) : void{
parent::syncNetworkData(); parent::syncNetworkData($properties);
$this->networkProperties->setGenericFlag(EntityMetadataFlags::IGNITED, true); $properties->setGenericFlag(EntityMetadataFlags::IGNITED, true);
$this->networkProperties->setInt(EntityMetadataProperties::FUSE_LENGTH, $this->fuse); $properties->setInt(EntityMetadataProperties::FUSE_LENGTH, $this->fuse);
} }
} }

View File

@ -32,6 +32,7 @@ use pocketmine\item\VanillaItems;
use pocketmine\math\RayTraceResult; use pocketmine\math\RayTraceResult;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds; use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags;
use pocketmine\player\Player; use pocketmine\player\Player;
use pocketmine\world\sound\ArrowHitSound; use pocketmine\world\sound\ArrowHitSound;
@ -193,9 +194,9 @@ class Arrow extends Projectile{
$this->flagForDespawn(); $this->flagForDespawn();
} }
protected function syncNetworkData() : void{ protected function syncNetworkData(EntityMetadataCollection $properties) : void{
parent::syncNetworkData(); parent::syncNetworkData($properties);
$this->networkProperties->setGenericFlag(EntityMetadataFlags::CRITICAL, $this->critical); $properties->setGenericFlag(EntityMetadataFlags::CRITICAL, $this->critical);
} }
} }

View File

@ -35,6 +35,7 @@ use pocketmine\event\entity\ProjectileHitEvent;
use pocketmine\item\Potion; use pocketmine\item\Potion;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds; use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties;
use pocketmine\world\particle\PotionSplashParticle; use pocketmine\world\particle\PotionSplashParticle;
@ -172,10 +173,10 @@ class SplashPotion extends Throwable{
return Potion::getPotionEffectsById($this->getPotionId()); return Potion::getPotionEffectsById($this->getPotionId());
} }
protected function syncNetworkData() : void{ protected function syncNetworkData(EntityMetadataCollection $properties) : void{
parent::syncNetworkData(); parent::syncNetworkData($properties);
$this->networkProperties->setShort(EntityMetadataProperties::POTION_AUX_VALUE, $this->potionId); $properties->setShort(EntityMetadataProperties::POTION_AUX_VALUE, $this->potionId);
$this->networkProperties->setGenericFlag(EntityMetadataFlags::LINGER, $this->linger); $properties->setGenericFlag(EntityMetadataFlags::LINGER, $this->linger);
} }
} }

View File

@ -86,6 +86,7 @@ use pocketmine\nbt\tag\ListTag;
use pocketmine\network\mcpe\NetworkSession; use pocketmine\network\mcpe\NetworkSession;
use pocketmine\network\mcpe\protocol\AnimatePacket; use pocketmine\network\mcpe\protocol\AnimatePacket;
use pocketmine\network\mcpe\protocol\MovePlayerPacket; use pocketmine\network\mcpe\protocol\MovePlayerPacket;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties;
use pocketmine\network\mcpe\protocol\types\entity\PlayerMetadataFlags; use pocketmine\network\mcpe\protocol\types\entity\PlayerMetadataFlags;
@ -2181,13 +2182,13 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
parent::attack($source); parent::attack($source);
} }
protected function syncNetworkData() : void{ protected function syncNetworkData(EntityMetadataCollection $properties) : void{
parent::syncNetworkData(); parent::syncNetworkData($properties);
$this->networkProperties->setGenericFlag(EntityMetadataFlags::ACTION, $this->startAction > -1); $properties->setGenericFlag(EntityMetadataFlags::ACTION, $this->startAction > -1);
$this->networkProperties->setPlayerFlag(PlayerMetadataFlags::SLEEP, $this->sleeping !== null); $properties->setPlayerFlag(PlayerMetadataFlags::SLEEP, $this->sleeping !== null);
$this->networkProperties->setBlockPos(EntityMetadataProperties::PLAYER_BED_POSITION, $this->sleeping ?? new Vector3(0, 0, 0)); $properties->setBlockPos(EntityMetadataProperties::PLAYER_BED_POSITION, $this->sleeping ?? new Vector3(0, 0, 0));
} }
public function broadcastAnimation(Animation $animation, ?array $targets = null) : void{ public function broadcastAnimation(Animation $animation, ?array $targets = null) : void{