View Single Post
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 03-15-2017 , 15:18   Re: [INC] CromChat
Reply With Quote #5

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 trueCC_MSG_SAYTEXTCC_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(iPlayersiPnum"ch")
        
        if(!
iPnum)
            return 
0
            
        CC_RefreshColors
()
    }
    
    static 
szMessage[191], szTeam[10], i
    vformat
(szMessage[1], charsmax(szMessage), szInput3)
    
szMessage[0] = 0x01
    szTeam
[0] = EOS
    
    
if(CC_PREFIX[0])
        
format(szMessagecharsmax(szMessage), "^x01%s %s"CC_PREFIXszMessage)
    
    for(
0sizeof(CC_REPLACE_COLORS) - 1+= 2)
        
replace_all(szMessagecharsmax(szMessage), CC_REPLACE_COLORS[i], CC_REPLACE_COLORS[1])
        
    for(
0sizeof(CC_PLUS_COLORS) - 1+= 2)
    {
        if(
contain(szMessageCC_PLUS_COLORS[i]) != -1)
        {
            
copy(szTeamcharsmax(szTeam), CC_PLUS_COLORS[1])
            break
        }
    }
    
    if(
szTeam[0])
    {
        for(
0sizeof(CC_PLUS_COLORS_LIST); i++)
            
replace_all(szMessagecharsmax(szMessage), CC_PLUS_COLORS_LIST[i], "^x03")
    }
    else if(
id)
        
get_user_team(idszTeamcharsmax(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(idszMessageszTeam)
    else
    {
        for(
0iPnumi++)
            
CC_SendMessage(iPlayers[i], szMessageszTeam)
    }
    
    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(iTypeCC_MSG_TEAMINFO_id)
        
write_byte(id)
        
write_string(szTeam)
        
message_end()
    }

    
message_begin(iTypeCC_MSG_SAYTEXT_id)
    
write_byte(id)
    
write_string("%s")
    
write_string(szMessage)
    
message_end()
}

stock CC_RefreshColors()
{
    static 
iPlayers[32], szTeam[10], iPnumiPlayeri
    get_players
(iPlayersiPnum"ch")
    
    for(
0iPnumi++)
    {
        
iPlayer iPlayers[i]
        
        
get_user_team(iPlayerszTeamcharsmax(szTeam))
        
message_begin(MSG_ONE_UNRELIABLECC_MSG_TEAMINFO_iPlayer)
        
write_byte(iPlayer)
        
write_string(szTeam)
        
message_end()
    }
}

stock CC_SetPrefix(const szPrefix[MAX_PREFIX_SIZE])
    
copy(CC_PREFIXcharsmax(CC_PREFIX), szPrefix
__________________

Last edited by OciXCrom; 03-15-2017 at 15:29.
OciXCrom is offline
Send a message via Skype™ to OciXCrom