ChunkLoader: remove getLoaderId() (take 2)

This commit is contained in:
Dylan K. Taylor 2019-01-25 18:58:01 +00:00
parent bb27c76d13
commit 6ee484e401
4 changed files with 13 additions and 33 deletions

View File

@ -240,8 +240,6 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
/** @var int */ /** @var int */
protected $gamemode; protected $gamemode;
/** @var int */
private $loaderId = 0;
/** @var bool[] chunkHash => bool (true = sent, false = needs sending) */ /** @var bool[] chunkHash => bool (true = sent, false = needs sending) */
public $usedChunks = []; public $usedChunks = [];
/** @var bool[] chunkHash => dummy */ /** @var bool[] chunkHash => dummy */
@ -690,7 +688,6 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
$this->networkSession = $session; $this->networkSession = $session;
$this->perm = new PermissibleBase($this); $this->perm = new PermissibleBase($this);
$this->loaderId = Level::generateChunkLoaderId($this);
$this->chunksPerTick = (int) $this->server->getProperty("chunk-sending.per-tick", 4); $this->chunksPerTick = (int) $this->server->getProperty("chunk-sending.per-tick", 4);
$this->spawnThreshold = (int) (($this->server->getProperty("chunk-sending.spawn-radius", 4) ** 2) * M_PI); $this->spawnThreshold = (int) (($this->server->getProperty("chunk-sending.spawn-radius", 4) ** 2) * M_PI);
@ -3393,8 +3390,4 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
public function onBlockChanged(Vector3 $block){ public function onBlockChanged(Vector3 $block){
} }
public function getLoaderId() : int{
return $this->loaderId;
}
} }

View File

