Added static create() functions for many packets

There are a few motivations here:
1) Less boilerplate code (this can be written inline)
2) It's possible to provide multiple constructors for different packet variations to reduce the chance of errors.
3) It makes things catch fire on updates in ways that static analysers can understand.
This commit is contained in:
Dylan K. Taylor
2019-06-05 15:00:08 +01:00
parent 09afb8e772
commit 287c8c2dd4
51 changed files with 481 additions and 247 deletions

View File

@ -874,11 +874,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
}
protected function broadcastMotion() : void{
$pk = new SetEntityMotionPacket();
$pk->entityRuntimeId = $this->id;
$pk->motion = $this->getMotion();
$this->world->broadcastPacketToViewers($this, $pk);
$this->world->broadcastPacketToViewers($this, SetEntityMotionPacket::create($this->id, $this->getMotion()));
}
public function hasGravity() : bool{
@ -1649,9 +1645,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
$id = spl_object_id($player);
if(isset($this->hasSpawned[$id])){
if($send){
$pk = new RemoveEntityPacket();
$pk->entityUniqueId = $this->id;
$player->sendDataPacket($pk);
$player->sendDataPacket(RemoveEntityPacket::create($this->id));
}
unset($this->hasSpawned[$id]);
}
@ -1778,9 +1772,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
$player = [$player];
}
$pk = new SetEntityDataPacket();
$pk->entityRuntimeId = $this->getId();
$pk->metadata = $data ?? $this->propertyManager->getAll();
$pk = SetEntityDataPacket::create($this->getId(), $data ?? $this->propertyManager->getAll());
foreach($player as $p){
if($p === $this){
@ -1795,19 +1787,11 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
}
public function broadcastEntityEvent(int $eventId, ?int $eventData = null, ?array $players = null) : void{
$pk = new EntityEventPacket();
$pk->entityRuntimeId = $this->id;
$pk->event = $eventId;
$pk->data = $eventData ?? 0;
$this->server->broadcastPacket($players ?? $this->getViewers(), $pk);
$this->server->broadcastPacket($players ?? $this->getViewers(), EntityEventPacket::create($this->id, $eventId, $eventData ?? 0));
}
public function broadcastAnimation(?array $players, int $animationId) : void{
$pk = new AnimatePacket();
$pk->entityRuntimeId = $this->id;
$pk->action = $animationId;
$this->server->broadcastPacket($players ?? $this->getViewers(), $pk);
$this->server->broadcastPacket($players ?? $this->getViewers(), AnimatePacket::create($this->id, $animationId));
}
public function __destruct(){