Raised This Month: $12 Target: $400
 3% 

[HELP] Set Player's Name Color


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SpirT
Senior Member
Join Date: Sep 2018
Location: Portugal
Old 08-13-2019 , 07:46   [HELP] Set Player's Name Color
Reply With Quote #1

Hey,

Today I tried to do by myself a plugin that changes, player's name color on chat.

I tried this but does not work:

PHP Code:
#pragma semicolon 1

#define DEBUG

#define PLUGIN_AUTHOR "SpirT"
#define PLUGIN_VERSION "1.0"

#include <sourcemod>
#include <sdktools>
#include <colors>

#pragma newdecls required

public Plugin myinfo 
{
    
name "[SpirT] Name Color",
    
author PLUGIN_AUTHOR,
    
description "This plugin allows any player to have permission to change his name in chat!",
    
version PLUGIN_VERSION,
    
url ""
};

public 
void OnPluginStart()
{
    
RegConsoleCmd("sm_nc"Command_NameColor);
}

public 
Action Command_NameColor(int clientint args)
{
    if(
args 1)
    {
        
ReplyToCommand(client"[SM] Use: sm_nc <color> (examples: {red} {blue} {green} ...)");
    }
    
    
char arg[64];
    
GetCmdArg(1argsizeof(arg));
    
    
char file[512];
    
BuildPath(Path_SMfilesizeof(file), "nc.cfg");
    
KeyValues kv = new KeyValues("NC");
    
kv.ImportFromFile(file);
    
    
char sID[64];
    
GetClientAuthId(clientAuthId_Steam2sIDsizeof(sID));
    
    if(
KvJumpToKey(kvsIDtrue))
    {
        
char name[MAX_NAME_LENGTH];
        
char cor[64];
        
GetClientName(clientnamesizeof(name));
        
        
KvGetString(kv"nome"namesizeof(name));
        
KvGetString(kv"cor"corsizeof(cor));
        
        if(!
StrEqual(argcor))
        {
            
KvSetString(kv"cor"arg);
        }
        
        
ReplyToCommand(client"[SM] Sorry, but the color you choosed is already you name's color. Please choose another");
    }
    
    
delete kv;
    return 
Plugin_Handled;
}

