From 6ac99c334f8a220ca9b1063696f9dccde35a8b91 Mon Sep 17 00:00:00 2001 From: Beu Date: Sat, 27 Mar 2021 22:59:29 +0100 Subject: [PATCH] Add ChatAdminColorer plugin --- Beu/ChatAdminColorer.php | 130 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 Beu/ChatAdminColorer.php diff --git a/Beu/ChatAdminColorer.php b/Beu/ChatAdminColorer.php new file mode 100644 index 0000000..a224801 --- /dev/null +++ b/Beu/ChatAdminColorer.php @@ -0,0 +1,130 @@ +maniaControl = $maniaControl; + $this->maniaControl->getCallbackManager()->registerCallbackListener('ManiaPlanet.PlayerChat', $this, 'handlePlayerChat'); + try { + $this->maniaControl->getClient()->chatEnableManualRouting(); + $this->enabled = true; + } catch (\Exception $ex) { + $this->enabled = false; + echo "error! \n"; + } + } + + public function handlePlayerChat($callback) { + $args = $callback[1]; + $this->onPlayerChatter($args[0], $args[1], $args[2]); + } + + private function onPlayerChatter($playerUid, $login, $text) { + if ($playerUid != 0 && substr($text, 0, 1) != "/" && $this->enabled) { + $source_player = $this->maniaControl->getPlayerManager()->getPlayer($login); + if ($source_player == null) { + return; + } + $nick = $source_player->nickname; + $authLevel = $source_player->authLevel; + + if ($authLevel > 0) { + $color = $this->maniaControl->getColorManager()->getColorByLevel($authLevel); + } + + try { + // change text color, if admin is defined at admingroups + if ($authLevel > 0) { + $this->chatSendServerMessage('[$<' . $color . $nick . '$>] ' . $text); + } else { + $this->chatSendServerMessage('[' . $nick . '] ' . $text); + } + } catch (\Exception $e) { + echo "error while sending chat message to $login: " . $e->getMessage() . "\n"; + } + } + } + + public function chatSendServerMessage($text) { + $this->maniaControl->getClient()->chatSendServerMessage($text); + } + + /** + * Unload the plugin and its Resources + */ + public function unload() { + $this->maniaControl->getClient()->chatEnableManualRouting(false); + $this->maniaControl->getCallbackManager()->unregisterCallbackListening('ManiaPlanet.OnPlayerChat', $this); + + } +}