Merge pull request #54 from Sunch233/master

[enchant] Player Ping
This commit is contained in:
enirboreHemaG 2025-03-21 07:14:12 +03:00 • committed by GitHub
commit 980e5f9ca7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 3 deletions

View file

@ -84,6 +84,8 @@ class Player{
* @var boolean $isSneaking
*/
public $isJumping, $isSneaking;
private $lastPing = -1;
/**
* @param integer $clientID
@ -1503,6 +1505,17 @@ class Player{
}
}
public function sendPing() {
$pk = new PingPacket;
$pk->time = intdiv(hrtime(true), 1_000_000);
$this->directDataPacket($pk);
}
public function getPing() {
return $this->lastPing;
}
public function handleDataPacket(RakNetDataPacket $packet){
if($this->connected === false){
return;
@ -1511,18 +1524,20 @@ class Player{
if(EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY){
return;
}
switch($packet->pid()){
case 0x01:
break;
case ProtocolInfo::PONG_PACKET:
$currentTime = intdiv(hrtime(true), 1_000_000);
if($currentTime > $packet->ptime){
$this->lastPing = $currentTime - $packet->ptime;
}
break;
case ProtocolInfo::PING_PACKET:
$pk = new PongPacket;
$pk->ptime = $packet->time;
$pk->time = abs(microtime(true) * 1000);
$pk->time = intdiv(hrtime(true), 1_000_000);
$this->directDataPacket($pk);
break;
case ProtocolInfo::DISCONNECT_PACKET:

View file

@ -618,6 +618,9 @@ class PocketMinecraftServer{
foreach($this->clients as $client){
$client->handlePacketQueues();
if($this->ticks % 40 == 0){ //2s
$client->sendPing();
}
}
foreach($this->api->level->levels as $l){