Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PlayedTimeScore #28

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .poggit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ build-by-default: true
branches:
- main
projects:
PlayedTimeScore:
path: PlayedTimeScore
lint:
phpstan: false
EcoAPIScore:
path: EcoAPIScore
lint:
Expand Down
6 changes: 6 additions & 0 deletions PlayedTimeScore/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### Available Tags:

| Tag | Description |
|:--:|:--:|
|`{ptscore.total_time}`|Totally played time of the player|
|`{ptscore.session_time}`|Played time while the player-session|
8 changes: 8 additions & 0 deletions PlayedTimeScore/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: PlayedTimeScore
main: supercrafter333\PlayedTimeScore\Main
src-namespace-prefix: supercrafter333\PlayedTimeScore
api: 4.0.0
version: 1.1.0
author: supercrafter333
description: A plugin implementing some basic tags for ScoreHud.
depend: ["PlayedTime", "ScoreHud"]
7 changes: 7 additions & 0 deletions PlayedTimeScore/resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dateformat-letters:
year: 'y'
month: 'mo'
day: 'd'
hour: 'h'
minute: 'm'
second: 's'
28 changes: 28 additions & 0 deletions PlayedTimeScore/src/Main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace supercrafter333\PlayedTimeScore;

use pocketmine\plugin\PluginBase;
use pocketmine\utils\SingletonTrait;
use supercrafter333\PlayedTimeScore\listeners\EventListener;
use supercrafter333\PlayedTimeScore\listeners\TagResolveListener;

class Main extends PluginBase
{
use SingletonTrait;

protected function onLoad(): void
{
self::setInstance($this);
}

protected function onEnable(): void
{
$this->saveResource("config.yml");

$this->getServer()->getPluginManager()->registerEvents(new EventListener(), $this);
$this->getServer()->getPluginManager()->registerEvents(new TagResolveListener($this), $this);

$this->getScheduler()->scheduleRepeatingTask(new ScorehudUpdateTask(), 20);
}
}
16 changes: 16 additions & 0 deletions PlayedTimeScore/src/ScorehudUpdateTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace supercrafter333\PlayedTimeScore;

use pocketmine\scheduler\Task;
use supercrafter333\PlayedTimeScore\listeners\EventListener;

class ScorehudUpdateTask extends Task
{

public function onRun(): void
{
foreach (Main::getInstance()->getServer()->getOnlinePlayers() as $player)
(new EventListener())->sendUpdate($player);
}
}
54 changes: 54 additions & 0 deletions PlayedTimeScore/src/listeners/EventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace supercrafter333\PlayedTimeScore\listeners;

use DateInterval;
use Ifera\ScoreHud\event\PlayerTagsUpdateEvent;
use Ifera\ScoreHud\scoreboard\ScoreTag;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerChatEvent;
use pocketmine\event\player\PlayerJoinEvent;
use pocketmine\player\Player;
use supercrafter333\PlayedTime\PlayedTimeLoader;
use supercrafter333\PlayedTimeScore\Main;

class EventListener implements Listener
{

public function onJoin(PlayerJoinEvent $event)
{
$player = $event->getPlayer();

if (is_null($player) || !$player->isOnline()) {
return;
}

$this->sendUpdate($player);
}

// no better way to detect when the suffix or prefix of a player changes
public function onPlayerChat(PlayerChatEvent $event)
{
$this->sendUpdate($event->getPlayer());
}

public function sendUpdate(Player $player): void
{
$time = function (DateInterval|null $dt): string {
if ($dt === null) return "§cNOT FOUND";
$str = "";
$cfg = Main::getInstance()->getConfig()->get("dateformat-letters", []);
if ($dt->y > 0) $str .= "§e" . $dt->y . $cfg["year"] . "§7, ";
if ($dt->m > 0) $str .= "§e" . $dt->m . $cfg["month"] . "§7, ";
if ($dt->d > 0) $str .= "§e" . $dt->d . $cfg["day"] . "§7, ";
if ($dt->h > 0) $str .= "§e" . $dt->h . $cfg["hour"] . "§7, ";
if ($dt->i > 0) $str .= "§e" . $dt->i . $cfg["minute"] . "§7, ";
if ($dt->s > 0) $str .= "§e" . $dt->s . $cfg["second"];
return $str;
};
(new PlayerTagsUpdateEvent($player, [
new ScoreTag("ptscore.total_time", $time(PlayedTimeLoader::getInstance()->getPlayedTimeManager()->getTotalTime($player))),
new ScoreTag("ptscore.session_time", $time(PlayedTimeLoader::getInstance()->getPlayedTimeManager()->getSessionTime($player)))
]))->call();
}
}
52 changes: 52 additions & 0 deletions PlayedTimeScore/src/listeners/TagResolveListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace supercrafter333\PlayedTimeScore\listeners;

use DateInterval;
use Ifera\ScoreHud\event\TagsResolveEvent;
use pocketmine\event\Listener;
use supercrafter333\PlayedTime\PlayedTimeLoader;
use supercrafter333\PlayedTimeScore\Main;

class TagResolveListener implements Listener
{

public function __construct(private Main $plugin) {}

public function onTagResolve(TagsResolveEvent $ev)
{
$player = $ev->getPlayer();
$tag = $ev->getTag();
$tags = explode('.', $tag->getName(), 2);
$value = "";

if ($tags[0] !== 'ptscore' || count($tags) < 2) {
return;
}

$time = function (DateInterval|null $dt): string {
if ($dt === null) return "§cNOT FOUND";
$str = "";
$cfg = Main::getInstance()->getConfig()->get("dateformat-letters", []);
if ($dt->y > 0) $str .= "§e" . $dt->y . $cfg["year"] . "§7, ";
if ($dt->m > 0) $str .= "§e" . $dt->m . $cfg["month"] . "§7, ";
if ($dt->d > 0) $str .= "§e" . $dt->d . $cfg["day"] . "§7, ";
if ($dt->h > 0) $str .= "§e" . $dt->h . $cfg["hour"] . "§7, ";
if ($dt->i > 0) $str .= "§e" . $dt->i . $cfg["minute"] . "§7, ";
if ($dt->s > 0) $str .= "§e" . $dt->s . $cfg["second"];
return $str;
};

switch ($tags[1]) {
case "total_time":
$value = $time(PlayedTimeLoader::getInstance()->getPlayedTimeManager()->getTotalTime($player));
break;

case "session_time":
$value = $time(PlayedTimeLoader::getInstance()->getPlayedTimeManager()->getSessionTime($player));
break;
}

$tag->setValue($value);
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ A repository containing plugins implementing tags for ScoreHud.
|CombatScore|[CombatLogger](https://github.com/JackNoordhuis/PocketMine-Plugins/tree/master/CombatLogger)|[Download](https://poggit.pmmp.io/ci/Ifera/ScoreHudX/CombatScore)|
|FactionsProScore|[FactionsPro](https://github.com/poggit-orphanage/FactionsPro)|[Download](https://poggit.pmmp.io/ci/Ifera/ScoreHudX/FactionsProScore)|
|PurePermsScore|[PurePerms](https://github.com/poggit-orphanage/PurePerms)|[Download](https://poggit.pmmp.io/ci/Ifera/ScoreHudX/PurePermsScore)|
|PlayedTimeScore|[PlayedTime](https://github.com/supercrafter333/PlayedTime)|[Download](https://poggit.pmmp.io/ci/Ifera/ScoreHudX/PlayedTimeScore)|

## More plugins

Expand Down