AlliedModders Donor
|
08-16-2018
, 16:42
Re: Get client's weapons.
|
#8
|
Here's two different methods I've used. All of which can be made better, but the bones are there.
Method of looping through m_hMyWeapons:
Spoiler
PHP Code:
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#include <cstrike>
#pragma newdecls required
char sWeapon[MAXPLAYERS + 1][5][32];
public void OnPluginStart()
{
RegAdminCmd("sm_ll", Command_LoadoutList, ADMFLAG_ROOT, "Lists current clients weapon loadout.");
}
public Action Command_LoadoutList(int client, int args)
{
if (client == 0)
{
ReplyToCommand(client, "[SM] %t", "Command is in-game only");
return Plugin_Handled;
}
if (IsValidClient(client) && GetClientTeam(client) > CS_TEAM_SPECTATOR)
{
int size = GetEntPropArraySize(client, Prop_Send, "m_hMyWeapons");
PrintToServer("///////////////////////////////////////////////////////////");
PrintToServer("");
for (int i = 0; i < size; i++)
{
int weaponEntity = GetEntPropEnt(client, Prop_Send, "m_hMyWeapons", i);
if (weaponEntity != -1 && GetEntityClassname(weaponEntity, sWeapon[client][i], sizeof(sWeapon[][])))
{
SetWeaponClassname(weaponEntity, sWeapon[client][i], sizeof(sWeapon[][]));
PrintToServer("SLOT: %i - ENTITY: %i - CLASS: %s", i, weaponEntity, sWeapon[client][i]);
}
}
PrintToServer("");
PrintToServer("///////////////////////////////////////////////////////////");
}
return Plugin_Handled;
}
void SetWeaponClassname(int weapon, char[] buffer, int size)
{
switch (GetEntProp(weapon, Prop_Send, "m_iItemDefinitionIndex"))
{
case 23: Format(buffer, size, "weapon_mp5sd");
case 60: Format(buffer, size, "weapon_m4a1_silencer");
case 61: Format(buffer, size, "weapon_usp_silencer");
case 63: Format(buffer, size, "weapon_cz75a");
case 64: Format(buffer, size, "weapon_revolver");
default: GetEntityClassname(weapon, buffer, size);
}
}
bool IsValidClient(int client)
{
if (!(0 < client <= MaxClients)) return false;
if (!IsClientInGame(client)) return false;
return true;
}
Method of looping through client's weapon slots:
Spoiler
PHP Code:
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#include <cstrike>
#pragma newdecls required
char sWeapon[MAXPLAYERS + 1][5][32];
public void OnPluginStart()
{
RegAdminCmd("sm_wu", Command_WeaponClassUpdate, ADMFLAG_ROOT, "Updates clients weapon index.");
RegAdminCmd("sm_wl", Command_WeaponClassList, ADMFLAG_ROOT, "Lists clients weapon index.");
RegAdminCmd("sm_wg", Command_WeaponClassGive, ADMFLAG_ROOT, "Gives clients weapon index.");
}
public Action Command_WeaponClassUpdate(int client, int args)
{
if (client == 0)
{
ReplyToCommand(client, "[SM] %t", "Command is in-game only");
return Plugin_Handled;
}
if (IsValidClient(client) && GetClientTeam(client) > CS_TEAM_SPECTATOR)
{
PrintToServer("///////////////////////////////////////////////////////////");
PrintToServer("");
PrintToServer("Command_WeaponClassUpdate");
PrintToServer("");
for (int i = 0; i < 5; i++)
{
int weapon = GetPlayerWeaponSlot(client, i);
if (weapon != -1)
{
SetWeaponClassname(weapon, sWeapon[client][i], sizeof(sWeapon[][]));
PrintToServer("SLOT: %i - WEAPON: %s", i, sWeapon[client][i]);
}
}
PrintToServer("");
PrintToServer("///////////////////////////////////////////////////////////");
}
return Plugin_Handled;
}
public Action Command_WeaponClassList(int client, int args)
{
if (client == 0)
{
ReplyToCommand(client, "[SM] %t", "Command is in-game only");
return Plugin_Handled;
}
if (IsValidClient(client) && GetClientTeam(client) > CS_TEAM_SPECTATOR)
{
PrintToServer("///////////////////////////////////////////////////////////");
PrintToServer("");
PrintToServer("Command_WeaponClassList");
PrintToServer("");
for (int i = 0; i < 5; i++)
{
if (strlen(sWeapon[client][i]))
{
PrintToServer("m_iItemDefinitionIndex: %i", GetWeaponClassname(client, i));
PrintToServer("SLOT: %i - WEAPON: %s", i, sWeapon[client][i]);
PrintToServer("");
}
}
PrintToServer("");
PrintToServer("///////////////////////////////////////////////////////////");
}
return Plugin_Handled;
}
public Action Command_WeaponClassGive(int client, int args)
{
if (client == 0)
{
ReplyToCommand(client, "[SM] %t", "Command is in-game only");
return Plugin_Handled;
}
if (IsValidClient(client) && GetClientTeam(client) > CS_TEAM_SPECTATOR)
{
RemoveClientWeapons(client);
PrintToServer("///////////////////////////////////////////////////////////");
PrintToServer("");
PrintToServer("Command_WeaponClassGive");
PrintToServer("");
for (int i = 0; i < 5; i++)
{
if (strlen(sWeapon[client][i]) && IsValidEntity(FindEntityByClassname(client, sWeapon[client][i]) != -1))
{
GivePlayerItem(client, sWeapon[client][i]);
PrintToServer("SLOT: %i - WEAPON: %s", i, sWeapon[client][i]);
}
}
PrintToServer("");
PrintToServer("///////////////////////////////////////////////////////////");
}
return Plugin_Handled;
}
void RemoveClientWeapons(int client)
{
if (IsValidClient(client))
{
for (int i = 0; i < 5; i++)
{
int index;
while ((index = GetPlayerWeaponSlot(client, i)) != -1)
{
RemovePlayerItem(client, index);
AcceptEntityInput(index, "Kill");
}
}
}
}
int GetWeaponClassname(int client, int slot)
{
int index = FindEntityByClassname(client, sWeapon[client][slot]);
if (StrEqual(sWeapon[client][slot], "weapon_mp5sd")) return 23;
if (StrEqual(sWeapon[client][slot], "weapon_m4a1_silencer")) return 60;
if (StrEqual(sWeapon[client][slot], "weapon_usp_silencer")) return 61;
if (StrEqual(sWeapon[client][slot], "weapon_cz75a")) return 63;
if (StrEqual(sWeapon[client][slot], "weapon_revolver")) return 64;
else
GetEntProp(index, Prop_Send, "m_iItemDefinitionIndex");
return index;
}
void SetWeaponClassname(int weapon, char[] buffer, int size)
{
switch (GetEntProp(weapon, Prop_Send, "m_iItemDefinitionIndex"))
{
case 23: Format(buffer, size, "weapon_mp5sd");
case 60: Format(buffer, size, "weapon_m4a1_silencer");
case 61: Format(buffer, size, "weapon_usp_silencer");
case 63: Format(buffer, size, "weapon_cz75a");
case 64: Format(buffer, size, "weapon_revolver");
default: GetEntityClassname(weapon, buffer, size);
}
}
bool IsValidClient(int client)
{
if (!(0 < client <= MaxClients)) return false;
if (!IsClientInGame(client)) return false;
return true;
}
|
|