From 728b8f6d2ea0e17478531a3e8b8a66f4fb9a4d6b Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 1 Dec 2023 08:50:46 +0300 Subject: [PATCH] client-server sync when using items --- src/Player.php | 39 ++++++++++++++++--- .../protocol/packet/UpdateBlockPacket.php | 5 +++ src/world/Level.php | 31 ++++++++++++++- 3 files changed, 68 insertions(+), 7 deletions(-) diff --git a/src/Player.php b/src/Player.php index 6d32549e..76188788 100755 --- a/src/Player.php +++ b/src/Player.php @@ -68,9 +68,10 @@ class Player{ private $chunkCount = []; private $received = []; public $cratingItems; - public $sleepingTime = 0; + public $blockSendQueue; + public $blockSendQueueLength = 0; /** * @param integer $clientID * @param string $ip @@ -97,6 +98,8 @@ class Player{ $this->packetStats = [0, 0]; $this->buffer = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->buffer->data = []; + $this->blockSendQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $this->blockSendQueue->data = []; $this->server->schedule(1, [$this, "handlePacketQueues"], [], true); $this->server->schedule(20 * 60, [$this, "clearQueue"], [], true); $this->evid[] = $this->server->event("server.close", [$this, "close"]); @@ -248,7 +251,23 @@ class Player{ $pk->teleport = true; $this->dataPacket($pk); } - + public function addToBlockSendQueue(RakNetDataPacket $packet){ + if($this->connected === false) return false; + + $packet->encode(); + $len = strlen($packet->buffer) + 1; + $MTU = $this->MTU - 24; + + + if(($this->blockSendQueueLength + $len) >= $MTU){ + $this->sendBlockUpdateQueue(); + } + + $packet->messageIndex = $this->counter[3]++; + $packet->reliability = 2; + @$this->blockSendQueue->data[] = $packet; + $this->blockSendQueueLength += 6 + $len; + } /** * @param integer $id * @param array $data @@ -325,7 +344,17 @@ class Player{ $this->bandwidthRaw += $this->server->send($packet); } } - + + + public function sendBlockUpdateQueue(){ + if($this->blockSendQueueLength > 0 and $this->blockSendQueue instanceof RakNetPacket){ + $this->blockSendQueue->seqNumber = $this->counter[0]++; + $this->send($this->blockSendQueue); + } + $this->blockSendQueueLength = 0; + $this->blockSendQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $this->blockSendQueue->data = []; + } public function sendBuffer(){ if($this->bufferLen > 0 and $this->buffer instanceof RakNetPacket){ $this->buffer->seqNumber = $this->counter[0]++; @@ -1622,10 +1651,10 @@ class Player{ //$this->lastChunk = [$packet->chunkX, $packet->chunkZ]; break; case ProtocolInfo::UPDATE_BLOCK_PACKET: - if($this->gamemode & 0x01 === 0){ + if($this->gamemode & 0x01 === 1){ $this->level->setBlock(new Vector3($packet->x, $packet->y, $packet->z), BlockAPI::get($packet->block, $packet->meta)); } - + $this->level->resendBlocksToPlayers[$this->CID]["{$packet->x}.{$packet->y}.{$packet->z}"] = true; //bad client i hate u break; case ProtocolInfo::USE_ITEM_PACKET: if(!($this->entity instanceof Entity)){ diff --git a/src/network/protocol/packet/UpdateBlockPacket.php b/src/network/protocol/packet/UpdateBlockPacket.php index 230e969b..141f61b1 100644 --- a/src/network/protocol/packet/UpdateBlockPacket.php +++ b/src/network/protocol/packet/UpdateBlockPacket.php @@ -12,6 +12,11 @@ class UpdateBlockPacket extends RakNetDataPacket{ } public function decode(){ + $this->x = $this->getInt(); + $this->z = $this->getInt(); + $this->y = $this->getByte(); + $this->block = $this->getByte(); + $this->meta = $this->getByte(); } public function encode(){ diff --git a/src/world/Level.php b/src/world/Level.php index 48b08d6c..1ff5fa0c 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -20,7 +20,7 @@ class Level{ public $entityList; public $tiles, $blockUpdates, $nextSave, $players = [], $level, $mobSpawner; public $time, $startCheck, $startTime, $server, $name, $usedChunks, $changedBlocks, $changedCount, $stopTime; - + public $resendBlocksToPlayers = []; public $generator; public function __construct(PMFLevel $level, Config $entities, Config $tiles, Config $blockUpdates, $name){ $this->server = ServerAPI::request(); @@ -452,7 +452,34 @@ class Level{ $this->level->unloadChunk($xz[0], $xz[1]); unset($this->level->fakeLoaded[$ind]); } - + foreach($this->resendBlocksToPlayers as $playerCID => $blockz){ + /** + * @var Player $player + */ + $player = $this->server->clients[$playerCID] ?? false; + if($player instanceof Player){ + foreach($blockz as $xyz => $_){ + $xyzz = explode(".", $xyz); + if(count($xyzz) == 3){ + $x = (int) $xyzz[0]; + $y = (int) $xyzz[1]; + $z = (int) $xyzz[2]; + $idm = $this->level->getBlock($x, $y, $z); + //$player->addToBlockSendQueue(new UpdateBlockPacket($x, $y, $z, $idm[0], $idm[1])); + $pk = new UpdateBlockPacket; + $pk->x = $x; + $pk->y = $y; + $pk->z = $z; + $pk->block = $idm[0] & 0xff; + $pk->meta = $idm[1] & 0xff; + $player->addToBlockSendQueue($pk); + } + unset($blockz[$xyz]); + } + $player->sendBlockUpdateQueue(); + } + unset($this->resendBlocksToPlayers[$playerCID]); + } } public function setBlock(Vector3 $pos, Block $block, $update = true, $tiles = false, $direct = false){