2 * ===============================================================
3 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ===============================================================
6 * This file is not open source and may not be copied without explicit
7 * written permission of AlliedModders LLC. This file may not be redistributed
8 * in whole or significant part.
9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
14 #include "CPlayerManager.h"
15 #include "sm_stringutil.h"
17 static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params)
21 return g_Players.NumPlayers();
24 int maxplayers = g_Players.MaxClients();
26 for (int i=1; i<=maxplayers; ++i)
28 CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
29 if ((pPlayer->IsConnected()) && !(pPlayer->IsInGame()))
35 return (g_Players.NumPlayers() + count);
38 static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params)
40 return g_Players.MaxClients();
43 static cell_t sm_GetClientName(IPluginContext *pCtx, const cell_t *params)
45 int index = params[1];
46 if ((index < 1) || (index > g_Players.GetMaxClients()))
48 return pCtx->ThrowNativeError("Invalid client index %d.", index);
51 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
52 if (!pPlayer->IsConnected())
54 return pCtx->ThrowNativeError("Client %d is not connected.", index);
57 pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), pPlayer->GetName(), NULL);
61 static cell_t sm_GetClientIP(IPluginContext *pCtx, const cell_t *params)
63 int index = params[1];
64 if ((index < 1) || (index > g_Players.GetMaxClients()))
66 return pCtx->ThrowNativeError("Invalid client index %d.", index);
69 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
70 if (!pPlayer->IsConnected())
72 return pCtx->ThrowNativeError("Client %d is not connected.", index);
76 strcpy(buf, pPlayer->GetIPAddress());
78 if (params[4] && (ptr = strchr(buf, ':')))
83 pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), buf);
87 static cell_t sm_GetClientAuthStr(IPluginContext *pCtx, const cell_t *params)
89 int index = params[1];
90 if ((index < 1) || (index > g_Players.GetMaxClients()))
92 return pCtx->ThrowNativeError("Invalid client index %d.", index);
95 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
96 if (!pPlayer->IsConnected())
98 return pCtx->ThrowNativeError("Client %d is not connected.", index);
101 if (!pPlayer->IsAuthorized())
106 pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), pPlayer->GetAuthString());
111 static cell_t sm_IsPlayerConnected(IPluginContext *pCtx, const cell_t *params)
113 int index = params[1];
114 if ((index < 1) || (index > g_Players.GetMaxClients()))
116 return pCtx->ThrowNativeError("Invalid client index %d.", index);
119 return (g_Players.GetPlayerByIndex(index)->IsConnected()) ? 1 : 0;
122 static cell_t sm_IsPlayerIngame(IPluginContext *pCtx, const cell_t *params)
124 int index = params[1];
125 if ((index < 1) || (index > g_Players.GetMaxClients()))
127 return pCtx->ThrowNativeError("Invalid client index %d.", index);
130 return (g_Players.GetPlayerByIndex(index)->IsInGame()) ? 1 : 0;
133 static cell_t sm_IsPlayerAuthorized(IPluginContext *pCtx, const cell_t *params)
135 int index = params[1];
136 if ((index < 1) || (index > g_Players.GetMaxClients()))
138 return pCtx->ThrowNativeError("Invalid client index %d.", index);
141 return (g_Players.GetPlayerByIndex(index)->IsAuthorized()) ? 1 : 0;
144 static cell_t sm_IsPlayerFakeClient(IPluginContext *pCtx, const cell_t *params)
146 int index = params[1];
147 if ((index < 1) || (index > g_Players.GetMaxClients()))
149 return pCtx->ThrowNativeError("Invalid client index %d.", index);
152 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
153 if (!pPlayer->IsConnected())
155 return pCtx->ThrowNativeError("Client %d is not connected.", index);
158 return (pPlayer->IsFakeClient()) ? 1 : 0;
161 static cell_t sm_PrintToServer(IPluginContext *pCtx, const cell_t *params)
167 pCtx->LocalToString(params[1], &fmt);
168 size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
170 buffer[res++] = '\n';
173 META_CONPRINT(buffer);
178 static cell_t sm_PrintToConsole(IPluginContext *pCtx, const cell_t *params)
180 int index = params[1];
181 if ((index < 1) || (index > g_Players.GetMaxClients()))
183 return pCtx->ThrowNativeError("Invalid client index %d.", index);
186 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
187 if (!pPlayer->IsInGame())
189 return pCtx->ThrowNativeError("Client %d is not in game.", index);
196 pCtx->LocalToString(params[2], &fmt);
197 size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
199 buffer[res++] = '\n';
202 engine->ClientPrintf(pPlayer->GetEdict(), buffer);
207 REGISTER_NATIVES(playernatives)
209 {"GetMaxClients", sm_GetMaxClients},
210 {"GetClientCount", sm_GetClientCount},
211 {"GetClientName", sm_GetClientName},
212 {"GetClientIP", sm_GetClientIP},
213 {"GetClientAuthString", sm_GetClientAuthStr},
214 {"IsPlayerConnected", sm_IsPlayerConnected},
215 {"IsPlayerInGame", sm_IsPlayerIngame},
216 {"IsPlayerAuthorized", sm_IsPlayerAuthorized},
217 {"IsPlayerFakeClient", sm_IsPlayerFakeClient},
218 {"PrintToServer", sm_PrintToServer},
219 {"PrintToConsole", sm_PrintToConsole},