I want to make a plugin that play a specific sound when a word it's triggered in chat.
PHP Code:
#pragma semicolon 1
#define PLUGIN_AUTHOR "Teazze aka kRatoss"
#define PLUGIN_VERSION "1.00"
#include <sourcemod>
#include <vip_core>
#include <sdktools>
#include <clientprefs>
#pragma newdecls required
public Plugin myinfo =
{
name = "Sank Sounds",
author = PLUGIN_AUTHOR,
description = "Provides in-game sanks.",
version = PLUGIN_VERSION,
url = ""
}
bool g_Enabled = false,
HasSoundsEnab[MAXPLAYERS + 1];
int g_iSankCount[MAXPLAYERS + 1],
g_MaxSanksPerRound = 15,
g_SanksThisRound,
g_MaxSanksPerClient = 5,
Id;
static const char Feature[] = "Sanks";
char g_sWord[999][1024];
char g_sPath[999][1024];
Handle g_Cookie;
public void OnPluginStart()
{
if(!VIP_IsVIPLoaded()) VIP_OnVIPLoaded();
RegConsoleCmd("say", OnSay);
RegConsoleCmd("teamsay", OnSay);
HookEventEx("announce_phase_end", DisabPlugin);
HookEventEx("cs_intermission", DisabPlugin);
HookEventEx("cs_win_panel_match", DisabPlugin);
HookEventEx("start_halftime", DisabPlugin);
HookEventEx("round_start", EnabPlugin);
RegConsoleCmd("sm_sank", SankMenu);
RegConsoleCmd("sm_sanks", SankMenu);
RegConsoleCmd("sm_pulamea", SankMenu);
g_Cookie = RegClientCookie("tz_sanks", "Sanks prefs.", CookieAccess_Private);
}
public void VIP_OnVIPLoaded()
{
VIP_RegisterFeature(Feature, INT);
}
public Action SankMenu(int Client, int Args)
{
Menu menu = new Menu(Handler);
menu.SetTitle("Enable Sank Sounds?");
menu.AddItem("no", "No");
menu.AddItem("yes", "Yes");
menu.Display(Client, MENU_TIME_FOREVER);
}
public int Handler(Menu menu, MenuAction action, int client, int param)
{
if(action == MenuAction_Select)
{
char item[1024];
GetMenuItem(menu, param, item, sizeof(item));
if(strcmp(item, "no") == 0)
{
SetClientCookie(client, g_Cookie, "0");
PrintToChat(client, "Sound Disabled");
}
if(strcmp(item, "yes") == 0)
{
SetClientCookie(client, g_Cookie, "1");
PrintToChat(client, "Sound Enable");
}
}
}
public void OnClientCookiesCached(int client)
{
char cookie[32];
GetClientCookie(client, g_Cookie, cookie, sizeof(cookie));
if(strcmp(cookie, "0"))
HasSoundsEnab[client] = false;
if(strcmp(cookie, "1"))
HasSoundsEnab[client] = true;
}
public Action DisabPlugin(Handle pHndl, const char[] Name, bool bNoBCast)
{
g_Enabled = false;
}
public Action EnabPlugin(Handle pHndl, const char[] Name, bool bNoBCast)
{
static int iId = INVALID_ENT_REFERENCE;
for (iId = 1; iId <= MaxClients; iId++)
g_iSankCount[iId] = 0;
g_SanksThisRound = 0;
g_Enabled = true;
}
public void OnMapStart()
{
LoadSanks();
}
public Action OnSay(int Client, int Args)
{
char sArg[64];
GetCmdArgString(sArg, sizeof(sArg));
if (sArg[0] == '"') /* Strip the ending quote, if there is one */
{
int len = strlen(sArg);
if (sArg[len-1] == '"')
sArg[len-1] = '\0';
}
static int Count;
for (Count = 0; Count < sizeof(g_sWord[]); Count++)
{
if(strcmp(sArg, g_sWord[Count]) == 0)
{
if (VIP_IsClientFeatureUse(Client, Feature) \
&& g_iSankCount[Client] < g_MaxSanksPerClient \
&& g_SanksThisRound < g_MaxSanksPerRound \
&& g_Enabled == true \
&& HasSoundsEnab[Client] == true)
{
for (int i = 1; i <= MaxClients; i++)
{
if (IsValidClient(i) && HasSoundsEnab[i])
{
ClientCommand(i, "play %s", g_sPath[Count]);
PrintToChatAll("[debug] A fost redat un sank cu Word Trigger-ul : %s si Path-ul : %s", g_sWord[Count], g_sPath[Count]);
}
}
g_SanksThisRound++;
g_iSankCount[Client]++;
}
}
}
}
void LoadSanks()
{
KeyValues kv = CreateKeyValues("Sanks");
char file[256], path[256], WordTrigger[256], SoundPath[256];
FormatEx(file, sizeof(file), "configs/teaze_plugins/sanks.cfg");
BuildPath(Path_SM, path, sizeof(path), file);
if(!FileExists(path))
SetFailState("Can not find config file ( %s )!", path);
if(FileExists(path))
kv.ImportFromFile(path);
Id = 1;
if(kv.GotoFirstSubKey())
{
do
{
kv.GetSectionName(WordTrigger, sizeof(WordTrigger));
kv.GetString("path", SoundPath, sizeof(SoundPath));
strcopy(g_sWord[Id], sizeof(g_sWord[]), WordTrigger);
strcopy(g_sPath[Id], sizeof(g_sPath[]), SoundPath);
PreCache(g_sPath[Id]);
Id++;
}
while (kv.GotoNextKey());
}
kv.Rewind();
delete kv;
}
stock bool IsValidClient(int client)
{
if (client <= 0) return false;
if (client > MaxClients) return false;
if (!IsClientConnected(client)) return false;
return IsClientInGame(client);
}
void PreCache(const char[] path)
{
char buffer[256];
FormatEx(buffer, sizeof(buffer), "sound/%s", path);
AddFileToDownloadsTable(buffer);
AddToStringTable(FindStringTable("soundprecache"), path);
}