PHP Code:
#pragma semicolon 1
#define DEBUG
#define PLUGIN_AUTHOR "Rowdy"
#define PLUGIN_VERSION "1.00"
#define PLUGIN_CONFIG "configs/Sounds"
#define MAX_SOUNDS 56
#include <sourcemod>
#include <sdktools>
#pragma newdecls required
enum Sounds_Preferences {
String:gSoundName[64],
String:gSoundPath[255],
Float:gVolume,
gCooldown
};
int iSoundId = 0;
int iSound[MAX_SOUNDS][Sounds_Preferences];
char cSoundsConfig[255];
Handle hTimer;
int iGlobalCooldown;
Handle hAdminFlag;
char cAdminFlag[2];
public Plugin myinfo =
{
name = "SoundsManager",
author = PLUGIN_AUTHOR,
description = "Play your own sound with this plugin. Customize your cooldown & volume for specific sound.",
version = PLUGIN_VERSION,
url = "http://steamcommunity.com/profiles/76561198307962930"
};
public void OnPluginStart()
{
RegConsoleCmd("sm_sounds", Command_Sounds);
RegConsoleCmd("sm_soundsreload", Command_Reload);
HookEvent("round_start", OnRoundStart);
hAdminFlag = CreateConVar("soundsmanager_flag", "b", "Set admin flag for access to command.\nLeave it blank for no access needed.");
AutoExecConfig(true, "soundsmanager");
}
public void OnMapStart() {
LoadConfig();
}
public void OnMapEnd() {
if (hTimer != null)
delete hTimer;
}
public void OnConfigsExecuted() {
GetConVarString(hAdminFlag, cAdminFlag, sizeof(cAdminFlag));
}
void LoadConfig() {
char file[255];
BuildPath(Path_SM, file, sizeof(file), "%s", PLUGIN_CONFIG);
if (!DirExists(file))
CreateDirectory(file, 511);
BuildPath(Path_SM, cSoundsConfig, sizeof(cSoundsConfig), "%s/config.cfg", PLUGIN_CONFIG);
if (!FileExists(cSoundsConfig)) {
KeyValues kv = new KeyValues("sounds");
kv.ImportFromFile(cSoundsConfig);
kv.JumpToKey("Sound #1", true);
kv.SetString("path", "sound/myfolder/apple.mp3");
kv.SetFloat("volume", 1.0);
kv.SetNum("cd", 5);
kv.Rewind();
kv.ExportToFile(cSoundsConfig);
delete kv;
}
clearSoundIds();
KeyValues kv = new KeyValues("sounds");
kv.ImportFromFile(cSoundsConfig);
if (kv.GotoFirstSubKey()) {
char cSoundName[64], cSoundPath[255], cDownloadPath[255];
float fSoundVolume;
int iSoundCooldown;
do {
kv.GetSectionName(cSoundName, sizeof(cSoundName));
kv.GetString("path", cSoundPath, sizeof(cSoundPath));
fSoundVolume = kv.GetFloat("volume", 1.0);
iSoundCooldown = kv.GetNum("cd", 0);
if (StrContains(cSoundPath, "sound/") != -1)
strcopy(cSoundPath, sizeof(cSoundPath), cSoundPath[6]);
PrecacheSound(cSoundPath, true);
Format(cDownloadPath, sizeof(cDownloadPath), "sound/%s", cSoundPath);
AddFileToDownloadsTable(cDownloadPath);
strcopy(iSound[iSoundId][gSoundName], 64, cSoundName);
strcopy(iSound[iSoundId][gSoundPath], 255, cSoundPath);
iSound[iSoundId][gVolume] = fSoundVolume;
iSound[iSoundId][gCooldown] = iSoundCooldown;
iSoundId++;
} while (kv.GotoNextKey());
}
delete kv;
}
public void OnRoundStart(Handle event, char[] name, bool dontBroadcast) {
iGlobalCooldown = 0;
}
void clearSoundIds() {
for (int i = 0; i < MAX_SOUNDS; i++) {
strcopy(iSound[iSoundId][gSoundName], 64, "");
strcopy(iSound[iSoundId][gSoundPath], 255, "");
iSound[iSoundId][gVolume] = 1.0;
iSound[iSoundId][gCooldown] = 0;
}
iSoundId = 0;
}
public Action Command_Reload(int client, int args) {
if (!HasPlayerAccess(client, cAdminFlag)) {
PrintToChat(client, " \x02You don't have access to this command!");
return Plugin_Continue;
}
LoadConfig();
PrintToChat(client, "Config is now reloaded!");
return Plugin_Handled;
}
public Action Command_Sounds(int client, int args) {
if (!HasPlayerAccess(client, cAdminFlag)) {
PrintToChat(client, " \x02You don't have access to this command!");
return Plugin_Continue;
}
SoundsMenu(client);
return Plugin_Handled;
}
public void SoundsMenu(int client) {
if (IsCooldown()) {
PrintToChat(client, "You need to wait %i second/s to play another sound.", iGlobalCooldown - 1);
return;
}
Menu menu = new Menu(SoundsMenuHandler);
char cId[5];
menu.SetTitle("Choose sound to play:");
for (int i = 0; i < iSoundId; i++) {
IntToString(i, cId, sizeof(cId));
menu.AddItem(cId, iSound[i][gSoundName]);
}
menu.Display(client, 60);
}
public int SoundsMenuHandler(Menu menu, MenuAction action, int client, int pos) {
if (action == MenuAction_Select) {
if (IsCooldown()) {
PrintToChat(client, "You need to wait %i second/s to play another sound.", iGlobalCooldown - 1);
return;
}
char Item[5];
menu.GetItem(pos, Item, sizeof(Item));
int id = StringToInt(Item);
EmitSoundToAll(iSound[id][gSoundPath], _, _, _, _, iSound[id][gVolume]);
iGlobalCooldown = iSound[id][gCooldown] + 1;
if (hTimer == null)
hTimer = CreateTimer(1.0, refreshTimer, _, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE);
PrintToChat(client, "Now playing: %s", iSound[id][gSoundName]);
}
}
public Action refreshTimer(Handle timer) {
if (iGlobalCooldown > 1) {
iGlobalCooldown--;
} else if (iGlobalCooldown == 1) {
iGlobalCooldown--;
if (hTimer != null)
delete hTimer;
}
}
public bool IsCooldown() {
if (iGlobalCooldown > 0)
return true;
return false;
}
public bool HasPlayerAccess(int client, char[] accessflag) {
if (strlen(accessflag) == 1) {
AdminId admin = GetUserAdmin(client);
AdminFlag flag;
if (!FindFlagByChar(accessflag[0], flag))
return false;
if (!GetAdminFlag(admin, flag))
return false;
return true;
}
return true;
}