#include <sourcemod>
#include <sdktools>
#include <morecolors>

#define JAILMUTER_VERSION "1.2"

new Handle:jailmuter_enabled;
new Handle:jailmuter_msg;
new Handle:jailmuter_adm;

public Plugin:myinfo = {
	name = "asdf",
	author = "asdf",
	description = "asdf",
	version = JAILMUTER_VERSION,
	url = "asdf"
};

public OnPluginStart() {
	
	jailmuter_enabled = CreateConVar("sm_jailmuter_enabled", "1", "Enable or disable the Jail Muter; 0 - disabled, 1 - enabled");
	jailmuter_msg = CreateConVar("sm_jailmuter_msg", "1", "Enable or disable the Jail Muter Messages; 0 - disabled, 1 - enabled");
	jailmuter_adm = CreateConVar("sm_jailmuter_adm", "0", "Enable or disable mute admins; 0 - Admins won't be muted, 1 - Admins will be muted");
	HookEvent("player_death", PlayerDeath);
	HookEvent("player_spawn", PlayerSpawn);
	HookEvent("round_start", RoundStart);
}

public Action:PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast) {
	if (GetConVarInt(jailmuter_enabled) == 1) {
		new index = GetClientOfUserId(GetEventInt(event, "userid"));
		if(GetConVarInt(jailmuter_adm) == 0) {
			new flags = GetUserFlagBits(index); 
			if(flags & ADMFLAG_GENERIC || flags & ADMFLAG_ROOT || flags & ADMFLAG_SLAY || flags & ADMFLAG_CHEATS) {
				return Plugin_Continue; 
			} else {
				SetClientListeningFlags(index, VOICE_MUTED);
			}
		}
		if (GetConVarInt(jailmuter_msg) == 1) {
			PrintToChat(index, "{red}[Jail Muter] {yellow}You are {blue}muted {yellow}you will be {blue}unmuted {yellow}in the next round");
		}
	}
	return Plugin_Continue;
}

public Action:PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast) {
	if (GetConVarInt(jailmuter_enabled) == 1) {
		new index = GetClientOfUserId(GetEventInt(event, "userid"));
		SetClientListeningFlags(index, VOICE_NORMAL);
	}
	return Plugin_Continue;
}

public Action:RoundStart(Handle:event, const String:name[], bool:dontBroadcast) {
	if (GetConVarInt(jailmuter_enabled) == 1) {
		if (GetConVarInt(jailmuter_msg) == 1) {
			PrintToChatAll("{red}[Jail Muter] {yellow}All the players are {blue}unmuted")
		}
	}
	return Plugin_Continue;
}