Solved Add a prefix?

Jakkee

Retired Staff
Retired Staff
Plugin Developer
Jul 28, 2014
1,465
932
113
Australia
Looking to add a prefix, This what I have at the moment;
JavaScript:
function On_PluginInit() {
    if (!Plugin.IniExists("Settings")){
        var ini = Plugin.CreateIni("Settings");
        ini.AddSetting("Settings", "AdminChatColour", "[color red]");
    }
    DataStore.Add("Prefix", "AdminChatColour", ini.GetSetting("Settings", "AdminChatColour"));
}

var Prefix = {
    get AdminChatColour () { return DataStore.Get("Prefix", "AdminChatColour");}
}

function On_Chat(Player, ChatMessage) {
    if(Player.Admin)){
        var ChatMSG = ChatMessage.toString();
        var ChatColour = Prefix.AdminChatColour;
        Server.BroadcastFrom("[Admin]  " + Player.Name, ChatColour ChatMSG);
        ChatMessage.NewText = ""
    return;
    }
}
So here is the error in the console:
Code:
Failed to invoke Method ConsoleNetworker:SV_RunConsoleCommand with 2 parameters:
chat.say "test", Sender: Player Client (113), timestamp: 10508.713
System.Reflection.TargetInvocationException: Exception has been thrown by the ta
rget of an invocation. ---> System.Reflection.TargetInvocationException: Excepti
on has been thrown by the target of an invocation. ---> System.ArgumentOutOfRang
eException: Cannot be negative.
Parameter name: length
  at System.String.Substring (Int32 startIndex, Int32 length) [0x00000] in <file
name unknown>:0
  at Fougerite.Hooks.ChatReceived (.Arg& arg) [0x00000] in <filename unknown>:0

  at chat.say (.Arg& arg) [0x00000] in <filename unknown>:0
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (ob
ject,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invoke
Attr, System.Reflection.Binder binder, System.Object[] parameters, System.Global
ization.CultureInfo culture) [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invoke
Attr, System.Reflection.Binder binder, System.Object[] parameters, System.Global
ization.CultureInfo culture) [0x00000] in <filename unknown>:0
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] par
ameters) [0x00000] in <filename unknown>:0
  at ConsoleSystem.RunCommand (.Arg& arg, Boolean bWantReply) [0x00000] in <file
name unknown>:0
  at ConsoleSystem.RunCommand_Serverside (System.String strCommand, .NetUser use
r, System.String& StrOutput) [0x00000] in <filename unknown>:0
  at ConsoleNetworker.SV_RunConsoleCommand (System.String cmd, uLink.NetworkMess
ageInfo info) [0x00000] in <filename unknown>:0
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (ob
ject,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invoke
Attr, System.Reflection.Binder binder, System.Object[] parameters, System.Global
ization.CultureInfo culture) [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invoke
Attr, System.Reflection.Binder binder, System.Object[] parameters, System.Global
ization.CultureInfo culture) [0x00000] in <filename unknown>:0
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] par
ameters) [0x00000] in <filename unknown>:0
  at #=qaL2AMBCaBLCj4lGSCwjcBVYUs_5vzeQVgu44HVklVYQ=.#=qG_E6hqIvz6NLWQQCT2N5IA==
(System.Object #=qHU1X5mvYu3s1KCG5AbpxtA==, System.Object[] #=qkZ6vhdfn7DFsLIYE
dsMCpg==) [0x00000] in <filename unknown>:0
I also have this that works but it changes the players name & it doesn't add a colour.
JavaScript:
function On_PlayerConnected(Player){
    if (Player.Admin){
        Player.Name = "[Admin]" +Player.Name
        Player.Message("Your new name is: " +Player.Name);
    }
}
 

balu92

Retired Staff
Retired Staff
Trusted Member
Jul 11, 2014
338
75
28
34
first look:
Server.BroadcastFrom("[Admin] "+ Player.Name, ChatColour ChatMSG);

Server.BroadcastFrom("[Admin] "+ Player.Name, ChatColour + ChatMSG);
 

Jakkee

Retired Staff
Retired Staff
Plugin Developer
Jul 28, 2014
1,465
932
113
Australia
first look:
Server.BroadcastFrom("[Admin] "+ Player.Name, ChatColour ChatMSG);

Server.BroadcastFrom("[Admin] "+ Player.Name, ChatColour + ChatMSG);
Still get the same error, but heres a screenshot from in game
 

balu92

