Chat Filter example
Posted: 05 Jan 2012, 16:45
Here's a simple code that adds '#' sign in the begging of every text message. Its purpose is to show how to use the new event feature. Now you can create a bad word, spam or any other kind of text filter.
- Code: Select all
using System;
namespace MCDzienny
{
public class CmdChatFilter : Command
{
// Don't change it.
public override string name { get { return ""; } }
public override string shortcut { get { return ""; } }
public override string type { get { return ""; } }
public override bool museumUsable { get { return false; } }
public override LevelPermission defaultRank { get { return LevelPermission.Banned; } }
// Init is executed when the code is loaded.
public override void Init()
{
// You tell the server to send arguments to FilterChat method when PlayerChatEvent occures.
Player.PlayerChatEvent += FilterChat;
}
// Here's the place where you want to write your filter code.
// You get Player object that describes the player who tries to send a message,
// string message is the message that is about to be send,
// bool stopIt is false by default, and if you set it to true the message will not be send.
private void FilterChat(Player p, ref string message, ref bool stopIt)
{
// You may, but you don't have to copy 'message' value to local variable.
// You may as well perform changes on 'message'.
string changedMessage = message;
// Hash sign is added to the beginning of the message.
changedMessage = "#" + changedMessage;
// 'message' value is now changed to 'changedMessage' value.
message = changedMessage;
}
// Don't change it.
public override void Help(Player p) { }
public override void Use(Player p, string message) { }
}
}