Raised This Month: $404 Target: $400
 101% 

[INC] CromChat - a better ColorChat!


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 03-14-2017 , 16:27   [INC] CromChat - a better ColorChat!
Reply With Quote #1


---------- [ Description ] ----------


Since every colorchat include that I used so far had something that I don't like, I decided to make one my self and fix the things that annoy me. For example, I didn't like using RED/BLUE/TEAM_COLOR as a parameter in the ColorChat include, another include displayed an error when sending a message in an empty server, and so on. So, here's my version.

---------- [ Color Codes ] ----------


Beside the default symbols - ^x04, ^x03 & ^x01, you can also use these ones:

&x07 = red
&x06 = blue
&x05 = white
&x04 = green
&x03 = team color
&x01 = normal
&x00 = removes message prefix (only if put in the beginning of the message)


You can use only one of the following in a single message: &x07, &x06, &x05, &x03. If you use more than one in a message, it will automatically select the color that is first in the list and will replace all other colors with that one. Combining more than one team color is not possible in CS 1.6!!!

You can choose from multiple color code groups by adding a single line in the .sma file. You can see all the different groups on the image below. By default, the CROMCHAT group is used.



To activate a different color group, simply add #define CC_COLORS_TYPE CC_COLORS_<group name> before #include <cromchat>. Example: if you want to activate the group SHORT, the code needs to look like this:

PHP Code:
#define CC_COLORS_TYPE CC_COLORS_SHORT
#include <cromchat> 
Bear in mind that the codes from the group STANDARD cannot be used lang, .ini and other files. They are only available in the .sma file, so don't use this group if the plugin uses multiple files.

You can also make your own custom set of codes, by using the group CUSTOM like this:

PHP Code:
#define CC_COLORS_TYPE CC_COLORS_CUSTOM
#define CC_SYM_CHAT_NORMAL         "&x01"
#define CC_SYM_CHAT_TEAM         "&x03"
#define CC_SYM_CHAT_GREEN         "&x04"
#define CC_SYM_CHAT_WHITE         "&x05"
#define CC_SYM_CHAT_BLUE         "&x06"
#define CC_SYM_CHAT_RED         "&x07"
#define CC_SYM_CHAT_NOPREF         "&x00"
#include <cromchat> 
In the code you replace the &x0 with whatever you want.

---------- [ Sending Messages ] ----------


Messages are sent using the function CC_SendMessage or CromChat:

PHP Code:
CC_SendMessage(idszMessage[], any:...) 
With the function CC_LogMessage you can send a chat message and log it at the same time:

PHP Code:
// This will send a message to "id" and log it in the default log file.
CC_LogMessage(id_"&x04green &x01is not &x07red")

