Player no longer implements ChunkLoader

this stops plugins from misusing Player as a ChunkLoader in ways it doesn't know about, causing leaks and god knows what else.
This commit is contained in:
Dylan K. Taylor 2020-07-11 18:48:10 +01:00
parent 180c0e4999
commit 217f9c574a
2 changed files with 58 additions and 21 deletions

View File

@ -134,7 +134,7 @@ use const PHP_INT_MAX;
/**
* Main class that handles networking, recovery, and packet sending to the server part
*/
class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, IPlayer{
class Player extends Human implements CommandSender, ChunkListener, IPlayer{
use PermissibleDelegateTrait {
recalculatePermissions as private delegateRecalculatePermissions;
}
@ -213,6 +213,8 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
protected $chunksPerTick;
/** @var ChunkSelector */
protected $chunkSelector;
/** @var TickingChunkLoader */
protected $chunkLoader;
/** @var bool[] map: raw UUID (string) => bool */
protected $hiddenPlayers = [];
@ -293,8 +295,10 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
$onGround = true;
}
$this->chunkLoader = new TickingChunkLoader($spawn);
//load the spawn chunk so we can see the terrain
$world->registerChunkLoader($this, $spawn->getFloorX() >> 4, $spawn->getFloorZ() >> 4, true);
$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();
@ -742,7 +746,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
$this->networkSession->stopUsingChunk($x, $z);
unset($this->usedChunks[$index]);
}
$world->unregisterChunkLoader($this, $x, $z);
$world->unregisterChunkLoader($this->chunkLoader, $x, $z);
$world->unregisterChunkListener($this, $x, $z);
unset($this->loadQueue[$index]);
}
@ -776,7 +780,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
++$count;
$this->usedChunks[$index] = UsedChunkStatus::NEEDED();
$this->getWorld()->registerChunkLoader($this, $X, $Z, true);
$this->getWorld()->registerChunkLoader($this->chunkLoader, $X, $Z, true);
$this->getWorld()->registerChunkListener($this, $X, $Z);
if(!$this->getWorld()->populateChunk($X, $Z)){
@ -871,6 +875,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
$this->loadQueue = $newOrder;
if(count($this->loadQueue) > 0 or count($unloadChunks) > 0){
$this->chunkLoader->setCurrentLocation($this->location);
$this->networkSession->syncViewAreaCenterPoint($this->location, $this->viewDistance);
}
@ -2338,21 +2343,4 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
$this->nextChunkOrderRun = 0;
}
}
/**
* @see ChunkLoader::getX()
* @return float
*/
public function getX(){
return $this->location->getX();
}
/**
* @see ChunkLoader::getZ()
* @return float
*/
public function getZ(){
return $this->location->getZ();
}
}

View File

@ -0,0 +1,49 @@
<?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 pocketmine\math\Vector3;
use pocketmine\world\ChunkLoader;
final class TickingChunkLoader implements ChunkLoader{
/** @var Vector3 */
private $currentLocation;
public function __construct(Vector3 $currentLocation){
$this->currentLocation = $currentLocation;
}
public function setCurrentLocation(Vector3 $currentLocation) : void{
$this->currentLocation = $currentLocation;
}
public function getX(){
return $this->currentLocation->getFloorX();
}
public function getZ(){
return $this->currentLocation->getFloorZ();
}
}