Solved Modify chat messages [C#]

Discussion in 'Rust Development' started by Maniah, Jun 16, 2015.

  1. Is it possible to modify chat messages before they are sent (C#)?

    Thank you,

    Maniah
     
  2. Wulf

    Wulf Community Admin

  3. I tried that before asking the question. But I can't find any C# examples. All the mods using this seem to be writtein in LUA.
     
  4. Pseudo code:
    Code:
    bool OnPlayerChat(ConsoleSystem.Arg arg)
    {
        BasePlayer player = PlayerFromArg;
        string message = MessageFromArg;
        string userId = UserIdFromPlayer;
        WriteInChat(DisplayNameFromPlayer, message, userId);
        blockMessage;
    }
    
     
    Last edited by a moderator: Jun 16, 2015
  5. Ended up with:
    Code:
            [HookMethod("OnPlayerChat")]
            object OnPlayerChat(ConsoleSystem.Arg arg)
            {
                string playerChat = arg.GetString(0, "text");
                BasePlayer player = arg.connection.player as BasePlayer;            string result = "";
                string playerColor = "#cccccc";            string factionColor = allianceColor;            if (player != null && playerChat != null)
                {                    result = string.Format("<color={0}>{1}</color>: {2}", playerColor, player.displayName, playerChat);
                        PrintToChat(result);
                        return "handled";
                    }
                }        }
     
  6. Does not look bad for now. But I recommend you simply returning false.
    Also I don't see the reason for
    string factionColor = allianceColor;
    The rest seems fine
     
  7. You do not need to use the HookMethod attribute in plugins :)
    Some redundancy here and there but should work, only part I'm not sure of is the arg.GetString part for when the user sends a message which has quotes in it, not sure if it will still see everything as a single argument or if it starts a second one, so that's something you'll need to test.