C#:
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Libraries.Covalence;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Oxide.Plugins
{
[Info("Chat Cleaner", "Wulf", "1.0.0")]
[Description("Cleans the chat for player(s) when joining the server or on command")]
class ChatCleaner : CovalencePlugin
{
#region Configuration
private Configuration config;
public class Configuration
{
[JsonProperty("Clean chat on connect (true/false)")]
public bool CleanOnConnect { get; set; } = true;
[JsonProperty("Exclude admin from global clean (true/false)")]
public bool ExcludeAdmin { get; set; } = true;
[JsonProperty("Show chat cleaned message (true/false)")]
public bool ShowChatCleaned { get; set; } = false;
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
config = Config.ReadObject<Configuration>();
if (config == null)
{
throw new JsonException();
}
}
catch
{
string configPath = $"{Interface.Oxide.ConfigDirectory}{Path.DirectorySeparatorChar}{Name}";
LogWarning($"Could not load a valid configuration file, creating a new configuration file at {configPath}.json");
Config.WriteObject(config, false, $"{configPath}_invalid.json");
LoadDefaultConfig();
SaveConfig();
}
}
protected override void LoadDefaultConfig() => config = new Configuration();
protected override void SaveConfig() => Config.WriteObject(config);
#endregion Configuration
#region Localization
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
["CommandClean"] = "clean",
["CommandCleanAll"] = "cleanall",
["ChatCleaned"] = "Chat has been cleaned, mmm fresh!",
["NotAllowed"] = "You are not allowed to use the '{0}' command"
}, this);
}
#endregion Localization
#region Initialization
private const string permAll = "chatcleaner.all";
private const string permUse = "chatcleaner.use";
private void Init()
{
permission.RegisterPermission(permAll, this);
permission.RegisterPermission(permUse, this);
AddLocalizedCommand("CommandClean", nameof(CleanCommand));
AddLocalizedCommand("CommandCleanAll", nameof(CleanAllCommand));
if (!config.CleanOnConnect)
{
Unsubscribe(nameof(OnUserConnected));
}
}
#endregion Initialization
#region Chat Cleaning
// TODO: Add support for BetterChat
private void CleanChat(IPlayer player)
{
if (!config.ExcludeAdmin)
{
player.Message(new string('\n', 300));
if (config.ShowChatCleaned)
{
Message(player, "ChatCleaned");
}
}
}
private void OnUserConnected(IPlayer player) => CleanChat(player);
private void CleanAllCommand(IPlayer player, string command, string[] args)
{
if (!player.HasPermission(permAll))
{
Message(player, "NotAllowed", command);
return;
}
foreach (IPlayer target in players.Connected.ToList())
{
CleanChat(target);
}
}
private void CleanCommand(IPlayer player, string command, string[] args)
{
if (!player.HasPermission(permUse) && !player.IsServer)
{
Message(player, "NotAllowed", command);
return;
}
CleanChat(player);
}
#endregion Chat Cleaning
#region Helpers
private void AddLocalizedCommand(string key, string command)
{
foreach (string language in lang.GetLanguages(this))
{
Dictionary<string, string> messages = lang.GetMessages(language, this);
foreach (KeyValuePair<string, string> message in messages.Where(m => m.Key.Equals(key)))
{
if (!string.IsNullOrEmpty(message.Value))
{
AddCovalenceCommand(message.Value, command);
}
}
}
}
private string Lang(string key, string id = null, params object[] args)
{
return string.Format(lang.GetMessage(key, this, id), args);
}
private void Message(IPlayer player, string key, params object[] args)
{
player.Reply(Lang(key, player.Id, args));
}
#endregion Helpers
}
}