LevelEventPacket: added create() to reduce boilerplate code

This commit is contained in:
Dylan K. Taylor 2019-03-26 16:38:35 +00:00
parent 7333e7118e
commit 1bf0802275
7 changed files with 16 additions and 35 deletions

View File

@ -507,14 +507,10 @@ class Level implements ChunkManager, Metadatable{
* @param int $data
*/
public function broadcastLevelEvent(?Vector3 $pos, int $evid, int $data = 0){
$pk = new LevelEventPacket();
$pk->evid = $evid;
$pk->data = $data;
$pk = LevelEventPacket::create($evid, $data, $pos);
if($pos !== null){
$pk->position = $pos->asVector3();
$this->broadcastPacketToViewers($pos, $pk);
}else{
$pk->position = null;
$this->broadcastGlobalPacket($pk);
}
}

View File

@ -37,11 +37,6 @@ class DestroyBlockParticle implements Particle{
}
public function encode(Vector3 $pos){
$pk = new LevelEventPacket;
$pk->evid = LevelEventPacket::EVENT_PARTICLE_DESTROY;
$pk->position = $pos;
$pk->data = $this->data;
return $pk;
return LevelEventPacket::create(LevelEventPacket::EVENT_PARTICLE_DESTROY, $this->data, $pos);
}
}

View File

@ -50,17 +50,13 @@ class DragonEggTeleportParticle implements Particle{
}
public function encode(Vector3 $pos){
$pk = new LevelEventPacket();
$pk->evid = LevelEventPacket::EVENT_PARTICLE_DRAGON_EGG_TELEPORT;
$pk->position = $pos;
$pk->data =
($this->zDiff < 0 ? 1 << 26 : 0) |
$data = ($this->zDiff < 0 ? 1 << 26 : 0) |
($this->yDiff < 0 ? 1 << 25 : 0) |
($this->xDiff < 0 ? 1 << 24 : 0) |
(abs($this->xDiff) << 16) |
(abs($this->yDiff) << 8) |
abs($this->zDiff);
return $pk;
return LevelEventPacket::create(LevelEventPacket::EVENT_PARTICLE_DRAGON_EGG_TELEPORT, $data, $pos);
}
}

View File

@ -38,11 +38,6 @@ class GenericParticle implements Particle{
}
public function encode(Vector3 $pos){
$pk = new LevelEventPacket;
$pk->evid = LevelEventPacket::EVENT_ADD_PARTICLE_MASK | $this->id;
$pk->position = $pos;
$pk->data = $this->data;
return $pk;
return LevelEventPacket::create(LevelEventPacket::EVENT_ADD_PARTICLE_MASK | $this->id, $this->data, $pos);
}
}

View File

@ -33,16 +33,12 @@ class MobSpawnParticle implements Particle{
protected $height;
public function __construct(int $width = 0, int $height = 0){
//TODO: bounds checks
$this->width = $width;
$this->height = $height;
}
public function encode(Vector3 $pos){
$pk = new LevelEventPacket;
$pk->evid = LevelEventPacket::EVENT_PARTICLE_SPAWN;
$pk->position = $pos;
$pk->data = ($this->width & 0xff) + (($this->height & 0xff) << 8);
return $pk;
return LevelEventPacket::create(LevelEventPacket::EVENT_PARTICLE_SPAWN, ($this->width & 0xff) | (($this->height & 0xff) << 8), $pos);
}
}

View File

@ -49,11 +49,6 @@ abstract class LevelEventSound implements Sound{
}
public function encode(Vector3 $pos){
$pk = new LevelEventPacket;
$pk->evid = $this->getLevelEventId();
$pk->position = $pos;
$pk->data = (int) $this->pitch;
return $pk;
return LevelEventPacket::create($this->getLevelEventId(), (int) $this->pitch, $pos);
}
}

View File

@ -119,6 +119,14 @@ class LevelEventPacket extends DataPacket implements ClientboundPacket{
/** @var int */
public $data;
public static function create(int $evid, int $data, ?Vector3 $pos) : self{
$pk = new self;
$pk->evid = $evid;
$pk->data = $data;
$pk->position = $pos !== null ? $pos->asVector3() : null;
return $pk;
}
protected function decodePayload() : void{
$this->evid = $this->getVarInt();
$this->position = $this->getVector3();