Hello!
I have a quick question in regard to what I am coding:
PHP Code:
#include <sourcemod>
#define CONFIG "addons/sourcemod/configs/commandcooldown.cfg"
StringMap g_hHashMap;
Handle g_hTimer[MAXPLAYERS+1];
int g_iTimeleft[MAXPLAYERS+1];
public void OnPluginStart()
{
BrowseKeyValues();
}
public void OnClientDisconnect(int client)
{
delete g_hTimer[client];
g_iTimeleft[client] = 0;
}
public Action OnCommand(int client, const char[] command, int argc)
{
if (g_iTimeleft[client])
{
ReplyToCommand(client, "[SM] You cannot use this command again for %i seconds.", g_iTimeleft[client]);
return Plugin_Handled;
} else {
g_hHashMap.GetValue(command, g_iTimeleft[client]);
g_hTimer[client] = CreateTimer(1.0, Timer_Callback, client, TIMER_REPEAT);
}
return Plugin_Continue;
}
public Action Timer_Callback(Handle timer, int client)
{
g_iTimeleft[client]--;
if (!g_iTimeleft[client])
{
g_hTimer[client] = null;
return Plugin_Stop;
}
return Plugin_Continue;
}
void BrowseKeyValues()
{
delete g_hHashMap;
KeyValues kv = new KeyValues("Command Cooldown");
if (!FileExists(CONFIG))
{
kv.SetString("kill", "30");
kv.Rewind();
kv.ExportToFile(CONFIG);
}
if (!kv.ImportFromFile(CONFIG) || !kv.GotoFirstSubKey(false))
{
delete kv;
SetFailState("Failure parsing the config file %s!", CONFIG);
}
g_hHashMap = new StringMap();
char command[64];
int cooldown;
do {
kv.GetSectionName(command, sizeof(command));
kv.GotoFirstSubKey(false);
cooldown = kv.GetNum(NULL_STRING);
g_hHashMap.SetValue(command, cooldown);
AddCommandListener(OnCommand, command);
LogMessage("Added %i second cooldown for command %s.", cooldown, command);
} while kv.GotoNextKey(false);
delete kv;
}
Do I need to pass the client's UserID instead of their index or not (in the CreateTimer() function)? By my understanding, since I am assigning the timer handle to each client specifically, then I don't need to.
Thanks,
Grant