mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-11 08:19:45 +00:00
LevelEventPacket: added create() to reduce boilerplate code
This commit is contained in:
parent
7333e7118e
commit
1bf0802275
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user