#include <amxmodx>
#define PLUGIN_NAME "EnJoin Music"
#define PLUGIN_VERSION "2.0.0"
#define PLUGIN_AUTHOR "the_hunter"
// [en]
// If set to 1, the music will play after the player
// fully connects to the server and before they choose a team.
// If set to 0, the music plays immediately upon connection.
//
// [ru]
// Если установлено значение 1, музыка будет воспроизводиться
// после полного входа игрока на сервер и до выбора команды.
// Если установлено значение 0, музыка воспроизводится сразу при подключении.
#define PLAY_ON_SERVER_ENTRY 1
// [en]
// Directory path where the music files are stored.
// The plugin will look for music in this folder.
//
// [ru]
// Путь к папке, где хранятся файлы музыки.
// Плагин будет искать музыку в этой папке.
#define MUSIC_FILES_DIR "sound/enjoin"
new JoinMusic[MAX_RESOURCE_PATH_LENGTH];
new bool:MusicPlayingForPlayer[MAX_PLAYERS + 1];
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
register_event("TeamInfo", "OnTeamInfo", "a", "2!UNASSIGNED");
}
public plugin_precache()
{
if (SelectRandomJoinMusic(JoinMusic, charsmax(JoinMusic))) {
precache_generic(JoinMusic);
}
}
#if PLAY_ON_SERVER_ENTRY == 0
public client_connect(player)
{
ScheduleJoinMusic(player);
}
#endif
public client_putinserver(player)
{
#if PLAY_ON_SERVER_ENTRY == 0
StopJoinMusic(player);
#else
ScheduleJoinMusic(player);
#endif
}
ScheduleJoinMusic(player)
{
if (EOS == JoinMusic[0]) {
return;
}
if (is_user_bot(player) || is_user_hltv(player)) {
return;
}
set_task(0.1, "PlayJoinMusic", player);
}
public client_disconnected(player)
{
remove_task(player);
MusicPlayingForPlayer[player] = false;
}
public OnTeamInfo()
{
new player = read_data(1);
if (!(0 < player <= MaxClients) || !MusicPlayingForPlayer[player]) {
return;
}
if (is_user_connected(player)) {
StopJoinMusic(player);
}
}
public PlayJoinMusic(player)
{
PlayMp3(player, JoinMusic);
MusicPlayingForPlayer[player] = true;
}
public StopJoinMusic(player)
{
StopMp3(player);
remove_task(player);
MusicPlayingForPlayer[player] = false;
}
bool:SelectRandomJoinMusic(musicFile[], maxLenght)
{
new Array:musicFiles = ArrayCreate(MAX_RESOURCE_PATH_LENGTH);
new dirPath[MAX_RESOURCE_PATH_LENGTH] = MUSIC_FILES_DIR;
new fileName[128];
new FileType:fileType = FileType_Unknown;
new dirHandle = open_dir(dirPath, fileName, charsmax(fileName), fileType);
if (0 == dirHandle) {
return false;
}
do {
if ((fileType != FileType_File) || (!CheckFileExtension(fileName, ".mp3"))) {
continue;
}
if (format(fileName, charsmax(fileName), "%s/%s", dirPath, fileName) > maxLenght) {
continue;
}
if (HasWhitespace(fileName)) {
continue;
}
ArrayPushString(musicFiles, fileName);
}
while (next_file(dirHandle, fileName, charsmax(fileName), fileType) != 0);
new musicCount = ArraySize(musicFiles);
ArrayGetString(musicFiles, Random(musicCount), musicFile, maxLenght);
close_dir(dirHandle);
ArrayDestroy(musicFiles);
return ((musicCount > 0) && (musicFile[0] != EOS));
}
stock Random(maximum)
{
return (random((1337 + maximum)) % maximum);
}
stock bool:CheckFileExtension(const fileName[], const extension[])
{
new fileLen = strlen(fileName);
new extLen = strlen(extension);
if (fileLen < extLen) {
return false;
}
return (equali(fileName[(fileLen - extLen)], extension) != 0);
}
stock bool:HasWhitespace(const string[])
{
for (new i = 0; EOS != string[i]; ++i) {
if ((' ' == string[i]) || ('^t' == string[i]) || ('^n' == string[i]) || ('^r' == string[i])) {
return true;
}
}
return false;
}
stock PlayMp3(player, const file[])
{
client_cmd(player, ";mp3 stop;wait;wait;wait;mp3 play ^"%s^"", file);
}
stock StopMp3(player)
{
client_cmd(player, ";mp3 stop");
}