// This will send a message to all players and log it in the file "test.txt".
CC_LogMessage(0"test.txt""&x03Cooool story bro!"
You can use CC_SendMatched to send a message that obeys the targeted player's team color. You can also use ColorChat or client_print_color:

PHP Code:
CC_SendMatched(const id, const iPlayer, const szInput[], any:...) 
Where iPlayer is the player that will be used as a target for the team color. Instead of a player, you can also add one of the following color codes: CC_COLOR_TEAM, CC_COLOR_GREY, CC_COLOR_BLUE, CC_COLOR_RED.

PHP Code:
CC_SendMatched(idiTarget"message")
CC_SendMatched(0CC_COLOR_GREY"message" 
It is also possible to send a message to a specific group of players by using the same flags as the get_players function. To do this, use the function CC_GroupMessage. The example below shows how to send a message to all alive terrorists.

PHP Code:
CC_GroupMessage("ae""TERRORIST""message"
The library also contains a function to send a message to all players who have specified admin flags. This is the function CC_SendAdminMessage.

PHP Code:
CC_SendAdminMessage("abcei"true"message"
If the second argument is set to true, the message will be sent to all players who have ALL of the specified admin flags, otherwise, if it's set to false, it will be sent to players who have ANY of the specified admin flags.

CromChat also has functions that can replace the show_activity functions that are used within the default AMXX plugins. By default, when you write #include <cromchat> in your plugin, all "activity" functions automatically get replaced with the corresponding functions from cromchat, which allows you to easily replace the messages from the default AMXX plugins with colored ones. If you want to disable the automatic transformation of these functions, simply add #define CC_DONT_OVERWRITE_ACTIVITY before #include <cromchat>.

---------- [ Adding A Prefix ] ----------


With the function CC_SetPrefix you can specify a global prefix that will automatically be added in the beginning of each message. This is much more easier than having to add a prefix manually on each line of code. The function needs to be added in plugin_init() (or some other forward if necessary).

PHP Code:
public plugin_init()
    
CC_SetPrefix("&x04[X-Servers]"
If you want to remove the prefix, you can use the function CC_RemovePrefix.

---------- [ Removing Colors ] ----------


To remove the color codes from a message:

PHP Code:
CC_RemoveColors(szMessage[], iLenbool:bChat truebool:bMenu false
If bChat = true, it will remove the codes for chat colors.
If bMenu = true, it will remove the codes for menu colors.

---------- [ View the API ] ----------

---------- [ Download ] ----------
__________________

Last edited by OciXCrom; 07-22-2018 at 16:21.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 03-14-2017 , 16:59   Re: [INC] CromChat
Reply With Quote #2

If we are on this let's break that horrible design which is present in almost every colorchat. Have a custom function to send the message and separat the single-player case from the array so you don't have to call is_user_connected for nothing.

PHP Code:
if(id)
{
    
SendMessage()
}
else
{
    
get_players(iPlayersiCount"ch")
    
    if(!
iCount)
        return

    
loop
    
{
        
SendMessage
    
}


__________________

Last edited by HamletEagle; 03-14-2017 at 17:00.
HamletEagle is offline
[email protected]
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 03-14-2017 , 20:57   Re: [INC] CromChat
Reply With Quote #3

With this you can't write just "!g" (without coloring), so you can replace "!!" with "!", but this would break phrases with "!!!", better to use something that is rarely used, like {}, so you can use it as "{g}", "{r}", etc. Or with full color naming "{green}", "{red}", etc.
static szMessage[192] this can lead server crash, if string would have 191 length. You should use 192 - 1 (for write_byte) = 191.
You can reserve TeamInfos for TR, CT and SPECTATOR on high-slots like 33-35 or 61-63 or 49-51, etc.
PHP Code:
            message_begin(MSG_ONE_UNRELIABLECC_MSG_SAYTEXT_iPlayer)
            
write_byte(iPlayer)
            
write_string(szMessage)
            
message_end() 
This is unsafe, if message contains something like "#Spec_Help_Text" it would show localized string, if message contains percents or something like "%s" it can show something strange and "%s0" can crash client. So, the correct way is:
PHP Code:
            message_begin(MSG_ONE_UNRELIABLECC_MSG_SAYTEXT_iPlayer)
            
write_byte(iPlayer)
            
write_string("%s")
            
write_string(szMessage)
            
message_end() 
And szMessage size now should be 192 - 1 (for write_byte) - 3 (for write_string("%s")) = 188.
Also why is it unreliable? And also it doesn't support ML for sending to all players.

See my ChatPrint
__________________

Last edited by [email protected]; 03-14-2017 at 21:02.
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
EFFx
Veteran Member
Join Date: Feb 2016
Location: SP, Brazil
Old 03-14-2017 , 21:17   Re: [INC] CromChat
Reply With Quote #4

I agree with {green} and {red}, will be much better.
__________________
EFFx is online now
Send a message via Skype™ to EFFx
Old 03-15-2017, 14:53
OciXCrom
This message has been deleted by OciXCrom. Reason: O-m-g. Just saw what I did there.
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
[email protected]
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 03-15-2017 , 16:36   Re: [INC] CromChat
Reply With Quote #6

Quote:
Originally Posted by OciXCrom View Post
I changed szMessage to have 191 characters.
I prevented the localized strings and %s bugs.
Quote:
Originally Posted by [email protected] View Post
And szMessage size now should be 192 - 1 (for write_byte) - 3 (for write_string("%s")) = 188.
Quote:
Originally Posted by OciXCrom View Post
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).
It can be done via macro:
PHP Code:
#defined PrefixedCromChat(%0,%1,%2) CromChat(%0, fmt("%s %s", YourPrefix, %1), %2) 
Quote:
Originally Posted by OciXCrom View Post
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?
There is a problem with your TeamInfo sending: you should restore it, but it isn't always trivial, because some plugins can change TeamInfo without changing actual team. You can't use MSG_ALL for SayText here, it would break ML support at all. You also can't use MSG_ALL for TeamInfo here, because MSG_ALL messages are sent after all MSG_ONE messages. (MSG_BROADCAST after MSG_ONE_UNRELIABLE too, order of MSG_ONE and MSG_ONE_UNRELIABLE messages is unstable)
Quote:
Originally Posted by OciXCrom View Post
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.
You can create escape function for this as I created for my ChatPrint - ChatPrint_EscapeString. But I think better and simpler (for user of your API) to read colors only from format string (including ML formats too), but this requires some untrivial workaround. 01, 02, 03, ... codes are also safe, because name can't contain it, but chat messages which are sent via "say" can contain them, so...
Quote:
Originally Posted by OciXCrom View Post
{green} style is way too long.
You can make code that would support both {g} and {green}.
Quote:
Originally Posted by OciXCrom View Post
By the way 2 - what's the difference between MSG_ONE and MSG_ONE_UNRELIABLE? Which one should I use?
In most cases you should use reliable channel - MSG_ONE and MSG_ALL, but in most temp entity messages you should use unreliable channel.
__________________
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
shehzad1234
Senior Member
Join Date: Jan 2016
Old 03-16-2017 , 06:25   Re: [INC] CromChat
Reply With Quote #7

w0w @OciXCrom
__________________
Contact : Facebook
All Join -=}BBI{=- Hns Server :- 103.50.161.8:10001
shehzad1234 is offline
shehzad1234
Senior Member
Join Date: Jan 2016
Old 03-16-2017 , 06:28   Re: [INC] CromChat
Reply With Quote #8

@OciXCrom bro can u add ^4 for Green ^3 for TeamColor ^1 for Normal ^5Red ^6Blue ^2 for White ??

what u think??
__________________
Contact : Facebook
All Join -=}BBI{=- Hns Server :- 103.50.161.8:10001
shehzad1234 is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Traphouse
Old 03-16-2017 , 08:30   Re: [INC] CromChat
Reply With Quote #9

Quote:
Originally Posted by shehzad1234 View Post
@OciXCrom bro can u add ^4 for Green ^3 for TeamColor ^1 for Normal ^5Red ^6Blue ^2 for White ??

what u think??
__________________
Knife Bounty
CS Battle Royale (PUBG)
Contact/Ticket System

Tournament Organizer Plugin PM if interested
edon1337 is offline
Send a message via Skype™ to edon1337
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 03-16-2017 , 08:40   Re: [INC] CromChat
Reply With Quote #10

@shehzad1234 - I think you should read all previous comments before suggesting it.

I updated the file in the first post and I think it's ready now. I changed the color symbols from !g to &x04. I don't want to use ^x04 style because this one can't be used inside ML/cfg/ini files. So I think the current one is the best solution.

&x07 = red
&x06 = blue
&x05 = white
&x04 = green
&x03 = team color
&x01 = normal


I also improved one more thing that bugged me - when I want to send a chat message and log it at the same time, I had to replace all color symbols in order to do it. So, I added CC_LogMessage, which will simultaneously send a chat message and log it.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 23:16.


Powered by vBulletin®
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Theme made by Freecode