Code:private List<string> BadWords;protected override void LoadDefaultConfig() { BadWords = JsonConvert.DeserializeObject<List<string>>(JsonConvert.SerializeObject(Config["BadWords"])); }object OnPlayerChat(ConsoleSystem.Arg arg) { for(int i = 0; i < arg.Args.Length; i++) { if(BadWords.Contains(arg.Args[i])) { PrintToChat(player, "Message ignored!"); return false; } } return null; }I read on MSDN that the object from the list is necessary, but the arg.Args is string.Code:NullReferenceException: Object reference not set to an instance of an object at Oxide.Plugins.Play4Free.OnPlayerChat (.Arg arg) [0x00000] in <filename unknown>:0 at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
Please, help.
Solved Help with anti bad words chat (C#)
Discussion in 'Rust Development' started by SwipoStyle, Sep 11, 2015.
-
Calytic Community Admin Community Mod
Here are the most apparent issues..
1. arg.Args is a zero-based array of strings split by spaces " ". Being that it is zero-based, in order to iterate over every element in the array, you want to change your loop to be less than OR EQUAL TO length (otherwise you will always miss the last word)..
Code:for(int i = 0; i <= arg.Args.Length; i++) {
3. The way you are checking for bad words is slow and easy to bypass. I would suggest looking into alternatives to filter/block messages irrespective of case, spacing, or formatting, etc. This can be very complicated and I suggest looking into the Chat Handler plugin for some guidance..
Note: you can use arg.ArgsStr as an alternative that contains the entire message..Last edited: Sep 12, 2015 -
- Tested. This method called NRE.
- "player" was previously declared
-
I think the issue here is that you do not have the proper signature for OnPlayerChat (unless the documentation is outdated)... your code inside it is irrelevant as it's not even being called.
void OnPlayerChat(NetUser netuser, string message)
{
Puts("OnPlayerChat works!");
} -
-
-
Code:
error CS1061: Type `ConsoleSystem.Arg' does not contain a definition for `ArgStr' and no extension method `ArgStr' of type `ConsoleSystem.Arg' could be found. Are you missing an assembly reference?
-
Code:
void OnPlayerChat(ConsoleSystem.Arg arg) { string message = arg.GetString("text", 0); Puts(message); }
-
Solved.
I fixed problems:
Code:private List<string> BadWords = new List<string>(); // and config loading fixed; object OnPlayerChat(ConsoleSystem.Arg arg) { BasePlayer player = arg.connection.player as BasePlayer; string message = arg.GetString(0); foreach(string item in BadWords) { if(message.Contains(item)) { PrintToChat(player, "Message ignored!"); return false; } } return null; }
Code:error CS1503: Argument `#1' cannot convert `string' expression to type `int'
Last edited by a moderator: Sep 12, 2015 -
-
Calytic Community Admin Community Mod
Code:arg.ArgsStr