From c092a2e83698b9ca31f1d9e14fd81b82b8b16b94 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 18 Mar 2021 23:19:27 +0000 Subject: [PATCH] Separate TickingChunkLoader from ChunkLoader this makes it possible to keep chunks loaded without ticking them. --- changelogs/4.0-snapshot.md | 3 ++ src/player/Player.php | 4 +- ...gChunkLoader.php => PlayerChunkLoader.php} | 4 +- src/world/ChunkLoader.php | 11 +---- src/world/TickingChunkLoader.php | 42 +++++++++++++++++++ src/world/World.php | 4 ++ 6 files changed, 54 insertions(+), 14 deletions(-) rename src/player/{TickingChunkLoader.php => PlayerChunkLoader.php} (92%) create mode 100644 src/world/TickingChunkLoader.php diff --git a/changelogs/4.0-snapshot.md b/changelogs/4.0-snapshot.md index cefa1c955..4a4fd5768 100644 --- a/changelogs/4.0-snapshot.md +++ b/changelogs/4.0-snapshot.md @@ -774,6 +774,9 @@ This version features substantial changes to the network system, improving coher - `BlockTransaction`: allows creating batch commits of block changes with validation conditions - if any block can't be applied, the whole transaction fails to apply. - `ChunkListenerNoOpTrait`: contains default no-op stubs for chunk listener implementations - `ChunkListener`: interface allowing subscribing to events happening on a given chunk + - `TickingChunkLoader`: a `ChunkLoader` specialization that allows ticking chunks +- `ChunkLoader` no longer requires implementing `getX()` and `getZ()`. +- `ChunkLoader` no longer causes chunks to get random updates. If this behaviour is needed, implement `TickingChunkLoader`. - The following classes have been renamed: - `pocketmine\world\utils\SubChunkIteratorManager` -> `pocketmine\world\utils\SubChunkExplorer` - The following API methods have been added: diff --git a/src/player/Player.php b/src/player/Player.php index c6f7e74f3..62a6b589e 100644 --- a/src/player/Player.php +++ b/src/player/Player.php @@ -211,7 +211,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{ protected $chunksPerTick; /** @var ChunkSelector */ protected $chunkSelector; - /** @var TickingChunkLoader */ + /** @var PlayerChunkLoader */ protected $chunkLoader; /** @var bool[] map: raw UUID (string) => bool */ @@ -295,7 +295,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{ $onGround = true; } - $this->chunkLoader = new TickingChunkLoader($spawn); + $this->chunkLoader = new PlayerChunkLoader($spawn); //load the spawn chunk so we can see the terrain $world->registerChunkLoader($this->chunkLoader, $spawn->getFloorX() >> 4, $spawn->getFloorZ() >> 4, true); diff --git a/src/player/TickingChunkLoader.php b/src/player/PlayerChunkLoader.php similarity index 92% rename from src/player/TickingChunkLoader.php rename to src/player/PlayerChunkLoader.php index 0a95240b4..597b95d82 100644 --- a/src/player/TickingChunkLoader.php +++ b/src/player/PlayerChunkLoader.php @@ -24,9 +24,9 @@ declare(strict_types=1); namespace pocketmine\player; use pocketmine\math\Vector3; -use pocketmine\world\ChunkLoader; +use pocketmine\world\TickingChunkLoader; -final class TickingChunkLoader implements ChunkLoader{ +final class PlayerChunkLoader implements TickingChunkLoader{ /** @var Vector3 */ private $currentLocation; diff --git a/src/world/ChunkLoader.php b/src/world/ChunkLoader.php index e68f88b56..a2c1e3b9a 100644 --- a/src/world/ChunkLoader.php +++ b/src/world/ChunkLoader.php @@ -24,7 +24,7 @@ declare(strict_types=1); namespace pocketmine\world; /** - * If you want to keep chunks loaded, implement this interface and register it into World. This will also tick chunks. + * If you want to keep chunks loaded, implement this interface and register it into World. * * @see World::registerChunkLoader() * @see World::unregisterChunkLoader() @@ -34,13 +34,4 @@ namespace pocketmine\world; */ interface ChunkLoader{ - /** - * @return float - */ - public function getX(); - - /** - * @return float - */ - public function getZ(); } diff --git a/src/world/TickingChunkLoader.php b/src/world/TickingChunkLoader.php new file mode 100644 index 000000000..d6bbf9481 --- /dev/null +++ b/src/world/TickingChunkLoader.php @@ -0,0 +1,42 @@ + $this->chunkTickRadius ? $this->chunkTickRadius : $randRange); foreach($this->loaders as $loader){ + if(!($loader instanceof TickingChunkLoader)){ + //TODO: maybe we should just not track non-ticking chunk loaders here? + continue; + } $chunkX = (int) floor($loader->getX()) >> 4; $chunkZ = (int) floor($loader->getZ()) >> 4;