1. 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;
    }
    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
    I read on MSDN that the object from the list is necessary, but the arg.Args is string.
    Please, help.
     
  2. Calytic

    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++) {
    2. the "player" variable you're using with PrintToChat does not exist. You need to use either the "arg.connection" with PrintToChat or retrieve the BasePlayer with "BasePlayer.FindByID(arg.connection.userid)".

    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
    1. Tested. This method called NRE.
    2. "player" was previously declared
     
  3. 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!");
    }
     
  4. I am working on the Experimental, not at the Legacy
     
  5. Right, sorry looked up the wrong doc for some reason. That said, the signature still isn't right the return value should be void, not object. This is your problem.
     
  6. 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?
     
  7. Code:
    void OnPlayerChat(ConsoleSystem.Arg arg) 
    {
    string message = arg.GetString("text", 0);
    Puts(message);
    }
     
  8. 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
  9. string message = arg.GetString(0, "text");
     
  10. Calytic

    Calytic Community Admin Community Mod

    Sorry, it's..
    Code:
    arg.ArgsStr