Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: BlockHorizons/PerWorldPlayer
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.4.7
Choose a base ref
...
head repository: BlockHorizons/PerWorldPlayer
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 3 commits
  • 14 files changed
  • 1 contributor

Commits on Jun 2, 2023

  1. Copy the full SHA
    68141e8 View commit details
  2. Copy the full SHA
    f63822b View commit details
  3. Copy the full SHA
    ff663a2 View commit details
Original file line number Diff line number Diff line change
@@ -12,8 +12,8 @@
abstract class PerWorldPlayerDataEvent extends Event{

public function __construct(
private Player $player,
private WorldInstance $worldInstance,
readonly private Player $player,
readonly private WorldInstance $worldInstance,
private PlayerWorldData $playerWorldData
){}

Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ class PerWorldPlayerDataSaveEvent extends PerWorldPlayerDataEvent implements Can
public const CAUSE_PLAYER_QUIT = 1;
public const CAUSE_CUSTOM = 2;

private int $cause;
readonly public int $cause;

public function __construct(Player $player, WorldInstance $worldInstance, PlayerWorldData $playerWorldData, int $cause){
parent::__construct($player, $worldInstance, $playerWorldData);
47 changes: 15 additions & 32 deletions src/BlockHorizons/PerWorldPlayer/player/PlayerInstance.php
Original file line number Diff line number Diff line change
@@ -22,9 +22,7 @@ final class PlayerInstance{

private const WORLD_DATA_CACHE_SIZE = 8;

private Loader $loader;
private Player $player;
private Logger $logger;
readonly public Logger $logger;

private int $lock_ids = 0;

@@ -34,30 +32,19 @@ final class PlayerInstance{
/** @var PlayerWorldData[] */
private array $world_data = [];

/**
* @var Closure[][]
*
* @phpstan-var array<string, array<int, Closure(PlayerWorldData) : void>>
*/
/** @var array<string, array<int, Closure(PlayerWorldData) : void>> */
private array $world_data_callbacks = [];

/**
* @var Closure[]
*
* @phpstan-var array<Closure() : void>
*/
/** @var array<Closure() : void> */
private array $on_locks_release = [];

public function __construct(Loader $loader, Player $player){
$this->loader = $loader;
$this->player = $player;
public function __construct(
readonly private Loader $loader,
readonly private Player $player
){
$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;
@@ -79,9 +66,7 @@ public function isLocked() : bool{
}

/**
* @param Closure $callback
*
* @phpstan-param Closure() : void $callback
* @param Closure() : void $callback
*/
public function waitForUnlock(Closure $callback) : void{
if($this->isLocked()){
@@ -94,13 +79,11 @@ public function waitForUnlock(Closure $callback) : void{

/**
* @param WorldInstance $world
* @param Closure $callback
*
* @phpstan-param Closure(PlayerWorldData) : void $callback
* @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;
}
@@ -120,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);
}
@@ -143,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){
2 changes: 1 addition & 1 deletion src/BlockHorizons/PerWorldPlayer/player/PlayerListener.php
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
final class PlayerListener implements Listener{

public function __construct(
private PlayerManager $manager
readonly private PlayerManager $manager
){}

private function shouldCancelEvent(Player $player) : bool{
7 changes: 3 additions & 4 deletions src/BlockHorizons/PerWorldPlayer/player/PlayerManager.php
Original file line number Diff line number Diff line change
@@ -9,13 +9,12 @@

final class PlayerManager{

private Loader $loader;

/** @var PlayerInstance[] */
private array $players = [];

public function __construct(Loader $loader){
$this->loader = $loader;
public function __construct(
readonly private Loader $loader
){
$this->loader->getServer()->getPluginManager()->registerEvents(new PlayerListener($this), $this->loader);
}

4 changes: 2 additions & 2 deletions src/BlockHorizons/PerWorldPlayer/util/WeakPlayer.php
Original file line number Diff line number Diff line change
@@ -16,8 +16,8 @@ public static function from(Player $player) : self{
}

private function __construct(
private UuidInterface $uuid,
private int $object_id
readonly private UuidInterface $uuid,
readonly private int $object_id
){}

public function get() : ?Player{
18 changes: 6 additions & 12 deletions src/BlockHorizons/PerWorldPlayer/world/WorldInstance.php
Original file line number Diff line number Diff line change
@@ -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;

@@ -18,24 +20,16 @@ private static function haveSameBundles(self $a, self $b) : bool{
return $a->bundle !== null && $b->bundle !== null && $a->bundle === $b->bundle;
}

private Loader $loader;
private string $name;
private ?string $bundle;
readonly private Loader $loader;
readonly public string $name;
readonly public ?string $bundle;

public function __construct(Loader $loader, World $world, ?string $bundle){
$this->loader = $loader;
$this->name = $world->getFolderName();
$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;
}

11 changes: 4 additions & 7 deletions src/BlockHorizons/PerWorldPlayer/world/WorldListener.php
Original file line number Diff line number Diff line change
@@ -16,13 +16,10 @@

final class WorldListener implements Listener{

private PlayerManager $player_manager;
private WorldManager $world_manager;

public function __construct(PlayerManager $player_manager, WorldManager $world_manager){
$this->player_manager = $player_manager;
$this->world_manager = $world_manager;

public function __construct(
readonly private PlayerManager $player_manager,
readonly private WorldManager $world_manager
){
foreach(Server::getInstance()->getWorldManager()->getWorlds() as $world){
$this->world_manager->onWorldLoad($world);
}
6 changes: 3 additions & 3 deletions src/BlockHorizons/PerWorldPlayer/world/WorldManager.php
Original file line number Diff line number Diff line change
@@ -16,9 +16,9 @@

final class WorldManager{

private Loader $loader;
private BundleManager $bundle;
private WorldDatabase $database;
readonly private Loader $loader;
readonly private BundleManager $bundle;
readonly private WorldDatabase $database;

/** @var WorldInstance[] */
private array $worlds = [];
Original file line number Diff line number Diff line change
@@ -15,9 +15,7 @@ final class BundleManager{
private array $bundled_worlds = [];

/**
* @param mixed[] $bundled_worlds_configuration
*
* @phpstan-param array<string, array<string>> $bundled_worlds_configuration
* @param array<string, array<string>> $bundled_worlds_configuration
*/
public function __construct(array $bundled_worlds_configuration){
/**
40 changes: 16 additions & 24 deletions src/BlockHorizons/PerWorldPlayer/world/data/PlayerWorldData.php
Original file line number Diff line number Diff line change
@@ -17,14 +17,10 @@ public static function empty() : PlayerWorldData{
}

/**
* @param Item[] $armor
* @param Item[] $inventory
* @param Item[] $ender
* @param array<int, Item> $armor
* @param array<int, Item> $inventory
* @param array<int, Item> $ender
* @return self
*
* @phpstan-param array<int, Item> $armor
* @phpstan-param array<int, Item> $inventory
* @phpstan-param array<int, Item> $ender
*/
public static function emptyWithInventory(array $armor, array $inventory, array $ender) : PlayerWorldData{
return new self($armor, $inventory, $ender, 20.0, [], Server::getInstance()->getGamemode(), 0, 20.0, 0.0, 5.0);
@@ -59,31 +55,27 @@ public static function fromPlayer(Player $player) : PlayerWorldData{
}

/**
* @param Item[] $armor_inventory
* @param Item[] $inventory
* @param Item[] $ender_inventory
* @param array<int, Item> $armor_inventory
* @param array<int, Item> $inventory
* @param array<int, Item> $ender_inventory
* @param float $health
* @param EffectInstance[] $effects
* @param GameMode $gamemode
* @param int $experience
* @param float $food
* @param float $exhaustion
* @param float $saturation
*
* @phpstan-param array<int, Item> $armor_inventory
* @phpstan-param array<int, Item> $inventory
* @phpstan-param array<int, Item> $ender_inventory
*/
public function __construct(
public array $armor_inventory,
public array $inventory,
public array $ender_inventory,
public float $health,
public array $effects,
public GameMode $gamemode,
public int $experience,
public float $food,
public float $exhaustion,
public float $saturation
readonly public array $armor_inventory,
readonly public array $inventory,
readonly public array $ender_inventory,
readonly public float $health,
readonly public array $effects,
readonly public GameMode $gamemode,
readonly public int $experience,
readonly public float $food,
readonly public float $exhaustion,
readonly public float $saturation
){}
}
10 changes: 2 additions & 8 deletions src/BlockHorizons/PerWorldPlayer/world/data/SaveDataManager.php
Original file line number Diff line number Diff line change
@@ -11,11 +11,7 @@

final class SaveDataManager{

/**
* @var Closure[]
*
* @phpstan-var array<string, Closure(PlayerWorldData $data, Player $player) : void>
*/
/** @var array<string, Closure(PlayerWorldData $data, Player $player) : void> */
private array $injectors = [];

public function __construct(Loader $loader){
@@ -52,9 +48,7 @@ public function __construct(Loader $loader){

/**
* @param string $identifier
* @param Closure $injector
*
* @phpstan-param Closure(PlayerWorldData $data, Player $player) : void $injector
* @param Closure(PlayerWorldData $data, Player $player) : void $injector
*/
private function registerInjector(string $identifier, Closure $injector) : void{
$this->injectors[$identifier] = $injector;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -16,8 +16,7 @@ interface WorldDatabase{
*
* @param WorldInstance $world
* @param Player $player
* @param Closure $onLoad
* @phpstan-param Closure(PlayerWorldData $data) : void $onLoad
* @param Closure(PlayerWorldData $data) : void $onLoad
*/
public function load(WorldInstance $world, Player $player, Closure $onLoad) : void;

@@ -28,8 +27,7 @@ public function load(WorldInstance $world, Player $player, Closure $onLoad) : vo
* @param Player $player
* @param PlayerWorldData $data
* @param int $cause what triggered the save.
* @param Closure $onSave
* @phpstan-param Closure(bool $success) : void $onSave
* @param Closure(bool $success) : void $onSave
*/
public function save(WorldInstance $world, Player $player, PlayerWorldData $data, int $cause, Closure $onSave) : void;