Retired Staff
Retired Staff
Trusted Member
Jul 11, 2014
338
75
28
34
second look:
if(!Plugin.IniExists("Settings")){
var ini = Plugin.CreateIni("Settings");
ini.AddSetting("Settings","AdminChatColour","[color red]");
ini.Save(); // save it
}
var ini = Plugin.GetIni("Settings");
DataStore.Add("Prefix","AdminChatColour", ini.GetSetting("Settings","AdminChatColour")); //in your code, ini exists only in the if () {}
 

Jakkee

Retired Staff
Retired Staff
Plugin Developer
Jul 28, 2014
1,465
932
113
Australia
second look:
if(!Plugin.IniExists("Settings")){
var ini = Plugin.CreateIni("Settings");
ini.AddSetting("Settings","AdminChatColour","[color red]");
ini.Save(); // save it
}
var ini = Plugin.GetIni("Settings");
DataStore.Add("Prefix","AdminChatColour", ini.GetSetting("Settings","AdminChatColour")); //in your code, ini exists only in the if () {}
Yeah i have, Just forget to add to the thread, I have a tone more settings and things. I just removed all the non releated code.
 

balu92

Retired Staff
Retired Staff
Trusted Member
Jul 11, 2014
338
75
28
34
probably [color red] is not supported format, try: [color #ff0000] ?
 

balu92

Retired Staff
Retired Staff
Trusted Member
Jul 11, 2014
338
75
28
34
I don't think ChatString has the java .toString(), as it's not a string itself, it has an overriden c# ToString() that you could use instead or: ChatMessage.origText.toString() or: String(ChatMessage)
 

Jakkee

Retired Staff
Retired Staff
Plugin Developer
Jul 28, 2014
1,465
932
113
Australia
Saddly that doesn't make a difference.
I just tried adding this & has same result
JavaScript:
var JointMSG = ChatColour + ChatMSG;
 

balu92

Retired Staff
Retired Staff
Trusted Member
Jul 11, 2014
338
75
28
34
try this:
JavaScript:
    if(Player.Admin)){
        var ChatColour = Prefix.AdminChatColour;
        Server.BroadcastFrom("[Admin]" + Player.Name, ChatColour + ChatMessage.ToString());
        ChatMessage.NewText = "";
    return;
    }
 

Jakkee

Retired Staff
Retired Staff
Plugin Developer
Jul 28, 2014
1,465
932
113
Australia
try this:
JavaScript:
    if(Player.Admin)){
        var ChatColour = Prefix.AdminChatColour;
        Server.BroadcastFrom("[Admin]" + Player.Name, ChatColour + ChatMessage.ToString());
        ChatMessage.NewText = "";
    return;
    }
Still likes to say undefined.
It has to be with the colour part, But i'v tried:
JavaScript:
        var ChatMSG = ChatMessage.toString();
        Server.BroadcastFrom("[Admin]  " + Player.Name, [color #00ff00] ChatMSG);
        ChatMessage.NewText = ""
Having [color #00ff00] just likes to throw an error xD
 

Jakkee

Retired Staff
Retired Staff
Plugin Developer
Jul 28, 2014
1,465
932
113
Australia
With
JavaScript:
Player.Name = "[Admin]" +Player.Name
is it possible to just have a [Admin] tag in the chat? and not in /help, etc.
EDIT: if it helps im running mikes build.
 

balu92

Retired Staff
Retired Staff
Trusted Member
Jul 11, 2014
338
75
28
34
color must be string:
"[color #00ff00]" + ChatMSG

there is NO: ChatMessage.toString()
there is: ChatMessage.ToString()
 

Jakkee

Retired Staff
Retired Staff
Plugin Developer
Jul 28, 2014
1,465
932
113
Australia
color must be string:
"[color #00ff00]" + ChatMSG

there is NO: ChatMessage.toString()
there is: ChatMessage.ToString()
This here works, Has the colour & chat message but still gives the same error and has " " around the text
JavaScript:
if(Player.Admin){
        var ChatMSG = ChatMessage.ToString()
        //var ChatColour = Prefix.AdminChatColour;
        Server.BroadcastFrom("[Admin]" + Player.Name, "[color #FF0000]" +ChatMSG);
        ChatMessage.NewText = "";
    return;
    }
 

balu92

Retired Staff
Retired Staff
Trusted Member
Jul 11, 2014
338
75
28
34
'ChatMessage.NewText="";' causes that error

var ChatMSG = ChatMessage.ToString().slice(1, ChatMessage.ToString().Length); // to remove the apostrofs