Fixed incorrect use of ReferenceToIndex, which caused some functions to be unstable during client connection. (#2)

While this function works well with the client indexes we pass in, as its name
suggests, we should essentially be passing player references, not client indexes.
The problem is that SourceMod looks for this player in the entity list, but they
might not be there when connecting, or they might be another client or a bot.

This makes the code unstable and confusing, and it works intermittently.
Steam doesn't care whether the player is in the entity list—it works with the
Steam ID. Using ReferenceToIndex here is fundamentally wrong and causes random
-1 errors, especially during OnClientConnect when the entity may not exist yet.

This commit replaces it with a direct client index lookup, which is correct and
reliable because params[1] is already a client index.
This commit is contained in:
A1m`
2026-07-13 21:39:04 +00:00
committed by GitHub
parent 8fd7a1988c
commit 07fc524d27
2 changed files with 56 additions and 27 deletions
+28 -14
View File
@@ -219,11 +219,16 @@ static cell_t sm_UserHasLicenseForApp(IPluginContext *pContext, const cell_t *pa
return k_EUserHasLicenseResultNoAuth; return k_EUserHasLicenseResultNoAuth;
} }
int client = gamehelpers->ReferenceToIndex(params[1]); int client = params[1];
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); /* Man, including GameHelpers and PlayerHelpers for this native :(. */ if (client < 1 || client > playerhelpers->GetMaxClients())
if (pPlayer == NULL || pPlayer->IsConnected() == false)
{ {
return pContext->ThrowNativeError("Client index %d is invalid", params[1]); return pContext->ThrowNativeError("Client index %d is invalid", client);
}
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client);
if (pPlayer == NULL || !pPlayer->IsConnected())
{
return pContext->ThrowNativeError("Client index %d is not connected", client);
} }
CSteamID checkid = CreateCommonCSteamID(pPlayer, params, 3, 4); CSteamID checkid = CreateCommonCSteamID(pPlayer, params, 3, 4);
@@ -245,12 +250,16 @@ static cell_t sm_UserHasLicenseForAppId(IPluginContext *pContext, const cell_t *
static cell_t sm_GetClientSteamID(IPluginContext *pContext, const cell_t *params) static cell_t sm_GetClientSteamID(IPluginContext *pContext, const cell_t *params)
{ {
int client = gamehelpers->ReferenceToIndex(params[1]); int client = params[1];
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); if (client < 1 || client > playerhelpers->GetMaxClients())
if (pPlayer == NULL || pPlayer->IsConnected() == false)
{ {
return pContext->ThrowNativeError("Client index %d is invalid", params[1]); return pContext->ThrowNativeError("Client index %d is invalid", client);
}
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client);
if (pPlayer == NULL || !pPlayer->IsConnected())
{
return pContext->ThrowNativeError("Client index %d is not connected", client);
} }
CSteamID steamId = CreateCommonCSteamID(pPlayer, params, 4, 5); CSteamID steamId = CreateCommonCSteamID(pPlayer, params, 4, 5);
@@ -270,14 +279,19 @@ static cell_t sm_GetUserGroupStatus(IPluginContext *pContext, const cell_t *para
if (pServer == NULL) if (pServer == NULL)
{ {
return false; return 0;
} }
int client = gamehelpers->ReferenceToIndex(params[1]); int client = params[1];
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); /* Man, including GameHelpers and PlayerHelpers for this native :(. */ if (client < 1 || client > playerhelpers->GetMaxClients())
if (pPlayer == NULL || pPlayer->IsConnected() == false)
{ {
return pContext->ThrowNativeError("Client index %d is invalid", params[1]); return pContext->ThrowNativeError("Client index %d is invalid", client);
}
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client);
if (pPlayer == NULL || !pPlayer->IsConnected())
{
return pContext->ThrowNativeError("Client index %d is not connected", client);
} }
CSteamID checkid = CreateCommonCSteamID(pPlayer, params, 3, 4); CSteamID checkid = CreateCommonCSteamID(pPlayer, params, 3, 4);
+27 -12
View File
@@ -60,11 +60,16 @@ static cell_t sm_RequestUserStats(IPluginContext *pContext, const cell_t *params
return 0; return 0;
} }
int client = gamehelpers->ReferenceToIndex(params[1]); int client = params[1];
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); /* Man, including GameHelpers and PlayerHelpers for this native :(. */ if (client < 1 || client > playerhelpers->GetMaxClients())
if (pPlayer == NULL || pPlayer->IsConnected() == false)
{ {
return pContext->ThrowNativeError("Client index %d is invalid", params[1]); return pContext->ThrowNativeError("Client index %d is invalid", client);
}
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client);
if (pPlayer == NULL || !pPlayer->IsConnected())
{
return pContext->ThrowNativeError("Client index %d is not connected", client);
} }
CSteamID checkid = CreateCommonCSteamID(pPlayer, params); CSteamID checkid = CreateCommonCSteamID(pPlayer, params);
@@ -80,11 +85,16 @@ static cell_t sm_GetStatCell(IPluginContext *pContext, const cell_t *params)
return 0; return 0;
} }
int client = gamehelpers->ReferenceToIndex(params[1]); int client = params[1];
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); /* Man, including GameHelpers and PlayerHelpers for this native :(. */ if (client < 1 || client > playerhelpers->GetMaxClients())
if (pPlayer == NULL || pPlayer->IsConnected() == false)
{ {
return pContext->ThrowNativeError("Client index %d is invalid", params[1]); return pContext->ThrowNativeError("Client index %d is invalid", client);
}
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client);
if (pPlayer == NULL || !pPlayer->IsConnected())
{
return pContext->ThrowNativeError("Client index %d is not connected", client);
} }
char *pName; char *pName;
@@ -123,11 +133,16 @@ static cell_t sm_GetStatFloat(IPluginContext *pContext, const cell_t *params)
return 0; return 0;
} }
int client = gamehelpers->ReferenceToIndex(params[1]); int client = params[1];
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); /* Man, including GameHelpers and PlayerHelpers for this native :(. */ if (client < 1 || client > playerhelpers->GetMaxClients())
if (pPlayer == NULL || pPlayer->IsConnected() == false)
{ {
return pContext->ThrowNativeError("Client index %d is invalid", params[1]); return pContext->ThrowNativeError("Client index %d is invalid", client);
}
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client);
if (pPlayer == NULL || !pPlayer->IsConnected())
{
return pContext->ThrowNativeError("Client index %d is not connected", client);
} }
char *pName; char *pName;