mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 01:46:04 +00:00
Fixed player spawning in ungenerated terrain (#4087)
fixes #4044 fixes #2724 this is significantly more complex than I hoped for, but it's a start... and it works.
This commit is contained in:
@ -34,7 +34,6 @@ use pocketmine\entity\animation\ArmSwingAnimation;
|
||||
use pocketmine\entity\animation\CriticalHitAnimation;
|
||||
use pocketmine\entity\effect\VanillaEffects;
|
||||
use pocketmine\entity\Entity;
|
||||
use pocketmine\entity\EntityDataHelper;
|
||||
use pocketmine\entity\Human;
|
||||
use pocketmine\entity\Living;
|
||||
use pocketmine\entity\Location;
|
||||
@ -261,7 +260,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
|
||||
/** @var SurvivalBlockBreakHandler|null */
|
||||
protected $blockBreakHandler = null;
|
||||
|
||||
public function __construct(Server $server, NetworkSession $session, PlayerInfo $playerInfo, bool $authenticated, ?CompoundTag $namedtag){
|
||||
public function __construct(Server $server, NetworkSession $session, PlayerInfo $playerInfo, bool $authenticated, Location $spawnLocation, ?CompoundTag $namedtag){
|
||||
$username = TextFormat::clean($playerInfo->getUsername());
|
||||
$this->logger = new \PrefixedLogger($server->getLogger(), "Player: $username");
|
||||
|
||||
@ -286,24 +285,15 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
|
||||
$this->spawnThreshold = (int) (($this->server->getConfigGroup()->getProperty("chunk-sending.spawn-radius", 4) ** 2) * M_PI);
|
||||
$this->chunkSelector = new ChunkSelector();
|
||||
|
||||
if($namedtag !== null and ($world = $this->server->getWorldManager()->getWorldByName($namedtag->getString("Level", ""))) !== null){
|
||||
$spawn = EntityDataHelper::parseLocation($namedtag, $world);
|
||||
$onGround = $namedtag->getByte("OnGround", 1) === 1;
|
||||
}else{
|
||||
$world = $this->server->getWorldManager()->getDefaultWorld();
|
||||
$spawn = Location::fromObject($world->getSafeSpawn(), $world);
|
||||
$onGround = true;
|
||||
}
|
||||
|
||||
$this->chunkLoader = new PlayerChunkLoader($spawn);
|
||||
$this->chunkLoader = new PlayerChunkLoader($spawnLocation);
|
||||
|
||||
$world = $spawnLocation->getWorld();
|
||||
//load the spawn chunk so we can see the terrain
|
||||
$world->registerChunkLoader($this->chunkLoader, $spawn->getFloorX() >> 4, $spawn->getFloorZ() >> 4, true);
|
||||
$world->registerChunkListener($this, $spawn->getFloorX() >> 4, $spawn->getFloorZ() >> 4);
|
||||
$this->usedChunks[World::chunkHash($spawn->getFloorX() >> 4, $spawn->getFloorZ() >> 4)] = UsedChunkStatus::NEEDED();
|
||||
$world->registerChunkLoader($this->chunkLoader, $spawnLocation->getFloorX() >> 4, $spawnLocation->getFloorZ() >> 4, true);
|
||||
$world->registerChunkListener($this, $spawnLocation->getFloorX() >> 4, $spawnLocation->getFloorZ() >> 4);
|
||||
$this->usedChunks[World::chunkHash($spawnLocation->getFloorX() >> 4, $spawnLocation->getFloorZ() >> 4)] = UsedChunkStatus::NEEDED();
|
||||
|
||||
parent::__construct($spawn, $this->playerInfo->getSkin(), $namedtag);
|
||||
$this->onGround = $onGround; //TODO: this hack is needed for new players in-air ticks - they don't get detected as on-ground until they move
|
||||
parent::__construct($spawnLocation, $this->playerInfo->getSkin(), $namedtag);
|
||||
|
||||
$ev = new PlayerLoginEvent($this, "Plugin reason");
|
||||
$ev->call();
|
||||
@ -712,6 +702,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
|
||||
Timings::$playerChunkSend->startTiming();
|
||||
|
||||
$count = 0;
|
||||
$world = $this->getWorld();
|
||||
foreach($this->loadQueue as $index => $distance){
|
||||
if($count >= $this->chunksPerTick){
|
||||
break;
|
||||
@ -728,30 +719,36 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
|
||||
$this->getWorld()->registerChunkLoader($this->chunkLoader, $X, $Z, true);
|
||||
$this->getWorld()->registerChunkListener($this, $X, $Z);
|
||||
|
||||
if(!$this->getWorld()->requestChunkPopulation($X, $Z)){
|
||||
continue;
|
||||
}
|
||||
|
||||
unset($this->loadQueue[$index]);
|
||||
$this->usedChunks[$index] = UsedChunkStatus::REQUESTED();
|
||||
|
||||
$this->getNetworkSession()->startUsingChunk($X, $Z, function(int $chunkX, int $chunkZ) use ($index) : void{
|
||||
$this->usedChunks[$index] = UsedChunkStatus::SENT();
|
||||
if($this->spawnChunkLoadCount === -1){
|
||||
$this->spawnEntitiesOnChunk($chunkX, $chunkZ);
|
||||
}elseif($this->spawnChunkLoadCount++ === $this->spawnThreshold){
|
||||
$this->spawnChunkLoadCount = -1;
|
||||
|
||||
foreach($this->usedChunks as $chunkHash => $status){
|
||||
if($status->equals(UsedChunkStatus::SENT())){
|
||||
World::getXZ($chunkHash, $_x, $_z);
|
||||
$this->spawnEntitiesOnChunk($_x, $_z);
|
||||
}
|
||||
$this->getWorld()->requestChunkPopulation($X, $Z, $this->chunkLoader)->onCompletion(
|
||||
function() use ($X, $Z, $index, $world) : void{
|
||||
if(!$this->isConnected() || !isset($this->usedChunks[$index]) || $world !== $this->getWorld()){
|
||||
return;
|
||||
}
|
||||
unset($this->loadQueue[$index]);
|
||||
$this->usedChunks[$index] = UsedChunkStatus::REQUESTED();
|
||||
|
||||
$this->getNetworkSession()->notifyTerrainReady();
|
||||
$this->getNetworkSession()->startUsingChunk($X, $Z, function(int $chunkX, int $chunkZ) use ($index) : void{
|
||||
$this->usedChunks[$index] = UsedChunkStatus::SENT();
|
||||
if($this->spawnChunkLoadCount === -1){
|
||||
$this->spawnEntitiesOnChunk($chunkX, $chunkZ);
|
||||
}elseif($this->spawnChunkLoadCount++ === $this->spawnThreshold){
|
||||
$this->spawnChunkLoadCount = -1;
|
||||
|
||||
foreach($this->usedChunks as $chunkHash => $status){
|
||||
if($status->equals(UsedChunkStatus::SENT())){
|
||||
World::getXZ($chunkHash, $_x, $_z);
|
||||
$this->spawnEntitiesOnChunk($_x, $_z);
|
||||
}
|
||||
}
|
||||
|
||||
$this->getNetworkSession()->notifyTerrainReady();
|
||||
}
|
||||
});
|
||||
},
|
||||
static function() : void{
|
||||
//NOOP: we'll re-request this if it fails anyway
|
||||
}
|
||||
});
|
||||
);
|
||||
}
|
||||
|
||||
Timings::$playerChunkSend->stopTiming();
|
||||
|
75
src/player/PlayerCreationPromise.php
Normal file
75
src/player/PlayerCreationPromise.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\player;
|
||||
|
||||
use function spl_object_id;
|
||||
|
||||
final class PlayerCreationPromise{
|
||||
/**
|
||||
* @var \Closure[]
|
||||
* @phpstan-var array<int, \Closure(Player) : void>
|
||||
*/
|
||||
private array $onSuccess = [];
|
||||
|
||||
/**
|
||||
* @var \Closure[]
|
||||
* @phpstan-var array<int, \Closure() : void>
|
||||
*/
|
||||
private array $onFailure = [];
|
||||
|
||||
private bool $resolved = false;
|
||||
private ?Player $result = null;
|
||||
|
||||
/**
|
||||
* @phpstan-param \Closure(Player) : void $onSuccess
|
||||
* @phpstan-param \Closure() : void $onFailure
|
||||
*/
|
||||
public function onCompletion(\Closure $onSuccess, \Closure $onFailure) : void{
|
||||
if($this->resolved){
|
||||
$this->result === null ? $onFailure() : $onSuccess($this->result);
|
||||
}else{
|
||||
$this->onSuccess[spl_object_id($onSuccess)] = $onSuccess;
|
||||
$this->onFailure[spl_object_id($onFailure)] = $onFailure;
|
||||
}
|
||||
}
|
||||
|
||||
public function resolve(Player $player) : void{
|
||||
$this->resolved = true;
|
||||
$this->result = $player;
|
||||
foreach($this->onSuccess as $c){
|
||||
$c($player);
|
||||
}
|
||||
$this->onSuccess = [];
|
||||
$this->onFailure = [];
|
||||
}
|
||||
|
||||
public function reject() : void{
|
||||
$this->resolved = true;
|
||||
foreach($this->onFailure as $c){
|
||||
$c();
|
||||
}
|
||||
$this->onSuccess = [];
|
||||
$this->onFailure = [];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user