First look at separating chunk sending from Level

This commit is contained in:
Dylan K. Taylor
2019-04-17 19:33:37 +01:00
parent 0973e39697
commit 939dfd9269
5 changed files with 337 additions and 111 deletions

View File

@ -744,6 +744,43 @@ class NetworkSession{
return $this->sendDataPacket($pk);
}
public function startUsingChunk(int $chunkX, int $chunkZ, bool $spawn = false) : void{
ChunkCache::getInstance($this->player->getLevel())->request($chunkX, $chunkZ)->onResolve(
//this callback may be called synchronously or asynchronously, depending on whether the promise is resolved yet
function(CompressBatchPromise $promise) use($chunkX, $chunkZ, $spawn){
if(!$this->isConnected()){
return;
}
$this->player->level->timings->syncChunkSendTimer->startTiming();
try{
$this->queueCompressed($promise);
foreach($this->player->getLevel()->getChunkEntities($chunkX, $chunkZ) as $entity){
if($entity !== $this->player and !$entity->isClosed() and !$entity->isFlaggedForDespawn()){
$entity->spawnTo($this->player);
}
}
if($spawn){
//TODO: potential race condition during chunk sending could cause this to be called too early
$this->onTerrainReady();
}
}finally{
$this->player->level->timings->syncChunkSendTimer->stopTiming();
}
}
);
}
public function stopUsingChunk(int $chunkX, int $chunkZ) : void{
foreach($this->player->getLevel()->getChunkEntities($chunkX, $chunkZ) as $entity){
if($entity !== $this->player){
$entity->despawnFrom($this->player);
}
}
}
public function tick() : bool{
if($this->handler instanceof LoginSessionHandler){
if(time() >= $this->connectTime + 10){