Skip to content
This repository was archived by the owner on Feb 9, 2021. It is now read-only.

Commit 38089af

Browse files
committed
Improved global entity motion encoding using per-player queues
1 parent cd135b3 commit 38089af

File tree

10 files changed

+44
-67
lines changed

10 files changed

+44
-67
lines changed

src/pocketmine/Player.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@
9393
use pocketmine\network\protocol\Info as ProtocolInfo;
9494
use pocketmine\network\protocol\LoginStatusPacket;
9595
use pocketmine\network\protocol\MessagePacket;
96+
use pocketmine\network\protocol\MoveEntityPacket;
9697
use pocketmine\network\protocol\MovePlayerPacket;
9798
use pocketmine\network\protocol\SetDifficultyPacket;
99+
use pocketmine\network\protocol\SetEntityMotionPacket;
98100
use pocketmine\network\protocol\SetHealthPacket;
99101
use pocketmine\network\protocol\SetSpawnPositionPacket;
100102
use pocketmine\network\protocol\SetTimePacket;
@@ -142,6 +144,9 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
142144

143145
protected $sendIndex = 0;
144146

147+
protected $moveToSend = [];
148+
protected $motionToSend = [];
149+
145150
public $blocked = false;
146151
public $achievements = [];
147152
public $lastCorrect;
@@ -1053,6 +1058,14 @@ protected function getCreativeBlock(Item $item){
10531058
return -1;
10541059
}
10551060

