Skip to content

Commit

Permalink
Implement a Tag System
Browse files Browse the repository at this point in the history
Close #8 #22 #23
  • Loading branch information
IvanCraft623 committed Aug 7, 2022
1 parent e92c447 commit 5df0971
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 29 deletions.
24 changes: 24 additions & 0 deletions resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,28 @@ Hierarchy:
- Owner #Highest rank
- Admin
- Guest #Lower rank

#Tags:
# Tags can be used both in nametag and chat. (These are not related to ScoreHud)
# Defaults are:
# {name} - The user's name
# {nametag_ranks_prefix} - Set of all rank prefixes in nametag
# {nametag_highest-rank_prefix} - Highest rank prefix in nametag
# {nametag_name-color} - The color of the name in nametag
# {chat_ranks_prefix} - Set of all rank prefixes in chat
# {chat_highest-rank_prefix} - Highest rank prefix in chat
# {chat_name-color} - The color of the name in chat
# {chat_format} - Stuff that goes before the message (Can be a colon)

#Other plugins can register custom tags

#Configure users's nametag
nametag:
enabled: true
format: "{nametag_ranks_prefix}{nametag_name-color}{name}"

#Configure users's chat
chat:
enabled: true
format: "{chat_ranks_prefix}{chat_name-color}{name}{chat_format}{message}"
...
8 changes: 5 additions & 3 deletions src/IvanCraft623/RankSystem/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ public function onPreLogin(PlayerPreLoginEvent $event) : void {
* @ignoreCancelled
*/
public function onChat(PlayerChatEvent $event) : void {
$player = $event->getPlayer();
$session = $this->plugin->getSessionManager()->get($player);
$event->setFormat($session->getChatFormat().$event->getMessage());
if ($this->plugin->getConfig()->getNested("chat.enabled", true)) {
$player = $event->getPlayer();
$session = $this->plugin->getSessionManager()->get($player);
$event->setFormat(str_replace("{message}", $event->getMessage(), $session->getChatFormat()));
}
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/IvanCraft623/RankSystem/RankSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@

use IvanCraft623\RankSystem\command\RankSystemCommand;
use IvanCraft623\RankSystem\form\FormManager;
use IvanCraft623\RankSystem\session\SessionManager;
use IvanCraft623\RankSystem\rank\RankManager;
use IvanCraft623\RankSystem\session\SessionManager;
use IvanCraft623\RankSystem\tag\TagManager;
use IvanCraft623\RankSystem\task\UpdateTask;
use IvanCraft623\RankSystem\migrator\LegacyRankSystem;
use IvanCraft623\RankSystem\migrator\Migrator;
Expand Down Expand Up @@ -72,6 +73,7 @@ public function onLoad() : void {
$this->saveResources();
$this->loadTranslations();
$this->getRankManager()->load();
$this->getTagManager()->registerDefaults();
}

public function onEnable() : void {
Expand All @@ -98,6 +100,10 @@ public function getSessionManager() : SessionManager {
return SessionManager::getInstance();
}

public function getTagManager() : TagManager {
return TagManager::getInstance();
}

public function getFormManager() : FormManager {
return FormManager::getInstance();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v
$sender->sendMessage(
"§r§f" . $translator->translate($sender, "text.user") . ": §a" . $session->getName() . "\n\n" .
"§r§f" . $translator->translate($sender, "text.nametag") . ": " . $session->getNameTagFormat() . "\n" .
"§r§f" . $translator->translate($sender, "text.chat") . ": " . $session->getChatFormat() . $translator->translate($sender, "text.hello_world") . "\n\n" .
"§r§f" . $translator->translate($sender, "text.chat") . ": " . str_replace("{message}", $translator->translate($sender, "text.hello_world"), $session->getChatFormat()) . "\n\n" .
"§r§f" . $translator->translate($sender, "text.ranks") . ": " . $ranks . "\n" .
"§r§f" . $translator->translate($sender, "text.permissions") . ": §a" . $permissions
);
Expand Down
4 changes: 2 additions & 2 deletions src/IvanCraft623/RankSystem/form/RankInfoForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public function send(Player $player, Rank $rank) : void {
$permissions .= "\n §e - " . $permission;
}
$form->setContent(
"§r§f" . $translator->translate($player, "text.rank") . ": §a" . $args["rank"]->getName() . "\n\n" .
"§r§f" . $translator->translate($player, "text.rank") . ": §a" . $rank->getName() . "\n\n" .
"§r§f" . $translator->translate($player, "text.nametag") . ": " . $nametag["prefix"] . $nametag["nameColor"] . "Steve" . "\n" .
"§r§f" . $translator->translate($player, "text.chat") . ": " . $chat["prefix"] . $chat["nameColor"] . $translator->translate($player, "text.steve") . $chat["chatFormat"] . $translator->translate($player, "text.hello_world") . "\n" .
"§r§f" . $translator->translate($player, "text.inheritance") . ": §a" . Utils::ranks2string($args["rank"]->getInheritance()) . "\n" .
"§r§f" . $translator->translate($player, "text.inheritance") . ": §a" . Utils::ranks2string($rank->getInheritance()) . "\n" .
"§r§f" . $translator->translate($player, "text.permissions") . ": §a" . $permissions
);
$form->sendToPlayer($player);
Expand Down
2 changes: 1 addition & 1 deletion src/IvanCraft623/RankSystem/form/UserInfoForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ function (bool $remove) use ($player, $session, $permission) {
$form->setContent(
"§r§f" . $this->translator->translate($player, "text.user") . ": §a" . $session->getName() . "\n\n" .
"§r§f" . $this->translator->translate($player, "text.nametag") . ": " . $session->getNameTagFormat() . "\n" .
"§r§f" . $this->translator->translate($player, "text.chat") . ": " . $session->getChatFormat() . $this->translator->translate($player, "text.hello_world") . "\n\n" .
"§r§f" . $this->translator->translate($player, "text.chat") . ": " . str_replace("{message}", $this->translator->translate($player, "text.hello_world"), $session->getChatFormat()) . "\n\n" .
"§r§f" . $this->translator->translate($player, "text.ranks") . ": " . $ranks . "\n" .
"§r§f" . $this->translator->translate($player, "text.permissions") . ": §a" . $permissions
);
Expand Down
39 changes: 18 additions & 21 deletions src/IvanCraft623/RankSystem/session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,30 +172,20 @@ public function setPlayer(Player $player) : void {
$this->player = $player;
}

public function getNameTagPrefix() : string {
$prefix = "";
foreach ($this->getRanks() as $rank) {
$prefix .= $rank->getNameTagFormat()["prefix"];
}
return $prefix;
}

public function getChatPrefix() : string {
$prefix = "";
foreach ($this->getRanks() as $rank) {
$prefix .= $rank->getChatFormat()["prefix"];
}
return $prefix;
}

public function getNameTagFormat() : string {
$highestFormat = $this->getHighestRank()->getNameTagFormat();
return $this->getNameTagPrefix().$highestFormat["nameColor"].$this->name;
$format = $this->plugin->getConfig()->getNested("nametag.format", "{nametag_ranks_prefix}{nametag_name-color}{name}");
foreach ($this->plugin->getTagManager()->getTags() as $tag) {
$format = str_replace($tag->getId(), $tag->getValue($this), $format);
}
return $format;
}

public function getChatFormat() : string {
$highestFormat = $this->getHighestRank()->getChatFormat();
return $this->getChatPrefix().$highestFormat["nameColor"].$this->name.$highestFormat["chatFormat"];
$format = $this->plugin->getConfig()->getNested("nametag.format", "{chat_ranks_prefix}{chat_name-color}{name}{chat_format}{message}");
foreach ($this->plugin->getTagManager()->getTags() as $tag) {
$format = str_replace($tag->getId(), $tag->getValue($this), $format);
}
return $format;
}

/**
Expand Down Expand Up @@ -414,7 +404,7 @@ public function updateRanks() {
$player = $this->getPlayer();
if ($player !== null) {
$this->updatePermissions();
$player->setNameTag($this->getNameTagFormat());
$this->updateNameTag();
Utils::updateScoreTags($this);
}
}
Expand All @@ -431,4 +421,11 @@ public function updatePermissions() {
}
}
}

public function updateNameTag() : void {
$player = $this->getPlayer();
if ($player !== null && $this->plugin->getConfig()->getNested("nametag.enabled", true)) {
$player->setNameTag($this->getNameTagFormat());
}
}
}
43 changes: 43 additions & 0 deletions src/IvanCraft623/RankSystem/tag/Tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

#Plugin By:

/*
8888888 .d8888b. .d888 888 .d8888b. .d8888b. .d8888b.
888 d88P Y88b d88P" 888 d88P Y88b d88P Y88b d88P Y88b
888 888 888 888 888 888 888 .d88P
888 888 888 8888b. 88888b. 888 888d888 8888b. 888888 888888 888d888b. .d88P 8888"
888 888 888 "88b 888 "88b 888 888P" "88b 888 888 888P "Y88b .od888P" "Y8b.
888 Y88 88P .d888888 888 888 888 888 888 .d888888 888 888 888 888 d88P" 888 888
888 Y8bd8P 888 888 888 888 Y88b d88P 888 888 888 888 Y88b. Y88b d88P 888" Y88b d88P
8888888 Y88P "Y888888 888 888 "Y8888P" 888 "Y888888 888 "Y888 "Y8888P" 888888888 "Y8888P"
*/

declare(strict_types=1);

namespace IvanCraft623\RankSystem\tag;

use IvanCraft623\RankSystem\session\Session;

use pocketmine\utils\Utils;

use Closure;

final class Tag {

public function __construct(protected string $name, protected Closure $valueCallback) {
Utils::validateCallableSignature(static function(Session $user) : string{ return ""; }, $valueCallback);
}

public function getId() : string {
return "{" . $this->name . "}";
}

public function getName() : string {
return $this->name;
}

public function getValue(Session $user) : string {
return ($this->valueCallback)($user);
}
}
72 changes: 72 additions & 0 deletions src/IvanCraft623/RankSystem/tag/TagManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

#Plugin By:

/*
8888888 .d8888b. .d888 888 .d8888b. .d8888b. .d8888b.
888 d88P Y88b d88P" 888 d88P Y88b d88P Y88b d88P Y88b
888 888 888 888 888 888 888 .d88P
888 888 888 8888b. 88888b. 888 888d888 8888b. 888888 888888 888d888b. .d88P 8888"
888 888 888 "88b 888 "88b 888 888P" "88b 888 888 888P "Y88b .od888P" "Y8b.
888 Y88 88P .d888888 888 888 888 888 888 .d888888 888 888 888 888 d88P" 888 888
888 Y8bd8P 888 888 888 888 Y88b d88P 888 888 888 888 Y88b. Y88b d88P 888" Y88b d88P
8888888 Y88P "Y888888 888 888 "Y8888P" 888 "Y888888 888 "Y888 "Y8888P" 888888888 "Y8888P"
*/

declare(strict_types=1);

namespace IvanCraft623\RankSystem\tag;

use IvanCraft623\RankSystem\rank\Rank;
use IvanCraft623\RankSystem\session\Session;

use pocketmine\utils\SingletonTrait;

final class TagManager {
use SingletonTrait;

/** @var Tag[] */
private array $tags = [];

public function registerTag(Tag $tag) : void {
$this->tags[$tag->getId()] = $tag;
}

public function getTag(string $tagId) : ?Tag {
return $this->tags[$tagId] ?? null;
}

public function getTags() : array {
return $this->tags;
}

/**
* @internal
*/
public function registerDefaults() : void {
$this->registerTag(new Tag("name", static function(Session $user) : string {
return $user->getName();
}));
$this->registerTag(new Tag("nametag_ranks_prefix", static function(Session $user) : string {
return implode("", array_map(fn(Rank $rank) => $rank->getNameTagFormat()["prefix"], $user->getRanks()));
}));
$this->registerTag(new Tag("nametag_highest-rank_prefix", static function(Session $user) : string {
return $user->getHighestRank()->getNameTagFormat()["prefix"];
}));
$this->registerTag(new Tag("nametag_name-color", static function(Session $user) : string {
return $user->getHighestRank()->getNameTagFormat()["nameColor"];
}));
$this->registerTag(new Tag("chat_ranks_prefix", static function(Session $user) : string {
return implode("", array_map(fn(Rank $rank) => $rank->getChatFormat()["prefix"], $user->getRanks()));
}));
$this->registerTag(new Tag("chat_highest-rank_prefix", static function(Session $user) : string {
return $user->getHighestRank()->getChatFormat()["prefix"];
}));
$this->registerTag(new Tag("chat_name-color", static function(Session $user) : string {
return $user->getHighestRank()->getChatFormat()["nameColor"];
}));
$this->registerTag(new Tag("chat_format", static function(Session $user) : string {
return $user->getHighestRank()->getChatFormat()["chatFormat"];
}));
}
}

0 comments on commit 5df0971

Please sign in to comment.