#pragma semicolon 1
public Plugin myinfo =
{
name = "all chat",
author = "Frenzzy",
description = "Relays chat messages to all players",
version = "1.1",
url = "http://forums.alliedmods.net/showthread.php?p=1593727",
};
/* Convars */
Handle
g_hCvarAllTalk = INVALID_HANDLE,
g_hCvarMode = INVALID_HANDLE,
g_hCvarTeam = INVALID_HANDLE;
/* Chat Message */
char
g_msgAuthor,
g_msgType[64],
g_msgName[64],
g_msgText[512];
bool
g_msgIsChat,
g_msgIsTeammate,
g_msgTarget[MAXPLAYERS + 1];
public void OnPluginStart()
{
// Events.
UserMsg SayText2 = GetUserMessageId("SayText2");
if (SayText2 == INVALID_MESSAGE_ID)
{
SetFailState("This game doesn't support SayText2 user messages.");
}
HookUserMessage(SayText2, Hook_UserMessage);
HookEvent("player_say", Event_PlayerSay);
g_hCvarAllTalk = FindConVar("sv_alltalk");
g_hCvarMode = CreateConVar("sm_allchat_mode", "2", "Relays chat messages to all players? 0 = No, 1 = Yes, 2 = If AllTalk On", _, true, 0.0, true, 2.0);
g_hCvarTeam = CreateConVar("sm_allchat_team", "2", "Who can see say_team messages? 0 = Default, 1 = All teammates, 2 = All players", _, true, 0.0, true, 2.0);
// Commands.
AddCommandListener(Command_Say, "say");
AddCommandListener(Command_Say, "say_team");
}
public Action Hook_UserMessage(UserMsg:msg_id, Handle bf, const players[], playersNum, bool reliable, bool init)
{
g_msgAuthor = BfReadByte(bf);
g_msgIsChat = bool:BfReadByte(bf);
BfReadString(bf, g_msgType, sizeof(g_msgType), false);
BfReadString(bf, g_msgName, sizeof(g_msgName), false);
BfReadString(bf, g_msgText, sizeof(g_msgText), false);
for (new i = 0; i < playersNum; i++)
{
g_msgTarget[players[i]] = false;
}
}
public Action Event_PlayerSay(Handle event, const char[] name, bool dontBroadcast)
{
int mode = GetConVarInt(g_hCvarMode);
if (mode < 1)
{
return;
}
if (mode > 1 && g_hCvarAllTalk != INVALID_HANDLE && !GetConVarBool(g_hCvarAllTalk))
{
return;
}
if (GetClientOfUserId(GetEventInt(event, "userid")) != g_msgAuthor)
{
return;
}
mode = GetConVarInt(g_hCvarTeam);
if (g_msgIsTeammate && mode < 1)
{
return;
}
decl players[MaxClients];
int playersNum = 0;
if (g_msgIsTeammate && mode == 1 && g_msgAuthor > 0)
{
int team = GetClientTeam(g_msgAuthor);
for (new client = 1; client <= MaxClients; client++)
{
if (IsClientInGame(client) && g_msgTarget[client] && GetClientTeam(client) == team)
{
players[playersNum++] = client;
}
g_msgTarget[client] = false;
}
}
else
{
for (new client = 1; client <= MaxClients; client++)
{
if (IsClientInGame(client) && g_msgTarget[client])
{
players[playersNum++] = client;
}
g_msgTarget[client] = false;
}
}
if (playersNum == 0)
{
return;
}
Handle SayText2 = StartMessage("SayText2", players, playersNum, USERMSG_RELIABLE | USERMSG_BLOCKHOOKS);
if (SayText2 != INVALID_HANDLE)
{
BfWriteByte(SayText2, g_msgAuthor);
BfWriteByte(SayText2, g_msgIsChat);
BfWriteString(SayText2, g_msgType);
BfWriteString(SayText2, g_msgName);
BfWriteString(SayText2, g_msgText);
EndMessage();
}
}
public Action Command_Say(client, const char[] command, argc)
{
for (new target = 1; target <= MaxClients; target++)
{
g_msgTarget[target] = true;
}
if (StrEqual(command, "say_team", false))
{
g_msgIsTeammate = true;
}
else
{
g_msgIsTeammate = false;
}
return Plugin_Continue;
}