From ff663a2912351f3f51e3042f2c43171c707b1998 Mon Sep 17 00:00:00 2001 From: Muqsit Date: Fri, 2 Jun 2023 02:05:34 +0000 Subject: [PATCH] Make a few readonly properties public, allow direct property access --- .../events/PerWorldPlayerDataSaveEvent.php | 2 +- .../PerWorldPlayer/player/PlayerInstance.php | 18 +++++++----------- .../PerWorldPlayer/world/WorldInstance.php | 16 +++++----------- .../world/database/LibasynqlWorldDatabase.php | 4 ++-- 4 files changed, 15 insertions(+), 25 deletions(-) diff --git a/src/BlockHorizons/PerWorldPlayer/events/PerWorldPlayerDataSaveEvent.php b/src/BlockHorizons/PerWorldPlayer/events/PerWorldPlayerDataSaveEvent.php index 06cfa46..efc965d 100644 --- a/src/BlockHorizons/PerWorldPlayer/events/PerWorldPlayerDataSaveEvent.php +++ b/src/BlockHorizons/PerWorldPlayer/events/PerWorldPlayerDataSaveEvent.php @@ -16,7 +16,7 @@ class PerWorldPlayerDataSaveEvent extends PerWorldPlayerDataEvent implements Can public const CAUSE_PLAYER_QUIT = 1; public const CAUSE_CUSTOM = 2; - readonly private int $cause; + readonly public int $cause; public function __construct(Player $player, WorldInstance $worldInstance, PlayerWorldData $playerWorldData, int $cause){ parent::__construct($player, $worldInstance, $playerWorldData); diff --git a/src/BlockHorizons/PerWorldPlayer/player/PlayerInstance.php b/src/BlockHorizons/PerWorldPlayer/player/PlayerInstance.php index 00839f9..cc89679 100644 --- a/src/BlockHorizons/PerWorldPlayer/player/PlayerInstance.php +++ b/src/BlockHorizons/PerWorldPlayer/player/PlayerInstance.php @@ -22,7 +22,7 @@ final class PlayerInstance{ private const WORLD_DATA_CACHE_SIZE = 8; - readonly private Logger $logger; + readonly public Logger $logger; private int $lock_ids = 0; @@ -45,10 +45,6 @@ public function __construct( $this->logger = new PrefixedLogger($this->loader->getLogger(), $player->getName()); } - public function getLogger() : Logger{ - return $this->logger; - } - public function acquireLock() : int{ $this->locks[$id = $this->lock_ids++] = $id; return $id; @@ -86,8 +82,8 @@ public function waitForUnlock(Closure $callback) : void{ * @param Closure(PlayerWorldData) : void $callback */ public function loadWorldData(WorldInstance $world, Closure $callback) : void{ - if(isset($this->world_data[$name = $world->getName()])){ - $this->getLogger()->debug("Loaded data for world " . $name . " from memory"); + if(isset($this->world_data[$name = $world->name])){ + $this->logger->debug("Loaded data for world " . $name . " from memory"); $callback($this->world_data[$name]); return; } @@ -107,7 +103,7 @@ public function loadWorldData(WorldInstance $world, Closure $callback) : void{ $player = $weak_player->get(); if($player !== null){ $instance = $loader->getPlayerManager()->get($player); - $instance->getLogger()->debug("Loaded data for world " . $name . " from database"); + $instance->logger->debug("Loaded data for world " . $name . " from database"); $instance->onWorldDataLoad($name, $data); $instance->releaseLock($lock); } @@ -130,10 +126,10 @@ private function onWorldDataLoad(string $world_name, PlayerWorldData $data) : vo } public function saveWorldData(WorldInstance $world, PlayerWorldData $data, int $cause) : void{ - $this->world_data[$world->getName()] = $data; + $this->world_data[$world->name] = $data; - $name = $world->getName(); - $logger = $this->getLogger(); + $name = $world->name; + $logger = $this->logger; $logger->debug("Saving data for world " . $name . "..."); $this->loader->getWorldManager()->getDatabase()->save($world, $this->player, $data, $cause, static function(bool $success) use($name, $logger, $cause) : void{ if($success){ diff --git a/src/BlockHorizons/PerWorldPlayer/world/WorldInstance.php b/src/BlockHorizons/PerWorldPlayer/world/WorldInstance.php index 51a0b4a..4df87c2 100644 --- a/src/BlockHorizons/PerWorldPlayer/world/WorldInstance.php +++ b/src/BlockHorizons/PerWorldPlayer/world/WorldInstance.php @@ -7,8 +7,10 @@ use BlockHorizons\PerWorldPlayer\events\PerWorldPlayerDataInjectEvent; use BlockHorizons\PerWorldPlayer\events\PerWorldPlayerDataSaveEvent; use BlockHorizons\PerWorldPlayer\Loader; +use BlockHorizons\PerWorldPlayer\player\PlayerInstance; use BlockHorizons\PerWorldPlayer\util\WeakPlayer; use BlockHorizons\PerWorldPlayer\world\data\PlayerWorldData; +use Logger; use pocketmine\player\Player; use pocketmine\world\World; @@ -19,8 +21,8 @@ private static function haveSameBundles(self $a, self $b) : bool{ } readonly private Loader $loader; - readonly private string $name; - readonly private ?string $bundle; + readonly public string $name; + readonly public ?string $bundle; public function __construct(Loader $loader, World $world, ?string $bundle){ $this->loader = $loader; @@ -28,14 +30,6 @@ public function __construct(Loader $loader, World $world, ?string $bundle){ $this->bundle = $bundle; } - public function getName() : string{ - return $this->name; - } - - public function getBundle() : ?string{ - return $this->bundle; - } - public function onPlayerEnter(Player $player, ?WorldInstance $from_world = null) : void{ if($player->hasPermission("per-world-player.bypass")){ return; @@ -90,7 +84,7 @@ public function save(Player $player, PlayerWorldData $data, int $cause = PerWorl $instance = $this->loader->getPlayerManager()->get($player); if($ev->isCancelled()){ - $instance->getLogger()->debug("Data for world " . $this->getName() . " failed to save due to event cancellation"); + $instance->logger->debug("Data for world " . $this->name . " failed to save due to event cancellation"); return; } diff --git a/src/BlockHorizons/PerWorldPlayer/world/database/LibasynqlWorldDatabase.php b/src/BlockHorizons/PerWorldPlayer/world/database/LibasynqlWorldDatabase.php index c41d8e9..f934b7d 100644 --- a/src/BlockHorizons/PerWorldPlayer/world/database/LibasynqlWorldDatabase.php +++ b/src/BlockHorizons/PerWorldPlayer/world/database/LibasynqlWorldDatabase.php @@ -18,8 +18,8 @@ abstract class LibasynqlWorldDatabase implements WorldDatabase{ private static function createIdentifier(Player $player, WorldInstance $world) : string{ $name = strtolower($player->getName()); - $bundle = $world->getBundle(); - return chr(strlen($name)) . $name . chr($bundle !== null ? 1 : 0) . ($bundle ?? $world->getName()); + $bundle = $world->bundle; + return chr(strlen($name)) . $name . chr($bundle !== null ? 1 : 0) . ($bundle ?? $world->name); } private DataConnector $database;