Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Linq;
- using System.Collections.Generic;
- using Oxide.Core;
- using Oxide.Core.Libraries.Covalence;
- using Newtonsoft.Json;
- using UnityEngine;
- namespace Oxide.Plugins
- {
- [Info("ElegantChat", "ice cold", "1.0.0")]
- [Description("Elegant chat plugin with many features and api support")]
- public class ElegantChat : CovalencePlugin
- {
- #region Fields
- private PluginConfig config = new PluginConfig();
- private readonly Dictionary<ulong, ushort> Warnings = new Dictionary<ulong, ushort>();
- private readonly Dictionary<ulong, double> Cooldown = new Dictionary<ulong, double>();
- private Dictionary<ulong, List<RankProps>> PlayerRanks = new Dictionary<ulong, List<RankProps>>();
- #endregion
- #region Oxide Hooks
- void Init()
- {
- config = Config.ReadObject<PluginConfig>();
- PlayerRanks = Interface.Oxide.DataFileSystem.ReadObject<Dictionary<ulong, List<RankProps>>>("ElegantChat_Players");
- }
- protected override void LoadDefaultConfig()
- {
- Config.WriteObject(config.Init(), true);
- }
- void OnUserConnected(IPlayer player)
- {
- ulong id = ulong.Parse(player.Id);
- if(!PlayerRanks.ContainsKey(id))
- {
- var drank = GetDefaultRank();
- if (drank != null)
- PlayerRanks.Add(id, new List<RankProps> { drank });
- else
- PrintWarning("Failed to give default rank \"{0}\" to {1} because it doesnt exist", config.Ranks.DefaultRank, player.Name);
- }
- var rank = GetPlayerRank(id);
- player.Name = string.Format("[{0}]{1} {2}", rank.Prefix, player.Name, rank.ChatModifier);
- }
- object OnUserChat(IPlayer player, string message)
- {
- ulong id = ulong.Parse(player.Id);
- if(config.Chat.AntiSpam && Cooldown.ContainsKey(id))
- {
- double calc = TimeSpan.FromTicks(DateTime.Now.Ticks).TotalSeconds - Cooldown[id];
- if (calc < config.Chat.ChatCooldown)
- {
- player.Reply(string.Format(lang.GetMessage("SpamMessage", this, id.ToString()), Math.Round(Math.Abs(calc - config.Chat.ChatCooldown))));
- return null;
- }
- }
- if (config.Chat.AntiCaps) RemoveCapital(ref message);
- GrammarFix(ref message);
- if (config.Chat.Normalize) message.Normalize(NormalizationForm.FormC);
- if (config.Chat.ChatFilter)
- {
- foreach(var word in config.Chat.Words)
- {
- if (message.ToLower().Contains(word.ToLower()))
- {
- player.Reply(lang.GetMessage("BadWordMessage", this, id.ToString()));
- char[] arr = message.ToCharArray();
- foreach (char x in arr)
- message.Replace(x, config.Chat.FilterLiteral);
- }
- }
- }
- var rank = GetPlayerRank(id);
- message = string.Format("{0}{1}", rank.ChatColor, message);
- if(config.Chat.DistanceChat)
- {
- player.Message(message, player.Name);
- foreach(var pl in players.Connected)
- {
- var loc = pl.Position();
- var loc2 = pl.Position();
- float dist = Vector3.Distance(new Vector3(loc.X, loc.Y, loc.Z), new Vector3(loc2.X, loc2.Y, loc2.Z));
- if(dist <= config.Chat.ChatDistance)
- {
- pl.Reply(message, player.Name);
- }
- }
- return null;
- }
- return message;
- }
- protected override void LoadDefaultMessages()
- {
- lang.RegisterMessages(new Dictionary<string, string>
- {
- {"SpamMessage", "You're under chat-cooldown for another {0} seconds." },
- {"BanMessage", "You've been banned for bad words." },
- {"KickMessage", "You've been kicked for bad words." },
- {"BadWordMessage", "HEY!, Thats a naughty word, please keep the chat clean." }
- }, this);
- lang.RegisterMessages(new Dictionary<string, string>
- {
- {"SpamMessage", "Je kan niet praten voor {0} seconden." },
- {"BanMessage", "Je bent verbannen voor het gebruik van slechte woorden." },
- {"KickMessage", "Je bent van de server getrapt voor het gebruik van slechte woorden." },
- {"BadWordMessage", "HEE!. Dat is een lelijk woord, houd de chat schoon A.U.B." }
- }, this, "nl");
- lang.RegisterMessages(new Dictionary<string, string>
- {
- {"SpamMessage", "Estás bajo enfriamiento por otros {0} segundos." },
- {"BanMessage", "Has sido prohibido por malas palabras." },
- {"KickMessage", "Has sido expulsado del servidor por usar malas palabras." },
- {"BadWordMessage", "HEY !, esa es una palabra traviesa, por favor mantengan el chat limpio" }
- }, this, "sp");
- lang.RegisterMessages(new Dictionary<string, string>
- {
- {"SpamMessage", "Вы отправляете сообщения слишком часто! Подождите еще {0} секунд(у)." },
- {"BanMessage", "Вы были заблокированы за использование запрещенных слов." },
- {"KickMessage", "Вы были отключены за использование запрещенных слов." },
- {"BadWordMessage", "Это слово запрещено в чате!" }
- }, this, "ru");
- }
- #endregion
- #region ElegantChat Hooks
- private RankProps GetDefaultRank()
- {
- return config.Ranks.Ranks.FirstOrDefault(x => x.Name == config.Ranks.DefaultRank);
- }
- private RankProps GetRankByName(string name)
- {
- return config.Ranks.Ranks.FirstOrDefault(x => x.Name == name);
- }
- private RankProps GetPlayerRank(ulong id)
- {
- var dict = PlayerRanks[id].OrderByDescending(x => x.AuthLevel);
- return config.Ranks.Ranks.FirstOrDefault(x => x.AuthLevel == dict.Max().AuthLevel);
- }
- private IPlayer FindPlayer(IPlayer player, string partialName)
- {
- IEnumerable<IPlayer> matches = (from pl in players.Connected
- where pl.Name.ToLower().Contains(partialName.ToLower())
- select pl);
- if (matches.Count() == 0)
- {
- return null;
- }
- if (matches.Count() == 1)
- return matches.First();
- }
- private void RemoveCapital(ref string message)
- {
- char[] arr = message.ToCharArray();
- for(int i = 0; i < arr.Length; i++)
- {
- if(char.IsUpper(arr[i]))
- {
- message = message.ToLower();
- }
- }
- }
- private void GrammarFix(ref string message)
- {
- if(message.Length != 0)
- {
- char first = message.ToCharArray()[0];
- if (char.IsDigit(first)) return;
- message = message.Substring(0, 0).Replace(first, char.ToUpper(first));
- }
- }
- public bool HasPermission(ulong id, ushort level)
- {
- var rank = GetPlayerRank(id);
- return rank?.AuthLevel >= level;
- }
- #endregion
- [Serializable]
- class PluginConfig
- {
- [JsonProperty("Chat configuration")]
- public ChatConfig Chat;
- [JsonProperty("Ranks configuration")]
- public RanksConfig Ranks;
- public PluginConfig Init()
- {
- Chat = new ChatConfig().Init();
- Ranks = new RanksConfig().Init();
- return this;
- }
- }
- [Serializable]
- class ChatConfig
- {
- [JsonProperty("Enable anti spam?")]
- public bool AntiSpam;
- [JsonProperty("How many seconds should be between each players chat message?")]
- public double ChatCooldown;
- [JsonProperty("Enable chat distance?")]
- public bool DistanceChat;
- [JsonProperty("Chat distance in meters")]
- public float ChatDistance;
- [JsonProperty("Enable chat filter?")]
- public bool ChatFilter;
- [JsonProperty("Normalize the chat message?")]
- public bool Normalize;
- [JsonProperty("Anti Capslock?")]
- public bool AntiCaps;
- [JsonProperty("Ban the player when reaching the maximum warnings? (Disabled = kick)")]
- public bool BanPlayer;
- [JsonProperty("Should we remove the message instead of replacing it?")]
- public bool RemoveMessage;
- [JsonProperty("How many warnings before we ban the player?")]
- public ushort MaxWarnings;
- [JsonProperty("The literal that wil be used instead of the badword")]
- public char FilterLiteral;
- [JsonProperty("The list of bad words")]
- public string[] Words;
- public ChatConfig Init()
- {
- AntiSpam = true;
- ChatCooldown = 3;
- DistanceChat = false;
- ChatDistance = 200f;
- ChatFilter = true;
- Normalize = true;
- AntiCaps = true;
- BanPlayer = true;
- RemoveMessage = false;
- MaxWarnings = 5;
- FilterLiteral = '*';
- Words = new string[]
- {
- "Cancer",
- "Aids",
- "Retard",
- "Ebola",
- "Nigger",
- "Negro",
- "Cunt"
- };
- return this;
- }
- }
- [Serializable]
- class RanksConfig
- {
- [JsonProperty("The default rank any player has")]
- public string DefaultRank;
- [JsonProperty("The master admin rank")]
- public string MasterAdmin;
- [JsonProperty("The list of available ranks")]
- public RankProps[] Ranks;
- public RanksConfig Init()
- {
- DefaultRank = "Default";
- MasterAdmin = "Owner";
- Ranks = new RankProps[]
- {
- new RankProps("Default", "Wanderer", string.Empty, '✌', 0),
- new RankProps("Donator", "Donator", string.Empty, '⌘', 1),
- new RankProps("VIP", "VIP", string.Empty, '☤', 2),
- new RankProps("Moderator", "Mod", string.Empty, '✪', 3),
- new RankProps("Admin", "Admin", string.Empty, '★', 4),
- new RankProps("Owner", "Owner", string.Empty, '♛', 5)
- };
- return this;
- }
- }
- [Serializable]
- class RankProps
- {
- public string Name;
- public string Prefix;
- public string ChatColor;
- public char ChatModifier;
- public ushort AuthLevel;
- public RankProps(string name, string prefix, string chatcolor, char modifier, ushort level)
- {
- this.Name = name;
- this.Prefix = prefix;
- this.ChatColor = chatcolor;
- this.ChatModifier = modifier;
- this.AuthLevel = level;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement