replaced more stuff with entity queue, added a limit to how many packets can be resent per tick

This commit is contained in:
GameHerobrine 2025-06-29 11:44:39 +03:00
parent 21f8796e89
commit 6d10cfd896
7 changed files with 31 additions and 24 deletions

View file

@ -433,7 +433,7 @@ class Player{
$pk->speedX = 0; $pk->speedX = 0;
$pk->speedY = 0; $pk->speedY = 0;
$pk->speedZ = 0; $pk->speedZ = 0;
$this->entityQueueDataPacket($pk); $this->dataPacketAlwaysRecover($pk);
$pk = new MovePlayerPacket; $pk = new MovePlayerPacket;
$pk->eid = $this->eid; $pk->eid = $this->eid;
@ -443,7 +443,7 @@ class Player{
$pk->bodyYaw = $yaw; $pk->bodyYaw = $yaw;
$pk->pitch = $pitch; $pk->pitch = $pitch;
$pk->yaw = $yaw; $pk->yaw = $yaw;
$this->entityQueueDataPacket($pk); $this->dataPacketAlwaysRecover($pk);
} }
/** /**
@ -1439,6 +1439,10 @@ class Player{
} }
public function handlePacketQueues(){ public function handlePacketQueues(){
if($this->server->ticks % 40 == 0){ //2s
$this->sendPing();
}
if($this->connected === false){ if($this->connected === false){
return false; return false;
} }
@ -1552,7 +1556,9 @@ class Player{
} }
if(($resendCnt = count($this->resendQueue)) > 0){ if(($resendCnt = count($this->resendQueue)) > 0){
$maxResendCnt = $resendCnt > 25 ? 25 : $resendCnt;
foreach($this->resendQueue as $count => $data){ foreach($this->resendQueue as $count => $data){
if(--$maxResendCnt) break;
unset($this->resendQueue[$count]); unset($this->resendQueue[$count]);
$this->packetStats[1]++; $this->packetStats[1]++;
$this->lag[] = $time - $data->sendtime; $this->lag[] = $time - $data->sendtime;
@ -3296,6 +3302,7 @@ class Player{
break; break;
case RakNetInfo::ACK: case RakNetInfo::ACK:
//if(mt_rand(0, 99) > 50) break;
foreach($packet->packets as $count){ foreach($packet->packets as $count){
if(isset($this->packetAlwaysRecoverQueue[$count])){ if(isset($this->packetAlwaysRecoverQueue[$count])){
$this->lag[] = $time - $this->packetAlwaysRecoverQueue[$count]->sendtime; $this->lag[] = $time - $this->packetAlwaysRecoverQueue[$count]->sendtime;

View file

@ -3,11 +3,16 @@
class PocketMinecraftServer{ class PocketMinecraftServer{
public $tCnt, $ticks; public $tCnt, $ticks;
public $extraprops, $serverID, $interface, $database, $version, $invisible, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $clients, $eidCnt, $custom, $description, $motd, $port, $saveEnabled; public $extraprops, $serverID, $interface, $database, $version, $invisible, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $eidCnt, $custom, $description, $motd, $port, $saveEnabled;
/** /**
* @var ServerAPI * @var ServerAPI
*/ */
public $api; public $api;
/**
* @var Player[]
*/
public $clients;
private $serverip, $evCnt, $handCnt, $events, $eventsID, $handlers, $serverType, $lastTick, $memoryStats, $async = [], $asyncID = 0; private $serverip, $evCnt, $handCnt, $events, $eventsID, $handlers, $serverType, $lastTick, $memoryStats, $async = [], $asyncID = 0;
public $doTick, $levelData, $tiles, $entities, $schedule, $scheduleCnt, $whitelist, $spawn, $difficulty, $stop, $asyncThread; public $doTick, $levelData, $tiles, $entities, $schedule, $scheduleCnt, $whitelist, $spawn, $difficulty, $stop, $asyncThread;
@ -631,9 +636,6 @@ class PocketMinecraftServer{
foreach($this->clients as $client){ foreach($this->clients as $client){
$client->handlePacketQueues(); $client->handlePacketQueues();
if($this->ticks % 40 == 0){ //2s
$client->sendPing();
}
} }
foreach($this->api->level->levels as $l){ foreach($this->api->level->levels as $l){

View file

@ -999,7 +999,9 @@ class Entity extends Position
$pk->yaw = $this->yaw; $pk->yaw = $this->yaw;
$pk->pitch = $this->pitch; $pk->pitch = $this->pitch;
$pk->bodyYaw = $this->yaw; $pk->bodyYaw = $this->yaw;
$this->server->api->player->broadcastPacket($players, $pk); foreach($players as $player){
if($player->hasEntity($this)) $player->entityQueueDataPacket(clone $pk);
}
} }
} }
} }
@ -1602,14 +1604,14 @@ class Entity extends Position
$pk->eid = $this->eid; $pk->eid = $this->eid;
$pk->event = EntityEventPacket::ENTITY_DAMAGE; $pk->event = EntityEventPacket::ENTITY_DAMAGE;
foreach($this->level->players as $p){ foreach($this->level->players as $p){
$p->entityQueueDataPacket(clone $pk); if($p->hasEntity($this)) $p->entityQueueDataPacket(clone $pk);
} }
} }
if($this->player instanceof Player){ if($this->player instanceof Player){
$pk = new SetHealthPacket(); $pk = new SetHealthPacket();
$pk->health = $this->health; $pk->health = $this->health;
$this->player->entityQueueDataPacket($pk); if($this->player->hasEntity($this)) $this->player->entityQueueDataPacket($pk);
} }
if($this->health <= 0 and $this->dead === false){ if($this->health <= 0 and $this->dead === false){
$this->makeDead($cause); $this->makeDead($cause);
@ -1646,12 +1648,16 @@ class Entity extends Position
$pk->z = -256; $pk->z = -256;
$pk->yaw = 0; $pk->yaw = 0;
$pk->pitch = 0; $pk->pitch = 0;
$this->server->api->player->broadcastPacket($this->level->players, $pk); foreach($this->level->players as $player){
if($player->hasEntity($this)) $player->entityQueueDataPacket(clone $pk);
}
}else{ }else{
$pk = new EntityEventPacket; $pk = new EntityEventPacket;
$pk->eid = $this->eid; $pk->eid = $this->eid;
$pk->event = EntityEventPacket::ENTITY_DEAD; $pk->event = EntityEventPacket::ENTITY_DEAD;
$this->server->api->player->broadcastPacket($this->level->players, $pk); foreach($this->level->players as $player){
if($player->hasEntity($this)) $player->entityQueueDataPacket(clone $pk);
}
} }
if($this->player instanceof Player){ if($this->player instanceof Player){

View file

@ -5,7 +5,9 @@ class TaskEatTileGoal extends TaskBase
public function onStart(EntityAI $ai) public function onStart(EntityAI $ai)
{ {
$this->selfCounter = 40; $this->selfCounter = 40;
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, new EntityEventPacket($ai->entity->eid, EntityEventPacket::ENTITY_ANIM_10)); foreach($ai->entity->level->players as $player){
if($player->hasEntity($ai->entity)) $player->entityQueueDataPacket(new EntityEventPacket($ai->entity->eid, EntityEventPacket::ENTITY_ANIM_10));
}
} }
public function onEnd(EntityAI $ai) public function onEnd(EntityAI $ai)

View file

@ -47,16 +47,6 @@ class Arrow extends Entity{
$data["shotByPlayer"] = $this->shotByPlayer; $data["shotByPlayer"] = $this->shotByPlayer;
return $data; return $data;
} }
public function handleUpdate(){
$pk = new MoveEntityPacket_PosRot;
$pk->eid = $this->eid;
$pk->x = $this->x;
$pk->y = $this->y;
$pk->z = $this->z;
$pk->yaw = $this->yaw;
$pk->pitch = $this->pitch;
$this->server->api->player->broadcastPacket($this->level->players, $pk);
}
public function shoot($d, $d1, $d2, $f, $f1){ //original name from 0.8.1 IDA decompilation, var names are taken from b1.7.3 public function shoot($d, $d1, $d2, $f, $f1){ //original name from 0.8.1 IDA decompilation, var names are taken from b1.7.3
$f2 = sqrt($d * $d + $d1 * $d1 + $d2 * $d2); $f2 = sqrt($d * $d + $d1 * $d1 + $d2 * $d2);

View file

@ -208,7 +208,7 @@ class DoorBlock extends TransparentBlock{
$pk->z = $this->z; $pk->z = $this->z;
$pk->evid = 1003; $pk->evid = 1003;
$pk->data = 0; $pk->data = 0;
ServerAPI::request()->api->player->broadcastPacket($players, $pk); ServerAPI::request()->api->player->broadcastPacket($players, $pk); //TODO use block queue?
return true; return true;
} }
return false; return false;

View file

@ -75,7 +75,7 @@ class FenceGateBlock extends TransparentBlock{
$pk->z = $this->z; $pk->z = $this->z;
$pk->evid = 1003; $pk->evid = 1003;
$pk->data = 0; $pk->data = 0;
ServerAPI::request()->api->player->broadcastPacket($players, $pk); ServerAPI::request()->api->player->broadcastPacket($players, $pk); //TODO use blok queue?
return true; return true;
} }
} }