1061+
public function addEntityMotion($entityId, $x, $y, $z){
1062+
$this->motionToSend[$entityId] = [$entityId, $x, $y, $z];
1063+
}
1064+
1065+
public function addEntityMovement($entityId, $x, $y, $z, $yaw, $pitch){
1066+
$this->moveToSend[$entityId] = [$entityId, $x, $y, $z, $yaw, $pitch];
1067+
}
1068+
10561069
protected function processMovement($currentTick){
10571070
if($this->dead or !$this->spawned or !($this->newPosition instanceof Vector3)){
10581071
return;
@@ -1271,6 +1284,21 @@ public function onUpdate($currentTick){
12711284
$this->sendNextChunk();
12721285
}
12731286

1287+
if(count($this->moveToSend) > 0){
1288+
$pk = new MoveEntityPacket();
1289+
$pk->entities = $this->moveToSend;
1290+
$this->dataPacket($pk);
1291+
$this->moveToSend = [];
1292+
}
1293+
1294+
1295+
if(count($this->motionToSend) > 0){
1296+
$pk = new SetEntityMotionPacket();
1297+
$pk->entities = $this->motionToSend;
1298+
$this->dataPacket($pk);
1299+
$this->motionToSend = [];
1300+
}
1301+
12741302
$this->timings->stopTiming();
12751303

12761304
return true;

src/pocketmine/entity/Arrow.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use pocketmine\level\format\FullChunk;
2525
use pocketmine\nbt\tag\Compound;
2626
use pocketmine\network\protocol\AddEntityPacket;
27-
use pocketmine\network\protocol\SetEntityMotionPacket;
2827
use pocketmine\Player;
2928

3029
class Arrow extends Projectile{
@@ -73,11 +72,7 @@ public function spawnTo(Player $player){
7372
$pk->did = 0; //TODO: send motion here
7473
$player->dataPacket($pk);
7574

76-
$pk = new SetEntityMotionPacket();
77-
$pk->entities = [
78-
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
79-
];
80-
$player->dataPacket($pk);
75+
$player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ);
8176

8277
parent::spawnTo($player);
8378
}

src/pocketmine/entity/Entity.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,9 @@
5151
use pocketmine\nbt\tag\Short;
5252
use pocketmine\nbt\tag\String;
5353
use pocketmine\Network;
54-
use pocketmine\network\protocol\MoveEntityPacket;
5554
use pocketmine\network\protocol\MovePlayerPacket;
5655
use pocketmine\network\protocol\RemoveEntityPacket;
5756
use pocketmine\network\protocol\SetEntityDataPacket;
58-
use pocketmine\network\protocol\SetEntityMotionPacket;
5957
use pocketmine\network\protocol\SetTimePacket;
6058
use pocketmine\Player;
6159
use pocketmine\plugin\Plugin;
@@ -594,28 +592,23 @@ public function updateMovement(){
594592
$pk->yaw = $this->yaw;
595593
$pk->pitch = $this->pitch;
596594
$pk->bodyYaw = $this->yaw;
595+
Server::broadcastPacket($this->hasSpawned, $pk);
597596
}else{
598-
//TODO: add to move list
599-
$pk = new MoveEntityPacket();
600-
$pk->entities = [
601-
[$this->id, $this->x, $this->y + $this->getEyeHeight(), $this->z, $this->yaw, $this->pitch]
602-
];
597+
foreach($this->hasSpawned as $player){
598+
$player->addEntityMovement($this->id, $this->x, $this->y + $this->getEyeHeight(), $this->z, $this->yaw, $this->pitch);
599+
}
603600
}
604601

605-
Server::broadcastPacket($this->hasSpawned, $pk);
606602
}
607603

608604
if(($this->lastMotionX != $this->motionX or $this->lastMotionY != $this->motionY or $this->lastMotionZ != $this->motionZ)){
609605
$this->lastMotionX = $this->motionX;
610606
$this->lastMotionY = $this->motionY;
611607
$this->lastMotionZ = $this->motionZ;
612608

613-
$pk = new SetEntityMotionPacket();
614-
$pk->entities = [
615-
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
616-
];
617-
618-
Server::broadcastPacket($this->hasSpawned, $pk);
609+
foreach($this->hasSpawned as $player){
610+
$player->addEntityMotion($this->id, $this->motionX, $this->motionY, $this->motionZ);
611+
}
619612

620613
if($this instanceof Player){
621614
$this->motionX = 0;
@@ -1152,11 +1145,7 @@ public function setMotion(Vector3 $motion){
11521145

11531146
if(!$this->justCreated){
11541147
if($this instanceof Player){
1155-
$pk = new SetEntityMotionPacket();
1156-
$pk->entities = [
1157-
[0, $this->motionX, $this->motionY, $this->motionZ]
1158-
];
1159-
$this->dataPacket($pk);
1148+
$this->addEntityMotion(0, $this->motionX, $this->motionY, $this->motionZ);
11601149
}
11611150
$this->updateMovement();
11621151
}

src/pocketmine/entity/FallingSand.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
use pocketmine\nbt\tag\Byte;
3232
use pocketmine\nbt\tag\Int;
3333
use pocketmine\network\protocol\AddEntityPacket;
34-
use pocketmine\network\protocol\SetEntityMotionPacket;
3534
use pocketmine\Player;
3635

3736
class FallingSand extends Entity{
@@ -159,11 +158,7 @@ public function spawnTo(Player $player){
159158
$pk->did = -($this->getBlock() | $this->getDamage() << 0x10);
160159
$player->dataPacket($pk);
161160

162-
$pk = new SetEntityMotionPacket();
163-
$pk->entities = [
164-
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
165-
];
166-
$player->dataPacket($pk);
161+
$player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ);
167162

168163
parent::spawnTo($player);
169164
}

src/pocketmine/entity/Human.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
use pocketmine\Network;
3333
use pocketmine\network\protocol\AddPlayerPacket;
3434
use pocketmine\network\protocol\RemovePlayerPacket;
35-
use pocketmine\network\protocol\SetEntityMotionPacket;
3635
use pocketmine\Player;
3736
use pocketmine\utils\TextFormat;
3837

@@ -175,11 +174,7 @@ public function spawnTo(Player $player){
175174
$pk->metadata = $this->getData();
176175
$player->dataPacket($pk);
177176

178-
$pk = new SetEntityMotionPacket();
179-
$pk->entities = [
180-
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
181-
];
182-
$player->dataPacket($pk);
177+
$player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ);
183178

184179
$this->inventory->sendArmorContents($player);
185180
}

src/pocketmine/entity/Item.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
use pocketmine\nbt\tag\Short;
3333
use pocketmine\nbt\tag\String;
3434
use pocketmine\network\protocol\AddItemEntityPacket;
35-
use pocketmine\network\protocol\SetEntityMotionPacket;
3635
use pocketmine\Player;
3736

3837
class Item extends Entity{
@@ -237,11 +236,7 @@ public function spawnTo(Player $player){
237236
$pk->item = $this->getItem();
238237
$player->dataPacket($pk);
239238

240-
$pk = new SetEntityMotionPacket();
241-
$pk->entities = [
242-
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
243-
];
244-
$player->dataPacket($pk);
239+
$player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ);
245240

246241
parent::spawnTo($player);
247242
}

src/pocketmine/entity/PrimedTNT.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use pocketmine\level\Explosion;
2929
use pocketmine\nbt\tag\Byte;
3030
use pocketmine\network\protocol\AddEntityPacket;
31-
use pocketmine\network\protocol\SetEntityMotionPacket;
3231
use pocketmine\Player;
3332

3433
class PrimedTNT extends Entity implements Explosive{
@@ -147,11 +146,7 @@ public function spawnTo(Player $player){
147146
$pk->did = 0;
148147
$player->dataPacket($pk);
149148

150-
$pk = new SetEntityMotionPacket();
151-
$pk->entities = [
152-
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
153-
];
154-
$player->dataPacket($pk);
149+
$player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ);
155150

156151
parent::spawnTo($player);
157152
}

src/pocketmine/entity/Snowball.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use pocketmine\level\format\FullChunk;
2626
use pocketmine\nbt\tag\Compound;
2727
use pocketmine\network\protocol\AddEntityPacket;
28-
use pocketmine\network\protocol\SetEntityMotionPacket;
2928
use pocketmine\Player;
3029

3130
class Snowball extends Projectile{
@@ -72,11 +71,7 @@ public function spawnTo(Player $player){
7271
$pk->did = 0; //TODO: send motion here
7372
$player->dataPacket($pk);
7473

75-
$pk = new SetEntityMotionPacket();
76-
$pk->entities = [
77-
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
78-
];
79-
$player->dataPacket($pk);
74+
$player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ);
8075

8176
parent::spawnTo($player);
8277
}

src/pocketmine/entity/Villager.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
use pocketmine\nbt\tag\Int;
2626
use pocketmine\network\protocol\AddMobPacket;
27-
use pocketmine\network\protocol\SetEntityMotionPacket;
2827
use pocketmine\Player;
2928

3029
class Villager extends Creature implements NPC, Ageable{
@@ -64,11 +63,7 @@ public function spawnTo(Player $player){
6463
$pk->metadata = $this->getData();
6564
$player->dataPacket($pk);
6665

67-
$pk = new SetEntityMotionPacket();
68-
$pk->entities = [
69-
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
70-
];
71-
$player->dataPacket($pk);
66+
$player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ);
7267

7368
parent::spawnTo($player);
7469
}

src/pocketmine/entity/Zombie.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use pocketmine\event\entity\EntityDamageByEntityEvent;
2626
use pocketmine\item\Item as ItemItem;
2727
use pocketmine\network\protocol\AddMobPacket;
28-
use pocketmine\network\protocol\SetEntityMotionPacket;
2928
use pocketmine\Player;
3029

3130
class Zombie extends Monster{
@@ -52,11 +51,7 @@ public function spawnTo(Player $player){
5251
$pk->metadata = $this->getData();
5352
$player->dataPacket($pk);
5453

55-
$pk = new SetEntityMotionPacket();
56-
$pk->entities = [
57-
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
58-
];
59-
$player->dataPacket($pk);
54+
$player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ);
6055

6156
parent::spawnTo($player);
6257
}

0 commit comments

Comments
 (0)