public 
Action OnChatMessage(intauthorHandle recipientschar[] namechar[] message)
{
    
char file[512];
    
BuildPath(Path_SMfilesizeof(file), "nc.cfg");
    
    
KeyValues kv = new KeyValues("NC");
    
kv.ImportFromFile(file);
    
    
char sid[64];
    
GetClientAuthId(authorAuthId_Steam2sidsizeof(sid));
    if(
KvJumpToKey(kvsid))
    {
        
char color[64];
        
KvGetString(kv"cor"colorsizeof(color));
        
Format(nameMAX_NAME_LENGTH"%s%s"colorname);
    }

Where is the problem?

Best Regards,

SpirT.
__________________

Last edited by SpirT; 08-15-2019 at 09:53.
SpirT is offline
Mathiaas
Senior Member
Join Date: Aug 2014
Location: Sweden ♥
Old 09-02-2019 , 21:18   Re: [HELP] Set Player's Name Color
Reply With Quote #2

Quickly reading through your code, it's not like your OnChatMessage callback does anything? Sure you format the "name" string but that's not what's being sent out to the clients as the message.

You will probably have to stop your message from going out and send your own, customized one. It's 03 in the morning and tired af, but try this one:

PHP Code:
public Action OnChatMessage(intauthorHandle recipientschar[] namechar[] message)
{
    
char file[512];
    
char msg[200];
    
BuildPath(Path_SMfilesizeof(file), "nc.cfg");
    
    
KeyValues kv = new KeyValues("NC");
    
kv.ImportFromFile(file);
    
    
char sid[64];
    
GetClientAuthId(authorAuthId_Steam2sidsizeof(sid));
    if(
KvJumpToKey(kvsid))
    {
        
char color[64];
        
KvGetString(kv"cor"colorsizeof(color));
        
Format(msgsizeof(msg), "%s%s: %s"colornamemessage);
        
PrintToChatAll(msg); // Change this into "SayText2" if you want the teamcolors in case you want teamchat etc. https://forums.alliedmods.net/showthread.php?t=206539
        
return Plugin_Handled;
    }
    return 
Plugin_Continue;

Mathiaas is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 09-03-2019 , 12:32   Re: [HELP] Set Player's Name Color
Reply With Quote #3

Quote:
Originally Posted by Mathiaas View Post
Quickly reading through your code, it's not like your OnChatMessage callback does anything? Sure you format the "name" string but that's not what's being sent out to the clients as the message.

You will probably have to stop your message from going out and send your own, customized one. It's 03 in the morning and tired af, but try this one:

PHP Code:
public Action OnChatMessage(intauthorHandle recipientschar[] namechar[] message)
{
    
char file[512];
    
char msg[200];
    
BuildPath(Path_SMfilesizeof(file), "nc.cfg");
    
    
KeyValues kv = new KeyValues("NC");
    
kv.ImportFromFile(file);
    
    
char sid[64];
    
GetClientAuthId(authorAuthId_Steam2sidsizeof(sid));
    if(
KvJumpToKey(kvsid))
    {
        
char color[64];
        
KvGetString(kv"cor"colorsizeof(color));
        
Format(msgsizeof(msg), "%s%s: %s"colornamemessage);
        
PrintToChatAll(msg); // Change this into "SayText2" if you want the teamcolors in case you want teamchat etc. https://forums.alliedmods.net/showthread.php?t=206539
        
return Plugin_Handled;
    }
    return 
Plugin_Continue;

OnChatMessage comes from the scp include or a similar chat-processor.
There are several things wrong with this code, especially the reading the keyvalue from file everytime some one types in chat.
There are a ton of custom chat color plugins already out there try using those as examples and write your code off of that.
Mitchell is offline
Mathiaas
Senior Member
Join Date: Aug 2014
Location: Sweden ♥
Old 09-03-2019 , 18:09   Re: [HELP] Set Player's Name Color
Reply With Quote #4

Quote:
Originally Posted by Mitchell View Post
OnChatMessage comes from the scp include or a similar chat-processor.
There are several things wrong with this code, especially the reading the keyvalue from file everytime some one types in chat.
There are a ton of custom chat color plugins already out there try using those as examples and write your code off of that.
I simply changed the output part, didn't really look at anything else.
Mathiaas is offline
SpirT
Senior Member
Join Date: Sep 2018
Location: Portugal
Old 09-04-2019 , 10:51   Re: [HELP] Set Player's Name Color
Reply With Quote #5

Quote:
Originally Posted by Mitchell View Post
OnChatMessage comes from the scp include or a similar chat-processor.
There are several things wrong with this code, especially the reading the keyvalue from file everytime some one types in chat.
There are a ton of custom chat color plugins already out there try using those as examples and write your code off of that.
So "There are several things wrong with this code, especially the reading the keyvalue from file everytime some one types in chat.", what is wrong with it? I have a simple chat processor include. I don't want to publish this plugin, but is there any way to get player message (OnChatMessage) without any chat processor?

Besr regards,

SpirT.
__________________
SpirT is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 09-04-2019 , 21:11   Re: [HELP] Set Player's Name Color
Reply With Quote #6

Quote:
Originally Posted by SpirT View Post
So "There are several things wrong with this code, especially the reading the keyvalue from file everytime some one types in chat.", what is wrong with it? I have a simple chat processor include. I don't want to publish this plugin, but is there any way to get player message (OnChatMessage) without any chat processor?

Besr regards,

SpirT.
Reading from a file everytime some one chats is not a good idea. Cache it internally like custom chat colors does. Loading from file implies the file will be changed by another plugin consistently. I'm not sure on the other issues and could be told wrong but file reading can hault the process causing a delay.

My comment on the chat processor had nothing to do with what was wrong, I was just pointing out that it's a forward from another plugin and you dont have which chat processor your using as an #include

Last edited by Mitchell; 09-04-2019 at 21:12.
Mitchell is offline
SpirT
Senior Member
Join Date: Sep 2018
Location: Portugal
Old 09-05-2019 , 09:00   Re: [HELP] Set Player's Name Color
Reply With Quote #7

Quote:
Originally Posted by Mitchell View Post
Reading from a file everytime some one chats is not a good idea. Cache it internally like custom chat colors does. Loading from file implies the file will be changed by another plugin consistently. I'm not sure on the other issues and could be told wrong but file reading can hault the process causing a delay.

My comment on the chat processor had nothing to do with what was wrong, I was just pointing out that it's a forward from another plugin and you dont have which chat processor your using as an #include
Hey, thanks again for your help. Now about the chat processor, OnChatMessage comes from scp include
Asking in a better way again, is there any way to get client message WITHOUT ANY Chat Processor include?

Regards,

SpirT.
__________________
SpirT is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 09-03-2019 , 13:47   Re: [HELP] Set Player's Name Color
Reply With Quote #8

Just use any chat processor wrapper
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]
Dragokas is offline
qNiGHT
AlliedModders Donor
Join Date: Mar 2019
Location: Romania
Old 09-03-2019 , 14:30   Re: [HELP] Set Player's Name Color
Reply With Quote #9

https://forums.alliedmods.net/showthread.php?t=286913
Try this
Code:
#pragma semicolon 1 

#define DEBUG 

#define PLUGIN_AUTHOR "SpirT" 
#define PLUGIN_VERSION "1.0" 

#include <sourcemod> 
#include <sdktools> 
#include <chat-processor>
#include <colors> 

#pragma newdecls required 

public Plugin myinfo = 
{
	name = "[SpirT] Name Color", 
	author = PLUGIN_AUTHOR, 
	description = "This plugin allows any player to have permission to change his name in chat!", 
	version = PLUGIN_VERSION, 
	url = ""
};

public void OnPluginStart()
{
	RegConsoleCmd("sm_nc", Command_NameColor);
}

public Action Command_NameColor(int client, int args)
{
	if (args < 1)
	{
		ReplyToCommand(client, "[SM] Use: sm_nc <color> (examples: {red} {blue} {green} ...)");
	}
	
	char arg[64];
	GetCmdArg(1, arg, sizeof(arg));
	
	char file[512];
	BuildPath(Path_SM, file, sizeof(file), "nc.cfg");
	KeyValues kv = new KeyValues("NC");
	kv.ImportFromFile(file);
	
	char sID[64];
	GetClientAuthId(client, AuthId_Steam2, sID, sizeof(sID));
	
	if (KvJumpToKey(kv, sID, true))
	{
		char name[MAX_NAME_LENGTH];
		char cor[64];
		GetClientName(client, name, sizeof(name));
		
		KvGetString(kv, "nome", name, sizeof(name));
		KvGetString(kv, "cor", cor, sizeof(cor));
		
		if (!StrEqual(arg, cor))
		{
			KvSetString(kv, "cor", arg);
		}
		
		ReplyToCommand(client, "[SM] Sorry, but the color you choosed is already you name's color. Please choose another");
	}
	
	delete kv;
	return Plugin_Handled;
}

public Action OnChatMessage(int & author, Handle recipients, char[] name, char[] message)
{
	char file[512];
	BuildPath(Path_SM, file, sizeof(file), "nc.cfg");
	
	KeyValues kv = new KeyValues("NC");
	kv.ImportFromFile(file);
	
	char sid[64];
	GetClientAuthId(author, AuthId_Steam2, sid, sizeof(sid));
	if (KvJumpToKey(kv, sid))
	{
		char color[64];
		KvGetString(kv, "cor", color, sizeof(color));
		Format(name, MAXLENGTH_NAME, "%s%s", color, name);
		return Plugin_Changed;
	}
	return Plugin_Handled;
}

Last edited by qNiGHT; 09-03-2019 at 14:31.
qNiGHT is offline
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 11:19.


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