diff --git a/core/PlayerManager.cpp b/core/PlayerManager.cpp index 93d3e511..7555c446 100644 --- a/core/PlayerManager.cpp +++ b/core/PlayerManager.cpp @@ -212,6 +212,9 @@ void PlayerManager::OnSourceModAllInitialized() SH_ADD_HOOK(ConCommand, Dispatch, pCmd, SH_STATIC(CmdMaxplayersCallback), true); maxplayersCmd = pCmd; } + + gameevents->AddListener(this, "player_connect", true); + gameevents->AddListener(this, "player_disconnect", true); } void PlayerManager::OnSourceModShutdown() @@ -259,6 +262,8 @@ void PlayerManager::OnSourceModShutdown() { SH_REMOVE_HOOK(ConCommand, Dispatch, maxplayersCmd, SH_STATIC(CmdMaxplayersCallback), true); } + + gameevents->RemoveListener(this); } ConfigResult PlayerManager::OnSourceModConfigChanged(const char *key, @@ -1424,6 +1429,11 @@ int PlayerManager::GetNumPlayers() return m_PlayerCount; } +int PlayerManager::GetNumClients() +{ + return m_ClientCount; +} + int PlayerManager::GetClientOfUserId(int userid) { if (userid < 0 || userid > USHRT_MAX) @@ -2019,6 +2029,27 @@ bool PlayerManager::HandleConVarQuery(QueryCvarCookie_t cookie, int client, EQue } #endif +/* IGameEventListener2::FireGameEvent */ +void PlayerManager::FireGameEvent(IGameEvent *pEvent) +{ + const char *name = pEvent->GetName(); + + if (strcmp(name, "player_connect") == 0) + { + const int client = pEvent->GetInt("index") + 1; + const int userid = pEvent->GetInt("userid"); + + m_ClientCount++; + } + else if (strcmp(name, "player_disconnect") == 0) + { + const int userid = pEvent->GetInt("userid"); + const int client = m_UserIdLookUp[userid]; + + m_ClientCount--; + } +} + /******************* *** PLAYER CODE *** diff --git a/core/PlayerManager.h b/core/PlayerManager.h index 34462a4f..602652bd 100644 --- a/core/PlayerManager.h +++ b/core/PlayerManager.h @@ -159,7 +159,8 @@ private: class PlayerManager : public SMGlobalClass, - public IPlayerManager + public IPlayerManager, + public IGameEventListener2 { friend class CPlayer; public: @@ -202,6 +203,7 @@ public: //IPlayerManager IGamePlayer *GetGamePlayer(edict_t *pEdict); int GetMaxClients(); int GetNumPlayers(); + int GetNumClients(); int GetClientOfUserId(int userid); bool IsServerActivated(); int FilterCommandTarget(IGamePlayer *pAdmin, IGamePlayer *pTarget, int flags); @@ -212,6 +214,8 @@ public: //IPlayerManager int GetClientFromSerial(unsigned int serial); void ClearAdminId(AdminId id); void RecheckAnyAdmins(); +public: // IGameEventListener2 + void FireGameEvent(IGameEvent *pEvent); public: inline int MaxClients() { @@ -272,6 +276,7 @@ private: int m_SourceTVUserId; int m_ReplayUserId; bool m_bInCCKVHook; + int m_ClientCount; private: static const int NETMSG_TYPE_BITS = 5; // SVC_Print overhead for netmsg type static const int SVC_Print_BufferSize = 2048 - 1; // -1 for terminating \0 diff --git a/core/logic/smn_players.cpp b/core/logic/smn_players.cpp index 9b73542c..3ad39538 100644 --- a/core/logic/smn_players.cpp +++ b/core/logic/smn_players.cpp @@ -270,18 +270,7 @@ static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params) return playerhelpers->GetNumPlayers(); } - int maxplayers = playerhelpers->GetMaxClients(); - int count = 0; - for (int i = 1; i <= maxplayers; ++i) - { - IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(i); - if ((pPlayer->IsConnected()) && !(pPlayer->IsInGame())) - { - count++; - } - } - - return (playerhelpers->GetNumPlayers() + count); + return playerhelpers->GetNumClients(); } static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params) diff --git a/public/IPlayerHelpers.h b/public/IPlayerHelpers.h index 672cddb5..89a977cc 100644 --- a/public/IPlayerHelpers.h +++ b/public/IPlayerHelpers.h @@ -41,7 +41,7 @@ #include #define SMINTERFACE_PLAYERMANAGER_NAME "IPlayerManager" -#define SMINTERFACE_PLAYERMANAGER_VERSION 21 +#define SMINTERFACE_PLAYERMANAGER_VERSION 22 struct edict_t; class IPlayerInfo; @@ -543,10 +543,17 @@ namespace SourceMod /** * @brief Returns the number of players currently connected. * - * @return Current number of connected clients. + * @return Current number of connected players. */ virtual int GetNumPlayers() =0; + /** + * @brief Returns the number of clients currently connected. + * + * @return Current number of connected clients. + */ + virtual int GetNumClients() =0; + /** * @brief Returns the client index by its userid. *