using Newtonsoft.Json; using Oxide.Core.Libraries.Covalence; using Oxide.Game.Rust.Cui; using Oxide.Core; using Oxide.Core.Plugins; using System; using System.Linq; using System.Globalization; using System.Collections.Generic; using UnityEngine; using Rust; namespace Oxide.Plugins { [Info("CMuteChat", "Crazy", "1.0.4")] [Description("Mute/Unmute chat server")] class CMuteChat : RustPlugin { #region Fields private static CMuteChat _ins; private static List muteList = new List(); private Dictionary _data; public class MTimer { public TimeSpan start; public TimeSpan end; public MTimer(int min) { this.start = DateTime.Now.TimeOfDay; this.end = DateTime.Now.TimeOfDay.Add(TimeSpan.FromMinutes(min)); } } #endregion #region Server private void Loaded() { _ins = this; _data = new Dictionary(); permission.RegisterPermission("cmutechat.use", this); foreach (var player in BasePlayer.activePlayerList) OnPlayerConnected(player); } private void Unload() { if (BasePlayer.activePlayerList.Count == 0) return; MuteChat[] objects = UnityEngine.Object.FindObjectsOfType(); if (objects != null) { foreach (var obj in objects) UnityEngine.Object.Destroy(obj); } } private void OnPlayerConnected(BasePlayer player) { if (player.HasPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot) || player.IsSleeping()) { timer.In(2, () => OnPlayerConnected(player)); return; } if (IsMuted() && !player.GetComponent()) _data[player.userID] = player.gameObject.AddComponent(); } private void OnPlayerDisconnected(BasePlayer player) { if(_data.ContainsKey(player.userID)) UnityEngine.Object.Destroy(_data[player.userID]); } private object OnPlayerChat(BasePlayer player, string message, ConVar.Chat.ChatChannel channel) { if (plugins.Exists("BetterChat")) return null; if(player.IsAdmin) return null; if(channel == ConVar.Chat.ChatChannel.Team) return null; if(IsMuted()) return false; return null; } private void OnBetterChat(Dictionary data) { var player = data["Player"] as IPlayer; var message = data["Message"] as string; var channel = data["ChatChannel"].ToString(); if(player.IsAdmin) return; if(channel == "Team") return; if(IsMuted()) data["CancelOption"] = 2; } #endregion #region Component private class MuteChat : FacepunchBehaviour { private BasePlayer player; private TimeSpan time; private void Awake() { player = GetComponent(); time = DateTime.Now.TimeOfDay; CreateUI(true); InvokeRepeating(Refresh, 0f, 1); } private void Refresh() { time = DateTime.Now.TimeOfDay; if(!IsMuted()) { Destroy(this); return; } if(IsFinish(time)) { muteList.Clear(); Interface.CallHook("CMuteChat_End"); _ins.Puts("Chat unmuted"); Destroy(this); return; } DestroyUI(); CreateUI(); } private string GetCooldown() { TimeSpan cooldown = muteList.First().end - time; string result = cooldown.ToString(@"mm\:ss"); return $"Restant • {result}"; } private void CreateUI(bool all = false) { TimeSpan start = muteList.First().start; TimeSpan end = muteList.First().end; string elapsedSeconds = (end - time).TotalSeconds.ToString(); string totalSeconds = (end - start).TotalSeconds.ToString(); float progressCount = (float) float.Parse(elapsedSeconds)/float.Parse(totalSeconds); if(all) { var container = UI.CreateElementContainer("cmutechat.static.txt", HexToRustFormat("#d8453dA1"), "0.925 0.66", "1 0.70"); UI.AddText(ref container, "cmutechat.static.txt", "›› Mute Chat", 15, "robotocondensed-regular.ttf", "1 1 1 0.8", TextAnchor.MiddleLeft, "0.05 0.0", "1.0 1"); UI.AddImageColor(ref container, "cmutechat.static.txt", HexToRustFormat("#000000F1"), "0 0", "1 0", "0 0", "0 5"); CuiHelper.AddUi(player, container); var container2 = UI.CreateElementContainer("cmutechat.statictime.txt", HexToRustFormat("#000000D5"), "0.925 0.63", "1 0.659"); CuiHelper.AddUi(player, container2); } else { var container = UI.CreateElementContainer("cmutechat.progressbar.txt", "0 0 0 0", "0.925 0.66", "1 0.70"); UI.AddImageColor(ref container, "cmutechat.progressbar.txt", HexToRustFormat("#d8453dF1"), "0 0", $"{progressCount} 0", "0 1", "0 4"); CuiHelper.AddUi(player, container); var container2 = UI.CreateElementContainer("cmutechat.progress.txt", "0 0 0 0", "0.925 0.63", "1 0.659"); UI.AddText(ref container2, "cmutechat.progress.txt", GetCooldown(), 11, "robotocondensed-regular.ttf", "1 1 1 0.6", TextAnchor.MiddleCenter, "0.0 0.0", "0.95 1.0"); CuiHelper.AddUi(player, container2); } } private void DestroyUI(bool all = false) { if(all) { CuiHelper.DestroyUi(player, "cmutechat.static.txt"); CuiHelper.DestroyUi(player, "cmutechat.statictime.txt"); CuiHelper.DestroyUi(player, "cmutechat.progressbar.txt"); CuiHelper.DestroyUi(player, "cmutechat.progress.txt"); } else { CuiHelper.DestroyUi(player, "cmutechat.progressbar.txt"); CuiHelper.DestroyUi(player, "cmutechat.progress.txt"); } } private void OnDestroy() => DestroyUI(true); } #endregion #region Functions private bool isNumeric(string valeur) { if (string.IsNullOrEmpty(valeur)) return false; foreach (char ch in valeur.ToCharArray()) if (char.IsLetter(ch)) return false; return true; } private static bool IsMuted() => muteList.Count == 0 ? false : true; private static bool IsFinish(TimeSpan time) => time > muteList.First().end ? true : false; #endregion #region UI static class UI { static public CuiElementContainer CreateElementContainer(string panel, string color, string aMin, string aMax) { var NewElement = new CuiElementContainer() { { new CuiPanel { Image = {Color = color}, RectTransform = {AnchorMin = aMin, AnchorMax = aMax} }, new CuiElement().Parent = "Hud", panel } }; return NewElement; } static public void AddText(ref CuiElementContainer container, string panel, string text, int size, string font, string color, TextAnchor align, string aMin, string aMax) { container.Add(new CuiElement { Name = CuiHelper.GetGuid(), Parent = panel, Components = { new CuiTextComponent { Text = text, FontSize = size, Font = font, Color = color, Align = align }, new CuiRectTransformComponent { AnchorMin = aMin, AnchorMax = aMax } } }); } static public void AddImageColor(ref CuiElementContainer container, string panel, string color, string aMin, string aMax, string oMin, string oMax) { container.Add(new CuiElement { Name = CuiHelper.GetGuid(), Parent = panel, Components = { new CuiImageComponent { Color = color }, new CuiRectTransformComponent { AnchorMin = aMin, AnchorMax = aMax, OffsetMin = oMin, OffsetMax = oMax } } }); } } #endregion #region Helpers private static string HexToRustFormat(string hex) { if (string.IsNullOrEmpty(hex)) hex = "#FFFFFFFF"; var str = hex.Trim('#'); if (str.Length == 6) str += "FF"; if (str.Length != 8) { throw new Exception(hex); throw new InvalidOperationException("Cannot convert a wrong format."); } var r = byte.Parse(str.Substring(0, 2), NumberStyles.HexNumber); var g = byte.Parse(str.Substring(2, 2), NumberStyles.HexNumber); var b = byte.Parse(str.Substring(4, 2), NumberStyles.HexNumber); var a = byte.Parse(str.Substring(6, 2), NumberStyles.HexNumber); Color color = new Color32(r, g, b, a); return string.Format("{0:F2} {1:F2} {2:F2} {3:F2}", color.r, color.g, color.b, color.a); } #endregion #region API [ConsoleCommand("/muteall")] private void API_MuteChat(ConsoleSystem.Arg arg) { if (!arg.IsAdmin) return; API_MuteChat((BasePlayer)null, "muteall", arg.Args); } [ChatCommand("muteall"), Permission("cmutechat.use")] private void API_MuteChat(BasePlayer player, string command, string[] args) { if (args == null || args.Length == 0) return; if (isNumeric(args[0])) { if (IsMuted()) { muteList.Clear(); Unload(); } muteList.Add(new MTimer(min:int.Parse(args[0]))); Interface.CallHook("CMuteChat_Start", args[0]); foreach (var p in BasePlayer.activePlayerList) OnPlayerConnected(p); Puts("Chat muted"); return; } if (args[0].ToLower() == "stop") { if (!IsMuted()) return; Puts("Chat unmuted (cancel)"); muteList.Clear(); Interface.CallHook("CMuteChat_Cancel"); } } private bool API_CMuteChat() => IsMuted(); #endregion } }