mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-11 00:09:39 +00:00
move entity spawning logic back to Player
This commit is contained in:
parent
d7a7ab5102
commit
c1a483a36d
@ -977,12 +977,27 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
|
|||||||
assert(isset($this->usedChunks[World::chunkHash($x, $z)]));
|
assert(isset($this->usedChunks[World::chunkHash($x, $z)]));
|
||||||
$this->usedChunks[World::chunkHash($x, $z)] = true;
|
$this->usedChunks[World::chunkHash($x, $z)] = true;
|
||||||
|
|
||||||
$spawn = $this->spawnChunkLoadCount++ === $this->spawnThreshold;
|
$this->networkSession->startUsingChunk($x, $z, function(int $chunkX, int $chunkZ) : void{
|
||||||
$this->networkSession->startUsingChunk($x, $z, $spawn);
|
if($this->spawned){
|
||||||
|
$this->spawnEntitiesOnChunk($chunkX, $chunkZ);
|
||||||
if($spawn){
|
}elseif($this->spawnChunkLoadCount++ === $this->spawnThreshold){
|
||||||
//TODO: not sure this should be here
|
|
||||||
$this->spawned = true;
|
$this->spawned = true;
|
||||||
|
|
||||||
|
foreach($this->usedChunks as $chunkHash => $_){
|
||||||
|
World::getXZ($chunkHash, $_x, $_z);
|
||||||
|
$this->spawnEntitiesOnChunk($_x, $_z);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->networkSession->onTerrainReady();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function spawnEntitiesOnChunk(int $chunkX, int $chunkZ) : void{
|
||||||
|
foreach($this->world->getChunkEntities($chunkX, $chunkZ) as $entity){
|
||||||
|
if($entity !== $this and !$entity->isClosed() and !$entity->isFlaggedForDespawn()){
|
||||||
|
$entity->spawnTo($this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,6 +69,7 @@ use pocketmine\PlayerInfo;
|
|||||||
use pocketmine\Server;
|
use pocketmine\Server;
|
||||||
use pocketmine\timings\Timings;
|
use pocketmine\timings\Timings;
|
||||||
use pocketmine\utils\BinaryDataException;
|
use pocketmine\utils\BinaryDataException;
|
||||||
|
use pocketmine\utils\Utils;
|
||||||
use pocketmine\world\Position;
|
use pocketmine\world\Position;
|
||||||
use function bin2hex;
|
use function bin2hex;
|
||||||
use function count;
|
use function count;
|
||||||
@ -760,28 +761,26 @@ class NetworkSession{
|
|||||||
return $this->sendDataPacket($pk);
|
return $this->sendDataPacket($pk);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function startUsingChunk(int $chunkX, int $chunkZ, bool $spawn = false) : void{
|
/**
|
||||||
|
* Instructs the networksession to start using the chunk at the given coordinates. This may occur asynchronously.
|
||||||
|
* @param int $chunkX
|
||||||
|
* @param int $chunkZ
|
||||||
|
* @param \Closure $onCompletion To be called when chunk sending has completed.
|
||||||
|
*/
|
||||||
|
public function startUsingChunk(int $chunkX, int $chunkZ, \Closure $onCompletion) : void{
|
||||||
|
Utils::validateCallableSignature(function(int $chunkX, int $chunkZ){}, $onCompletion);
|
||||||
|
|
||||||
ChunkCache::getInstance($this->player->getWorld())->request($chunkX, $chunkZ)->onResolve(
|
ChunkCache::getInstance($this->player->getWorld())->request($chunkX, $chunkZ)->onResolve(
|
||||||
|
|
||||||
//this callback may be called synchronously or asynchronously, depending on whether the promise is resolved yet
|
//this callback may be called synchronously or asynchronously, depending on whether the promise is resolved yet
|
||||||
function(CompressBatchPromise $promise) use($chunkX, $chunkZ, $spawn){
|
function(CompressBatchPromise $promise) use($chunkX, $chunkZ, $onCompletion){
|
||||||
if(!$this->isConnected()){
|
if(!$this->isConnected()){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$this->player->world->timings->syncChunkSendTimer->startTiming();
|
$this->player->world->timings->syncChunkSendTimer->startTiming();
|
||||||
try{
|
try{
|
||||||
$this->queueCompressed($promise);
|
$this->queueCompressed($promise);
|
||||||
|
$onCompletion($chunkX, $chunkZ);
|
||||||
foreach($this->player->getWorld()->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{
|
}finally{
|
||||||
$this->player->world->timings->syncChunkSendTimer->stopTiming();
|
$this->player->world->timings->syncChunkSendTimer->stopTiming();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user