Sorry for the late answer, I was a little busy today.
So, here's what I did so far:
- I made a sepparate function to send the message. Another issue that was present in the other includes, when sending a message to all players at once, the team color sometimes wasn't correct. What's happening is - when I send a message with some color, it keeps that color until the player receives the TeamInfo message again. For that problem I added a function that will reset the TeamInfo message for all players when sending it to everyone, but this includes two sepparate loops - should I keep it this way or should I not use MSG_ALL and use a loop for everyone?
- Another thing that bugged me was having to add the prefix manually every time. I added a stock which can be used to add a prefix to the messages with one single line in plugin_init() (or anywhere else).
- I changed szMessage to have 191 characters.
- I prevented the localized strings and %s bugs.
About the color symbols - they are not that much used in normal messages, but when I think about it, a player's nickname can easily contain them on purpose or not. Is it possible to use symbols such as ^7, ^6, etc? I tried doing it, but it showed a blank space in the chat. Any way to make them replaceable inside the code? I know they can't be manually added in chat, so this would be the most safe way to do it.
{green} style is way too long.
By the way, is it possible to add a parameter after "any:..."?
By the way 2 - what's the difference between MSG_ONE and MSG_ONE_UNRELIABLE? Which one should I use?
PHP Code:
#if defined _cromchat_included
#endinput
#endif
#define _cromchat_included
#if !defined ColorChat
#define ColorChat CromChat
#endif
#if !defined client_print_color
#define client_print_color CromChat
#endif
#define MAX_PREFIX_SIZE 64
new CC_PREFIX[MAX_PREFIX_SIZE], bool:CC_FIRST_TIME = true, CC_MSG_SAYTEXT, CC_MSG_TEAMINFO
new const CC_REPLACE_COLORS[][] = { "!g", "^x04", "!t", "^x03", "!n", "^x01" }
new const CC_PLUS_COLORS[][] = { "!r", "TERRORIST", "!b", "CT", "!w", "SPECTATOR" }
new const CC_PLUS_COLORS_LIST[][] = { "!r", "!b", "!w" }
stock CromChat(const id, const szInput[], any:...)
{
static iPlayers[32], iPnum
if(!id)
{
get_players(iPlayers, iPnum, "ch")
if(!iPnum)
return 0
CC_RefreshColors()
}
static szMessage[191], szTeam[10], i
vformat(szMessage[1], charsmax(szMessage), szInput, 3)
szMessage[0] = 0x01
szTeam[0] = EOS
if(CC_PREFIX[0])
format(szMessage, charsmax(szMessage), "^x01%s %s", CC_PREFIX, szMessage)
for(i = 0; i < sizeof(CC_REPLACE_COLORS) - 1; i += 2)
replace_all(szMessage, charsmax(szMessage), CC_REPLACE_COLORS[i], CC_REPLACE_COLORS[i + 1])
for(i = 0; i < sizeof(CC_PLUS_COLORS) - 1; i += 2)
{
if(contain(szMessage, CC_PLUS_COLORS[i]) != -1)
{
copy(szTeam, charsmax(szTeam), CC_PLUS_COLORS[i + 1])
break
}
}
if(szTeam[0])
{
for(i = 0; i < sizeof(CC_PLUS_COLORS_LIST); i++)
replace_all(szMessage, charsmax(szMessage), CC_PLUS_COLORS_LIST[i], "^x03")
}
else if(id)
get_user_team(id, szTeam, charsmax(szTeam))
if(CC_FIRST_TIME)
{
CC_FIRST_TIME = false
CC_MSG_SAYTEXT = get_user_msgid("SayText")
CC_MSG_TEAMINFO = get_user_msgid("TeamInfo")
}
if(id)
CC_SendMessage(id, szMessage, szTeam)
else
{
for(i = 0; i < iPnum; i++)
CC_SendMessage(iPlayers[i], szMessage, szTeam)
}
return strlen(szMessage)
}
stock CC_SendMessage(const id, const szMessage[], const szTeam[])
{
static iType
iType = !id ? MSG_ALL : MSG_ONE_UNRELIABLE
if(szTeam[0])
{
message_begin(iType, CC_MSG_TEAMINFO, _, id)
write_byte(id)
write_string(szTeam)
message_end()
}
message_begin(iType, CC_MSG_SAYTEXT, _, id)
write_byte(id)
write_string("%s")
write_string(szMessage)
message_end()
}
stock CC_RefreshColors()
{
static iPlayers[32], szTeam[10], iPnum, iPlayer, i
get_players(iPlayers, iPnum, "ch")
for(i = 0; i < iPnum; i++)
{
iPlayer = iPlayers[i]
get_user_team(iPlayer, szTeam, charsmax(szTeam))
message_begin(MSG_ONE_UNRELIABLE, CC_MSG_TEAMINFO, _, iPlayer)
write_byte(iPlayer)
write_string(szTeam)
message_end()
}
}
stock CC_SetPrefix(const szPrefix[MAX_PREFIX_SIZE])
copy(CC_PREFIX, charsmax(CC_PREFIX), szPrefix)
__________________