Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.19 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6.  
  7. using Oxide.Core;
  8. using Oxide.Core.Libraries.Covalence;
  9.  
  10. using Newtonsoft.Json;
  11. using UnityEngine;
  12.  
  13. namespace Oxide.Plugins
  14. {
  15.     [Info("ElegantChat",  "ice cold", "1.0.0")]
  16.     [Description("Elegant chat plugin with many features and api support")]
  17.     public class ElegantChat : CovalencePlugin
  18.     {
  19.         #region Fields
  20.         private PluginConfig config = new PluginConfig();
  21.  
  22.         private readonly Dictionary<ulong, ushort> Warnings = new Dictionary<ulong, ushort>();
  23.         private readonly Dictionary<ulong, double> Cooldown = new Dictionary<ulong, double>();
  24.         private Dictionary<ulong, List<RankProps>> PlayerRanks = new Dictionary<ulong, List<RankProps>>();
  25.        
  26.  
  27.         #endregion
  28.  
  29.         #region Oxide Hooks
  30.         void Init()
  31.         {
  32.             config = Config.ReadObject<PluginConfig>();
  33.             PlayerRanks = Interface.Oxide.DataFileSystem.ReadObject<Dictionary<ulong, List<RankProps>>>("ElegantChat_Players");
  34.         }
  35.         protected override void LoadDefaultConfig()
  36.         {
  37.             Config.WriteObject(config.Init(), true);
  38.         }
  39.         void OnUserConnected(IPlayer player)
  40.         {
  41.             ulong id = ulong.Parse(player.Id);
  42.  
  43.             if(!PlayerRanks.ContainsKey(id))
  44.             {
  45.                 var drank = GetDefaultRank();
  46.  
  47.                 if (drank != null)
  48.                     PlayerRanks.Add(id, new List<RankProps> { drank });
  49.                 else
  50.                     PrintWarning("Failed to give default rank \"{0}\" to {1} because it doesnt exist", config.Ranks.DefaultRank, player.Name);
  51.             }
  52.  
  53.             var rank = GetPlayerRank(id);
  54.             player.Name = string.Format("[{0}]{1} {2}", rank.Prefix, player.Name, rank.ChatModifier);
  55.         }
  56.  
  57.         object OnUserChat(IPlayer player, string message)
  58.         {
  59.             ulong id = ulong.Parse(player.Id);
  60.            
  61.             if(config.Chat.AntiSpam && Cooldown.ContainsKey(id))
  62.             {
  63.                 double calc = TimeSpan.FromTicks(DateTime.Now.Ticks).TotalSeconds - Cooldown[id];
  64.  
  65.                 if (calc < config.Chat.ChatCooldown)
  66.                 {
  67.                     player.Reply(string.Format(lang.GetMessage("SpamMessage", this, id.ToString()), Math.Round(Math.Abs(calc - config.Chat.ChatCooldown))));
  68.                     return null;
  69.                 }
  70.             }
  71.  
  72.             if (config.Chat.AntiCaps) RemoveCapital(ref message);
  73.             GrammarFix(ref message);
  74.             if (config.Chat.Normalize) message.Normalize(NormalizationForm.FormC);
  75.             if (config.Chat.ChatFilter)
  76.             {
  77.                 foreach(var word in config.Chat.Words)
  78.                 {
  79.                     if (message.ToLower().Contains(word.ToLower()))
  80.                     {
  81.                         player.Reply(lang.GetMessage("BadWordMessage", this, id.ToString()));
  82.                         char[] arr = message.ToCharArray();
  83.                         foreach (char x in arr)
  84.                             message.Replace(x, config.Chat.FilterLiteral);
  85.                     }
  86.                 }  
  87.             }
  88.             var rank = GetPlayerRank(id);
  89.             message = string.Format("{0}{1}", rank.ChatColor, message);
  90.  
  91.             if(config.Chat.DistanceChat)
  92.             {
  93.                 player.Message(message, player.Name);
  94.                 foreach(var pl in players.Connected)
  95.                 {
  96.                     var loc = pl.Position();
  97.                     var loc2 = pl.Position();
  98.                     float dist = Vector3.Distance(new Vector3(loc.X, loc.Y, loc.Z), new Vector3(loc2.X, loc2.Y, loc2.Z));
  99.  
  100.                     if(dist <= config.Chat.ChatDistance)
  101.                     {
  102.                         pl.Reply(message, player.Name);
  103.                     }
  104.                 }
  105.                 return null;
  106.             }
  107.             return message;
  108.         }
  109.  
  110.        
  111.  
  112.         protected override void LoadDefaultMessages()
  113.         {
  114.             lang.RegisterMessages(new Dictionary<string, string>
  115.             {
  116.                 {"SpamMessage", "You're under chat-cooldown for another {0} seconds." },
  117.                 {"BanMessage", "You've been banned for bad words." },
  118.                 {"KickMessage", "You've been kicked for bad words." },
  119.                 {"BadWordMessage", "HEY!, Thats a naughty word, please keep the chat clean." }
  120.             }, this);
  121.             lang.RegisterMessages(new Dictionary<string, string>
  122.             {
  123.                 {"SpamMessage", "Je kan niet praten voor {0} seconden." },
  124.                 {"BanMessage", "Je bent verbannen voor het gebruik van slechte woorden." },
  125.                 {"KickMessage", "Je bent van de server getrapt voor het gebruik van slechte woorden." },
  126.                 {"BadWordMessage", "HEE!. Dat is een lelijk woord, houd de chat schoon A.U.B." }
  127.             }, this, "nl");
  128.             lang.RegisterMessages(new Dictionary<string, string>
  129.             {
  130.                 {"SpamMessage", "Estás bajo enfriamiento por otros {0} segundos." },
  131.                 {"BanMessage", "Has sido prohibido por malas palabras." },
  132.                 {"KickMessage", "Has sido expulsado del servidor por usar malas palabras." },
  133.                 {"BadWordMessage", "HEY !, esa es una palabra traviesa, por favor mantengan el chat limpio" }
  134.             }, this, "sp");
  135.             lang.RegisterMessages(new Dictionary<string, string>
  136.             {
  137.                 {"SpamMessage", "Вы отправляете сообщения слишком часто! Подождите еще {0} секунд(у)." },
  138.                 {"BanMessage", "Вы были заблокированы за использование запрещенных слов." },
  139.                 {"KickMessage", "Вы были отключены за использование запрещенных слов." },
  140.                 {"BadWordMessage", "Это слово запрещено в чате!" }
  141.             }, this, "ru");
  142.         }
  143.         #endregion
  144.  
  145.         #region ElegantChat Hooks
  146.  
  147.         private RankProps GetDefaultRank()
  148.         {
  149.             return config.Ranks.Ranks.FirstOrDefault(x => x.Name == config.Ranks.DefaultRank);
  150.         }
  151.         private RankProps GetRankByName(string name)
  152.         {
  153.             return config.Ranks.Ranks.FirstOrDefault(x => x.Name == name);
  154.         }
  155.         private RankProps GetPlayerRank(ulong id)
  156.         {
  157.             var dict = PlayerRanks[id].OrderByDescending(x => x.AuthLevel);
  158.  
  159.             return config.Ranks.Ranks.FirstOrDefault(x => x.AuthLevel == dict.Max().AuthLevel);
  160.         }
  161.  
  162.         private IPlayer FindPlayer(IPlayer player, string partialName)
  163.         {
  164.             IEnumerable<IPlayer> matches = (from pl in players.Connected
  165.                                             where pl.Name.ToLower().Contains(partialName.ToLower())
  166.                                             select pl);
  167.             if (matches.Count() == 0)
  168.             {
  169.  
  170.                 return null;
  171.             }
  172.             if (matches.Count() == 1)
  173.                 return matches.First();
  174.  
  175.         }
  176.  
  177.         private void RemoveCapital(ref string message)
  178.         {
  179.             char[] arr = message.ToCharArray();
  180.  
  181.             for(int i = 0; i < arr.Length; i++)
  182.             {
  183.                 if(char.IsUpper(arr[i]))
  184.                 {
  185.                     message = message.ToLower();
  186.                 }
  187.             }
  188.         }
  189.  
  190.         private void GrammarFix(ref string message)
  191.         {
  192.             if(message.Length != 0)
  193.             {
  194.                 char first = message.ToCharArray()[0];
  195.                 if (char.IsDigit(first)) return;
  196.                 message = message.Substring(0, 0).Replace(first, char.ToUpper(first));
  197.             }
  198.            
  199.         }
  200.  
  201.         public bool HasPermission(ulong id, ushort level)
  202.         {
  203.             var rank = GetPlayerRank(id);
  204.  
  205.             return rank?.AuthLevel >= level;
  206.         }
  207.         #endregion
  208.  
  209.         [Serializable]
  210.         class PluginConfig
  211.         {
  212.             [JsonProperty("Chat configuration")]
  213.             public ChatConfig Chat;
  214.             [JsonProperty("Ranks configuration")]
  215.             public RanksConfig Ranks;
  216.  
  217.             public PluginConfig Init()
  218.             {
  219.                 Chat = new ChatConfig().Init();
  220.                 Ranks = new RanksConfig().Init();
  221.                 return this;
  222.             }
  223.         }
  224.        
  225.         [Serializable]
  226.         class ChatConfig
  227.         {
  228.             [JsonProperty("Enable anti spam?")]
  229.             public bool AntiSpam;
  230.             [JsonProperty("How many seconds should be between each players chat message?")]
  231.             public double ChatCooldown;
  232.  
  233.             [JsonProperty("Enable chat distance?")]
  234.             public bool DistanceChat;
  235.             [JsonProperty("Chat distance in meters")]
  236.             public float ChatDistance;
  237.  
  238.             [JsonProperty("Enable chat filter?")]
  239.             public bool ChatFilter;
  240.             [JsonProperty("Normalize the chat message?")]
  241.             public bool Normalize;
  242.             [JsonProperty("Anti Capslock?")]
  243.             public bool AntiCaps;
  244.             [JsonProperty("Ban the player when reaching the maximum warnings? (Disabled = kick)")]
  245.             public bool BanPlayer;
  246.             [JsonProperty("Should we remove the message instead of replacing it?")]
  247.             public bool RemoveMessage;
  248.             [JsonProperty("How many warnings before we ban the player?")]
  249.             public ushort MaxWarnings;
  250.             [JsonProperty("The literal that wil be used instead of the badword")]
  251.             public char FilterLiteral;
  252.             [JsonProperty("The list of bad words")]
  253.             public string[] Words;
  254.  
  255.             public ChatConfig Init()
  256.             {
  257.                 AntiSpam = true;
  258.                 ChatCooldown = 3;
  259.  
  260.                 DistanceChat = false;
  261.                 ChatDistance = 200f;
  262.  
  263.                 ChatFilter = true;
  264.                 Normalize = true;
  265.                 AntiCaps = true;
  266.                 BanPlayer = true;
  267.                 RemoveMessage = false;
  268.                 MaxWarnings = 5;
  269.                 FilterLiteral = '*';
  270.                 Words = new string[]
  271.                 {
  272.                     "Cancer",
  273.                     "Aids",
  274.                     "Retard",
  275.                     "Ebola",
  276.                     "Nigger",
  277.                     "Negro",
  278.                     "Cunt"
  279.                 };
  280.                 return this;
  281.             }
  282.         }
  283.  
  284.         [Serializable]
  285.         class RanksConfig
  286.         {
  287.             [JsonProperty("The default rank any player has")]
  288.             public string DefaultRank;
  289.             [JsonProperty("The master admin rank")]
  290.             public string MasterAdmin;
  291.             [JsonProperty("The list of available ranks")]
  292.             public RankProps[] Ranks;
  293.  
  294.             public RanksConfig Init()
  295.             {
  296.                 DefaultRank = "Default";
  297.                 MasterAdmin = "Owner";
  298.  
  299.                 Ranks = new RankProps[]
  300.                 {
  301.                     new RankProps("Default", "Wanderer", string.Empty, '✌', 0),
  302.                     new RankProps("Donator", "Donator", string.Empty, '⌘', 1),
  303.                     new RankProps("VIP", "VIP", string.Empty, '☤', 2),
  304.                     new RankProps("Moderator", "Mod", string.Empty, '✪', 3),
  305.                     new RankProps("Admin", "Admin", string.Empty, '★', 4),
  306.                     new RankProps("Owner", "Owner", string.Empty, '♛', 5)
  307.                 };
  308.                 return this;
  309.             }
  310.         }
  311.  
  312.         [Serializable]
  313.         class RankProps
  314.         {
  315.             public string Name;
  316.             public string Prefix;
  317.             public string ChatColor;
  318.             public char ChatModifier;
  319.  
  320.             public ushort AuthLevel;
  321.  
  322.             public RankProps(string name, string prefix, string chatcolor, char modifier, ushort level)
  323.             {
  324.                 this.Name = name;
  325.                 this.Prefix = prefix;
  326.                 this.ChatColor = chatcolor;
  327.                 this.ChatModifier = modifier;
  328.                 this.AuthLevel = level;
  329.             }
  330.         }
  331.     }
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement