-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa5744b
commit 0ab533f
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace antbag\JoinCount; | ||
|
||
use pocketmine\event\Listener; | ||
use pocketmine\event\player\PlayerJoinEvent; | ||
use pocketmine\Server; | ||
use antbag\JoinCount\Main; | ||
|
||
|
||
|
||
class PlayerJoinListener implements Listener { | ||
|
||
private $dataFile; // File to store player information | ||
|
||
public function __construct(string $dataFilePath) { | ||
$this->dataFile = $dataFilePath; | ||
} | ||
|
||
public function onPlayerJoin(PlayerJoinEvent $event) { | ||
$player = $event->getPlayer(); | ||
$playerName = $player->getName(); | ||
|
||
$data = $this->loadData(); // Load existing player data | ||
|
||
if (!isset($data[$playerName])) { | ||
$data[$playerName] = true; // Store the player's join status | ||
|
||
$this->saveData($data); // Save updated player data | ||
|
||
$totalPlayers = count($data); | ||
|
||
$welcomeMessage = "§8Welcome,§c $playerName! §8You are the #§8 $totalPlayers §8player to join our server!"; | ||
Server::getInstance()->broadcastMessage($welcomeMessage); | ||
|
||
|
||
} | ||
} | ||
|
||
private function loadData(): array { | ||
if (!file_exists($this->dataFile)) { | ||
return []; | ||
} | ||
|
||
$data = file_get_contents($this->dataFile); | ||
return json_decode($data, true) ?? []; | ||
} | ||
|
||
private function saveData(array $data) { | ||
$encodedData = json_encode($data); | ||
file_put_contents($this->dataFile, $encodedData); | ||
} | ||
} |