@ -75,6 +75,7 @@ use function is_infinite;
use function is_nan; use function is_nan;
use function lcg_value; use function lcg_value;
use function sin; use function sin;
use function spl_object_id;
use const M_PI_2; use const M_PI_2;
abstract class Entity extends Location implements Metadatable, EntityIds{ abstract class Entity extends Location implements Metadatable, EntityIds{
@ -1621,10 +1622,10 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
if(!$this->justCreated){ if(!$this->justCreated){
$newChunk = $this->level->getViewersForPosition($this); $newChunk = $this->level->getViewersForPosition($this);
foreach($this->hasSpawned as $player){ foreach($this->hasSpawned as $player){
if(!isset($newChunk[$player->getLoaderId()])){ if(!isset($newChunk[spl_object_id($player)])){
$this->despawnFrom($player); $this->despawnFrom($player);
}else{ }else{
unset($newChunk[$player->getLoaderId()]); unset($newChunk[spl_object_id($player)]);
} }
} }
foreach($newChunk as $player){ foreach($newChunk as $player){
@ -1767,8 +1768,9 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
* @param Player $player * @param Player $player
*/ */
public function spawnTo(Player $player) : void{ public function spawnTo(Player $player) : void{
if(!isset($this->hasSpawned[$player->getLoaderId()])){ $id = spl_object_id($player);
$this->hasSpawned[$player->getLoaderId()] = $player; if(!isset($this->hasSpawned[$id])){
$this->hasSpawned[$id] = $player;
$this->sendSpawnPacket($player); $this->sendSpawnPacket($player);
} }
@ -1797,13 +1799,14 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
* @param bool $send * @param bool $send
*/ */
public function despawnFrom(Player $player, bool $send = true) : void{ public function despawnFrom(Player $player, bool $send = true) : void{
if(isset($this->hasSpawned[$player->getLoaderId()])){ $id = spl_object_id($player);
if(isset($this->hasSpawned[$id])){
if($send){ if($send){
$pk = new RemoveEntityPacket(); $pk = new RemoveEntityPacket();
$pk->entityUniqueId = $this->id; $pk->entityUniqueId = $this->id;
$player->sendDataPacket($pk); $player->sendDataPacket($pk);
} }
unset($this->hasSpawned[$player->getLoaderId()]); unset($this->hasSpawned[$id]);
} }
} }

View File

@ -39,14 +39,6 @@ use pocketmine\math\Vector3;
*/ */
interface ChunkLoader{ interface ChunkLoader{
/**
* Returns the ChunkLoader id.
* Call Level::generateChunkLoaderId($this) to generate and save it
*
* @return int
*/
public function getLoaderId() : int;
/** /**
* @return float * @return float
*/ */

View File

@ -106,6 +106,7 @@ use function max;
use function microtime; use function microtime;
use function min; use function min;
use function mt_rand; use function mt_rand;
use function spl_object_id;
use function strtolower; use function strtolower;
use function trim; use function trim;
use const INT32_MAX; use const INT32_MAX;
@ -117,7 +118,6 @@ use const M_PI;
class Level implements ChunkManager, Metadatable{ class Level implements ChunkManager, Metadatable{
private static $levelIdCounter = 1; private static $levelIdCounter = 1;
private static $chunkLoaderCounter = 1;
public const Y_MASK = 0xFF; public const Y_MASK = 0xFF;
public const Y_MAX = 0x100; //256 public const Y_MAX = 0x100; //256
@ -321,14 +321,6 @@ class Level implements ChunkManager, Metadatable{
$z = ($hash & 0xFFFFFFFF) << 32 >> 32; $z = ($hash & 0xFFFFFFFF) << 32 >> 32;
} }
public static function generateChunkLoaderId(ChunkLoader $loader) : int{
if($loader->getLoaderId() === 0){
return self::$chunkLoaderCounter++;
}else{
throw new \InvalidStateException("ChunkLoader has a loader id already assigned: " . $loader->getLoaderId());
}
}
/** /**
* @param string $str * @param string $str
* *
@ -642,7 +634,7 @@ class Level implements ChunkManager, Metadatable{
} }
public function registerChunkLoader(ChunkLoader $loader, int $chunkX, int $chunkZ, bool $autoLoad = true){ public function registerChunkLoader(ChunkLoader $loader, int $chunkX, int $chunkZ, bool $autoLoad = true){
$loaderId = $loader->getLoaderId(); $loaderId = spl_object_id($loader);
if(!isset($this->chunkLoaders[$chunkHash = Level::chunkHash($chunkX, $chunkZ)])){ if(!isset($this->chunkLoaders[$chunkHash = Level::chunkHash($chunkX, $chunkZ)])){
$this->chunkLoaders[$chunkHash] = []; $this->chunkLoaders[$chunkHash] = [];
@ -672,7 +664,7 @@ class Level implements ChunkManager, Metadatable{
public function unregisterChunkLoader(ChunkLoader $loader, int $chunkX, int $chunkZ){ public function unregisterChunkLoader(ChunkLoader $loader, int $chunkX, int $chunkZ){
$chunkHash = Level::chunkHash($chunkX, $chunkZ); $chunkHash = Level::chunkHash($chunkX, $chunkZ);
$loaderId = $loader->getLoaderId(); $loaderId = spl_object_id($loader);
if(isset($this->chunkLoaders[$chunkHash][$loaderId])){ if(isset($this->chunkLoaders[$chunkHash][$loaderId])){
unset($this->chunkLoaders[$chunkHash][$loaderId]); unset($this->chunkLoaders[$chunkHash][$loaderId]);
unset($this->playerLoaders[$chunkHash][$loaderId]); unset($this->playerLoaders[$chunkHash][$loaderId]);
@ -2401,7 +2393,7 @@ class Level implements ChunkManager, Metadatable{
$this->chunkSendQueue[$index] = []; $this->chunkSendQueue[$index] = [];
} }
$this->chunkSendQueue[$index][$player->getLoaderId()] = $player; $this->chunkSendQueue[$index][spl_object_id($player)] = $player;
} }
private function sendCachedChunk(int $x, int $z){ private function sendCachedChunk(int $x, int $z){