From e8a907d89e37bf6eeab8e852c8439ae4899cd114 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 31 Jul 2013 11:16:11 +0200 Subject: [PATCH] Implemented the actual tab completion for commands and playernames. Each command is reported only if the player has the permission to use it. --- source/ClientHandle.cpp | 2 +- source/PluginManager.cpp | 22 +++++++++++++++------- source/PluginManager.h | 6 ++++-- source/World.cpp | 16 ++++++++++------ 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/source/ClientHandle.cpp b/source/ClientHandle.cpp index 75f29790c..45b604b65 100644 --- a/source/ClientHandle.cpp +++ b/source/ClientHandle.cpp @@ -1239,7 +1239,7 @@ void cClientHandle::HandleTabCompletion(const AString & a_Text) { AStringVector Results; m_Player->GetWorld()->TabCompleteUserName(a_Text, Results); - cRoot::Get()->GetPluginManager()->TabCompleteCommand(a_Text, Results); + cRoot::Get()->GetPluginManager()->TabCompleteCommand(a_Text, Results, m_Player); if (Results.empty()) { return; diff --git a/source/PluginManager.cpp b/source/PluginManager.cpp index 37032f6c3..043390cbc 100644 --- a/source/PluginManager.cpp +++ b/source/PluginManager.cpp @@ -1335,14 +1335,22 @@ bool cPluginManager::ExecuteConsoleCommand(const AStringVector & a_Split, cComma -void cPluginManager::TabCompleteCommand(const AString & a_Text, AStringVector & a_Results) +void cPluginManager::TabCompleteCommand(const AString & a_Text, AStringVector & a_Results, cPlayer * a_Player) { - // TODO - // DEBUG: - LOGWARNING("%s: Not implemented yet!", __FUNCTION__); - a_Results.push_back(a_Text + "_plgmgr1"); - a_Results.push_back(a_Text + "_plgmgr3"); - a_Results.push_back(a_Text + "_plgmgr2"); + for (CommandMap::iterator itr = m_Commands.begin(), end = m_Commands.end(); itr != end; ++itr) + { + if (NoCaseCompare(itr->first.substr(0, a_Text.length()), a_Text) != 0) + { + // Command name doesn't match + continue; + } + if ((a_Player != NULL) && !a_Player->HasPermission(itr->second.m_Permission)) + { + // Player doesn't have permission for the command + continue; + } + a_Results.push_back(itr->first); + } } diff --git a/source/PluginManager.h b/source/PluginManager.h index 150132f9f..6bdf87fc7 100644 --- a/source/PluginManager.h +++ b/source/PluginManager.h @@ -197,8 +197,10 @@ public: // tolua_export /// Executes the command split into a_Split, as if it was given on the console. Returns true if executed. Output is sent to the a_Output callback bool ExecuteConsoleCommand(const AStringVector & a_Split, cCommandOutputCallback & a_Output); - /// Appends all commands beginning with a_Text (case-insensitive) into a_Results - void TabCompleteCommand(const AString & a_Text, AStringVector & a_Results); + /** Appends all commands beginning with a_Text (case-insensitive) into a_Results. + If a_Player is not NULL, only commands for which the player has permissions are added. + */ + void TabCompleteCommand(const AString & a_Text, AStringVector & a_Results, cPlayer * a_Player); private: friend class cRoot; diff --git a/source/World.cpp b/source/World.cpp index da29565cb..880f6e5b2 100644 --- a/source/World.cpp +++ b/source/World.cpp @@ -2366,12 +2366,16 @@ int cWorld::SpawnMob(double a_PosX, double a_PosY, double a_PosZ, int a_EntityTy void cWorld::TabCompleteUserName(const AString & a_Text, AStringVector & a_Results) { - // TODO - // DEBUG: - LOGWARNING("%s: Not implemented yet!", __FUNCTION__); - a_Results.push_back(a_Text + "_world1"); - a_Results.push_back(a_Text + "_world3"); - a_Results.push_back(a_Text + "_world2"); + cCSLock Lock(m_CSPlayers); + for (cPlayerList::iterator itr = m_Players.begin(), end = m_Players.end(); itr != end; ++itr) + { + if (NoCaseCompare((*itr)->GetName().substr(0, a_Text.length()), a_Text) != 0) + { + // Player name doesn't match + continue; + } + a_Results.push_back((*itr)->GetName()); + } }