Skip to content

Commit

Permalink
remove PlayerSession
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackjack200 committed Nov 22, 2024
1 parent 4fae1b8 commit ca88ad0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 39 deletions.
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: WorldsX
version: 1.2.2
version: 1.2.3
api: 5.0.0
main: blackjack200\worldsx\WorldsX
author: Blackjack200
Expand Down
23 changes: 0 additions & 23 deletions src/blackjack200/worldsx/session/PlayerSession.php

This file was deleted.

19 changes: 4 additions & 15 deletions src/blackjack200/worldsx/session/WorldsXListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace blackjack200\worldsx\session;

use blackjack200\worldsx\world\GameRuleUtil;
use blackjack200\worldsx\world\types\DefaultGameRules;
use pocketmine\block\VanillaBlocks;
use pocketmine\entity\effect\VanillaEffects;
Expand All @@ -25,34 +26,22 @@
use pocketmine\player\Player;
use pocketmine\Server;
use pocketmine\world\World;
use WeakMap;

class WorldsXListener implements Listener {
/** @var WeakMap<PlayerSession> */
private WeakMap $sessions;

public function __construct() {
$this->sessions = new WeakMap();
foreach (Server::getInstance()->getWorldManager()->getWorlds() as $world) {
WorldGameRules::setupGameRules($world);
}
}

protected function getSession(Player $e) : ?PlayerSession {
return $this->sessions[$e] ?? null;
}

public function syncGameRules(World $world) : void {
foreach ($world->getPlayers() as $player) {
$this->getSession($player)?->sendGameRules();
GameRuleUtil::send($player);
}
}

public function onPlayerJoin(PlayerJoinEvent $e) : void {
$p = $e->getPlayer();
$s = new PlayerSession($p);
$this->sessions[$p] = $s;
$s->sendGameRules();
GameRuleUtil::send($e->getPlayer());
}

public function onWorldLoad(WorldLoadEvent $event) : void {
Expand All @@ -66,7 +55,7 @@ public function onWorldUnload(WorldUnloadEvent $event) : void {
public function onPlayerTeleport(EntityTeleportEvent $event) : void {
$p = $event->getEntity();
if ($p instanceof Player && $event->getFrom()->getWorld() !== $event->getTo()->getWorld()) {
$this->getSession($p)?->sendGameRules(WorldGameRules::mustGetGameRuleCollection($event->getTo()->getWorld()));
GameRuleUtil::send($p, WorldGameRules::mustGetGameRuleCollection($event->getTo()->getWorld()));
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/blackjack200/worldsx/world/GameRuleUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace blackjack200\worldsx\world;

use blackjack200\worldsx\session\WorldGameRules;
use blackjack200\worldsx\world\types\GameRuleMapping;
use pocketmine\network\mcpe\protocol\GameRulesChangedPacket;
use pocketmine\player\Player;
use pocketmine\world\format\io\data\BaseNbtWorldData;

class GameRuleUtil {
Expand All @@ -20,4 +23,12 @@ public static function save(BaseNbtWorldData $data, GameRuleCollection $rules) :
$data->getCompoundTag()->setTag('GameRules', $rules->toCompoundTag());
$data->save();
}

public static function send(Player $player, ?GameRuleCollection $rules = null) : void {
if ($rules === null) {
$rules = WorldGameRules::mustGetGameRuleCollection($player->getWorld());
}
$pk = GameRulesChangedPacket::create($rules->toGameRules());
$player->getNetworkSession()->sendDataPacket($pk);
}
}

0 comments on commit ca88ad0

Please sign in to comment.