using System; using System.Collections.Generic; using System.Linq; using Facepunch; using Newtonsoft.Json; using Oxide.Core; using Oxide.Core.Libraries.Covalence; namespace Oxide.Plugins { [Info("Prefix Visibility", "Reheight", "1.0.1")] [Description("Allows players to prevent their titles from showing in chat.")] class PrefixVisibility : CovalencePlugin { #region Global Declarations private PluginConfig _config; private List _hiddenPlayers = Pool.GetList(); #endregion #region Configuration Management protected override void LoadDefaultConfig() => _config = GetDefaultConfig(); protected override void SaveConfig() => Config.WriteObject(_config); protected override void LoadConfig() { base.LoadConfig(); try { _config = Config.ReadObject(); if (_config == null) { throw new JsonException(); } if (!_config.ToDictionary().Keys.SequenceEqual(Config.ToDictionary(x => x.Key, x => x.Value).Keys)) { PrintWarning($"PluginConfig file {Name}.json updated."); SaveConfig(); } } catch { LoadDefaultConfig(); PrintError("Config file contains an error and has been replaced with the default file."); } } private class PluginConfig { [JsonProperty(PropertyName = "Prefix", Order = 0)] public string Prefix { get; set; } [JsonProperty(PropertyName = "Commands", Order = 1)] public string[] ToggleCommands { get; set; } [JsonProperty(PropertyName = "Permission Required", Order = 2)] public bool PermissionRequired { get; set; } [JsonProperty(PropertyName = "Permission", Order = 3)] public string Permission { get; set; } [JsonProperty(PropertyName = "Remove Only Listed", Order = 4)] public bool BlacklistedEnabled { get; set; } [JsonProperty(PropertyName = "Remove Titles", Order = 5)] public string[] BlacklistedTitles { get; set; } public string ToJson() => JsonConvert.SerializeObject(this); public Dictionary ToDictionary() => JsonConvert.DeserializeObject>(ToJson()); } private PluginConfig GetDefaultConfig() { return new PluginConfig { Prefix = "Prefix Visibility:", ToggleCommands = new string[] { "tag", "prefix", "toggleprefix", "toggletag", "hideprefix", "hidetag" }, PermissionRequired = false, Permission = "prefixvisibility.use", BlacklistedEnabled = true, BlacklistedTitles = new string[] { "[Owner]", "[Administrator]", "[Moderator]" } }; } #endregion #region Data Management private void LoadData() { _hiddenPlayers.Clear(); var data = Interface.Oxide.DataFileSystem.GetDatafile("PrefixVisibility"); if (data["hiddenPlayers"] != null) { List hiddenPlayers = (List)data["hiddenPlayers"]; foreach (string steamid in hiddenPlayers) { _hiddenPlayers.Add(steamid); } } } private void SaveData() { var data = Interface.Oxide.DataFileSystem.GetDatafile("PrefixVisibility"); data["hiddenPlayers"] = _hiddenPlayers; Interface.Oxide.DataFileSystem.SaveDatafile("PrefixVisibility"); } #endregion #region Language Management protected override void LoadDefaultMessages() { lang.RegisterMessages(new Dictionary { ["Enabled"] = "Your tags will now show in chat.", ["Disabled"] = "Your tags will no longer show in chat.", ["NoPermission"] = "You do not have the permissions required to use this!" }, this); } private string Lang(string key, string pid, params object[] args) => _config.Prefix + " " + String.Format(lang.GetMessage(key, this, pid), args); #endregion private void Init() { if (_config.PermissionRequired) permission.RegisterPermission(_config.Permission, this); AddCovalenceCommand(_config.ToggleCommands, nameof(ToggleCommand)); LoadData(); } private void Unload() { SaveData(); Pool.FreeList(ref _hiddenPlayers); } #region ToggleCommand private void ToggleCommand(IPlayer player, string command, string[] args) { if (_config.PermissionRequired && !permission.UserHasPermission(player.Id, _config.Permission)) { player.Reply(Lang("NoPermissions", player.Id)); return; } if (_hiddenPlayers.Contains(player.Id)) { player.Reply(Lang("Enabled", player.Id)); _hiddenPlayers.Remove(player.Id); return; } else { player.Reply(Lang("Disabled", player.Id)); _hiddenPlayers.Add(player.Id); } } #endregion #region BetterChat Handling private object OnBetterChat(Dictionary data) { List titles = (List)data["Titles"]; IPlayer player = (IPlayer)data["Player"]; if (!titles.Any() || player == null) return null; if (!_hiddenPlayers.Contains(player.Id)) return null; if (_config.BlacklistedEnabled) { foreach (var title in titles.ToList()) { var formattedTitle = Formatter.ToPlaintext(title); if (!_config.BlacklistedTitles.Contains(formattedTitle)) { continue; } titles.Remove(title); } } else data["Titles"] = new string[] { }; return data; } #endregion } }