From 0ad0d06f852e07f35ea175376c4c19c8faf9152f Mon Sep 17 00:00:00 2001 From: Beu Date: Mon, 14 Aug 2023 17:11:09 +0200 Subject: [PATCH] Add a setting to disable ManiaExchange --- core/ManiaExchange/ManiaExchangeList.php | 6 ++++- core/ManiaExchange/ManiaExchangeManager.php | 23 ++++++++++++++++++- core/ManiaExchange/ManiaExchangeMapSearch.php | 2 ++ core/Maps/MapCommands.php | 19 ++++++++++----- core/Maps/MapManager.php | 20 ++++++++++++++++ 5 files changed, 62 insertions(+), 8 deletions(-) diff --git a/core/ManiaExchange/ManiaExchangeList.php b/core/ManiaExchange/ManiaExchangeList.php index b293bf9e..84340c9f 100644 --- a/core/ManiaExchange/ManiaExchangeList.php +++ b/core/ManiaExchange/ManiaExchangeList.php @@ -71,6 +71,7 @@ class ManiaExchangeList implements CallbackListener, ManialinkPageAnswerListener * @param array $callback */ public function handleManialinkPageAnswer(array $callback) { + if (!$this->maniaControl->getMapManager()->getMXManager()->getStatus()) return; $actionId = $callback[1][2]; $actionArray = explode('.', $actionId); if (count($actionArray) <= 2) { @@ -101,6 +102,7 @@ class ManiaExchangeList implements CallbackListener, ManialinkPageAnswerListener * @param Player $player */ public function showListCommand(array $chatCallback, Player $player) { + if (!$this->maniaControl->getMapManager()->getMXManager()->getStatus()) return; $this->mapListShown[$player->login] = true; $params = explode(' ', $chatCallback[1][2]); $searchString = ''; @@ -141,6 +143,7 @@ class ManiaExchangeList implements CallbackListener, ManialinkPageAnswerListener * @param string $searchString */ private function getMXMapsAndShowList(Player $player, $author = '', $environment = '', $searchString = '') { + if (!$this->maniaControl->getMapManager()->getMXManager()->getStatus()) return; //TODO do more clean solution if($environment == ""){ $titleId = $this->maniaControl->getServer()->titleId; @@ -191,6 +194,7 @@ class ManiaExchangeList implements CallbackListener, ManialinkPageAnswerListener * @internal param array $chatCallback */ private function showManiaExchangeList(array $maps, Player $player) { + if (!$this->maniaControl->getMapManager()->getMXManager()->getStatus()) return; // Start offsets $width = $this->maniaControl->getManialinkManager()->getStyleManager()->getListWidgetsWidth(); $height = $this->maniaControl->getManialinkManager()->getStyleManager()->getListWidgetsHeight(); @@ -340,6 +344,7 @@ class ManiaExchangeList implements CallbackListener, ManialinkPageAnswerListener * @param $openedWidget */ public function handleWidgetOpened(Player $player, $openedWidget) { + if (!$this->maniaControl->getMapManager()->getMXManager()->getStatus()) return; //unset when another main widget got opened if ($openedWidget !== 'ManiaExchangeList') { unset($this->mapListShown[$player->login]); @@ -355,5 +360,4 @@ class ManiaExchangeList implements CallbackListener, ManialinkPageAnswerListener public function closeWidget(Player $player) { unset($this->mapListShown[$player->login]); } - } diff --git a/core/ManiaExchange/ManiaExchangeManager.php b/core/ManiaExchange/ManiaExchangeManager.php index 94949078..27488623 100644 --- a/core/ManiaExchange/ManiaExchangeManager.php +++ b/core/ManiaExchange/ManiaExchangeManager.php @@ -54,6 +54,7 @@ class ManiaExchangeManager implements UsageInformationAble { */ /** @var ManiaControl $maniaControl */ private $maniaControl = null; + private $enabled = true; private $mxIdUidVector = array(); /** @@ -67,6 +68,22 @@ class ManiaExchangeManager implements UsageInformationAble { //$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_MX_KEY, ""); } + /** + * Set the status of the plugin + * + * @param bool $status + */ + public function setStatus(bool $status) { + $this->enabled = $status; + } + + /** + * Get the status of the plugin + */ + public function getStatus() { + return $this->enabled; + } + /** * Unset Map by Mx Id * @@ -84,6 +101,7 @@ class ManiaExchangeManager implements UsageInformationAble { * @param mixed $maps */ public function fetchManiaExchangeMapInformation($maps = null) { + if (!$this->enabled) return; if ($maps) { // Fetch Information for a single map $maps = array($maps); @@ -160,7 +178,7 @@ class ManiaExchangeManager implements UsageInformationAble { * @param string $string */ public function fetchMaplistByMixedUidIdString($string) { - + if (!$this->enabled) return; // For TM2020 if ($this->maniaControl->getServer()->titleId == "Trackmania") { // Get Title Prefix @@ -217,6 +235,7 @@ class ManiaExchangeManager implements UsageInformationAble { * @param array $mxMapInfos */ public function updateMapObjectsWithManiaExchangeIds(array $mxMapInfos) { + if (!$this->enabled) return; $mysqli = $this->maniaControl->getDatabase()->getMysqli(); // Save map data $saveMapQuery = "UPDATE `" . MapManager::TABLE_MAPS . "` @@ -269,6 +288,7 @@ class ManiaExchangeManager implements UsageInformationAble { * @param callable $function */ public function fetchMapInfo($mapId, callable $function) { + if (!$this->enabled) return; // For TM2020 if ($this->maniaControl->getServer()->titleId == "Trackmania") { // Get Title Prefix @@ -335,6 +355,7 @@ class ManiaExchangeManager implements UsageInformationAble { * @see \ManiaControl\ManiaExchange\ManiaExchangeMapSearch */ public function fetchMapsAsync(callable $function, $name = '', $author = '', $env = '', $maxMapsReturned = 100, $sortOrder = ManiaExchangeMapSearch::SEARCH_ORDER_UPDATED_NEWEST) { + if (!$this->enabled) return; $mapSearch = new ManiaExchangeMapSearch($this->maniaControl); $mapSearch->setMapName($name); $mapSearch->setAuthorName($author); diff --git a/core/ManiaExchange/ManiaExchangeMapSearch.php b/core/ManiaExchange/ManiaExchangeMapSearch.php index 451b1442..5008afe7 100644 --- a/core/ManiaExchange/ManiaExchangeMapSearch.php +++ b/core/ManiaExchange/ManiaExchangeMapSearch.php @@ -140,6 +140,8 @@ class ManiaExchangeMapSearch implements UsageInformationAble { * @param callable $function */ public function fetchMapsAsync(callable $function) { + if (!$this->maniaControl->getMapManager()->getMXManager()->getStatus()) return; + // compile search URL $parameters = ""; diff --git a/core/Maps/MapCommands.php b/core/Maps/MapCommands.php index b5d7c607..e52eb830 100644 --- a/core/Maps/MapCommands.php +++ b/core/Maps/MapCommands.php @@ -8,6 +8,7 @@ use FML\Controls\Quads\Quad_UIConstruction_Buttons; use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackManager; +use ManiaControl\Callbacks\Callbacks; use ManiaControl\Commands\CommandListener; use ManiaControl\Logger; use ManiaControl\ManiaControl; @@ -49,7 +50,6 @@ class MapCommands implements CommandListener, ManialinkPageAnswerListener, Callb */ public function __construct(ManiaControl $maniaControl) { $this->maniaControl = $maniaControl; - $this->initActionsMenuButtons(); // Admin commands $this->maniaControl->getCommandManager()->registerCommandListener(array('nextmap', 'next', 'skip'), $this, 'command_NextMap', true, 'Skips to the next map.'); @@ -68,6 +68,7 @@ class MapCommands implements CommandListener, ManialinkPageAnswerListener, Callb $this->maniaControl->getCommandManager()->registerCommandListener(array('xmaps', 'xlist'), $this, 'command_xList', false, 'Shows maps from ManiaExchange.'); // Callbacks + $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'handleAfterInit'); $this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer'); $this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener(self::ACTION_OPEN_XLIST, $this, 'command_xList'); $this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener(self::ACTION_OPEN_MAPLIST, $this, 'command_List'); @@ -75,16 +76,22 @@ class MapCommands implements CommandListener, ManialinkPageAnswerListener, Callb $this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener(self::ACTION_SKIP_MAP, $this, 'command_NextMap'); } + public function handleAfterInit() { + $this->initActionsMenuButtons(); + } + /** * Add all Actions Menu Buttons */ private function initActionsMenuButtons() { // Menu Open xList - $itemQuad = new Quad(); - $itemQuad->setImageUrl($this->maniaControl->getManialinkManager()->getIconManager()->getIcon(IconManager::MX_ICON)); - $itemQuad->setImageFocusUrl($this->maniaControl->getManialinkManager()->getIconManager()->getIcon(IconManager::MX_ICON_MOVER)); - $itemQuad->setAction(self::ACTION_OPEN_XLIST); - $this->maniaControl->getActionsMenu()->addPlayerMenuItem($itemQuad, 5, 'Open MX List'); + if ($this->maniaControl->getMapManager()->getMXManager()->getStatus()) { + $itemQuad = new Quad(); + $itemQuad->setImageUrl($this->maniaControl->getManialinkManager()->getIconManager()->getIcon(IconManager::MX_ICON)); + $itemQuad->setImageFocusUrl($this->maniaControl->getManialinkManager()->getIconManager()->getIcon(IconManager::MX_ICON_MOVER)); + $itemQuad->setAction(self::ACTION_OPEN_XLIST); + $this->maniaControl->getActionsMenu()->addPlayerMenuItem($itemQuad, 5, 'Open MX List'); + } // Menu Open List $itemQuad = new Quad_Icons64x64_1(); diff --git a/core/Maps/MapManager.php b/core/Maps/MapManager.php index 7557eab5..b8c5ad10 100644 --- a/core/Maps/MapManager.php +++ b/core/Maps/MapManager.php @@ -19,6 +19,8 @@ use ManiaControl\ManiaExchange\ManiaExchangeList; use ManiaControl\ManiaExchange\ManiaExchangeManager; use ManiaControl\ManiaExchange\MXMapInfo; use ManiaControl\Players\Player; +use ManiaControl\Settings\Setting; +use ManiaControl\Settings\SettingManager; use ManiaControl\Utils\Formatter; use Maniaplanet\DedicatedServer\InvalidArgumentException; use Maniaplanet\DedicatedServer\Xmlrpc\AlreadyInListException; @@ -56,6 +58,7 @@ class MapManager implements CallbackListener, CommunicationListener, UsageInform const SETTING_AUTOSAVE_MAPLIST = 'Autosave Maplist file'; const SETTING_MAPLIST_FILE = 'File to write Maplist in'; const SETTING_WRITE_OWN_MAPLIST_FILE = 'Write a own Maplist File for every Server called serverlogin.txt'; + const SETTING_ENABLE_MX = 'Enable MX features'; const SEARCH_BY_AUTHOR = 'Author'; const SEARCH_BY_MAP_NAME = 'Mapname'; @@ -116,6 +119,7 @@ class MapManager implements CallbackListener, CommunicationListener, UsageInform // Callbacks $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::ONINIT, $this, 'handleOnInit'); $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'handleAfterInit'); + $this->maniaControl->getCallbackManager()->registerCallbackListener(SettingManager::CB_SETTING_CHANGED, $this, 'updateSettings'); $this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_MAPLISTMODIFIED, $this, 'mapsModified'); // Permissions @@ -131,6 +135,9 @@ class MapManager implements CallbackListener, CommunicationListener, UsageInform $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_AUTOSAVE_MAPLIST, true); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_MAPLIST_FILE, "MatchSettings/tracklist.txt"); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WRITE_OWN_MAPLIST_FILE, false); + $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_ENABLE_MX, true); + + $this->mxManager->setStatus($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_ENABLE_MX, true)); //Initlaize Communication Listenings $this->initalizeCommunicationListenings(); @@ -213,6 +220,19 @@ class MapManager implements CallbackListener, CommunicationListener, UsageInform } } + /** + * Update Settings + * + * @param ?Setting $setting + */ + public function updateSettings(Setting $setting = null) { + if (!isset($setting) || !$setting->belongsToClass($this)) return; + + if ($setting->setting === self::SETTING_ENABLE_MX) { + $this->mxManager->setStatus($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_ENABLE_MX, true)); + } + } + /** * Update the Timestamp of a Map *