using System; using UnityEngine; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; using Oxide.Core.Plugins; namespace Oxide.Plugins { [Info("Vanish Status", "IIIaKa", "0.1.3")] [Description("The plugin displays invisibility indication in the status bar. Depends on Vanish/BetterVanish and AdvancedStatus plugins.")] class VanishStatus : RustPlugin { //TODO carbon [PluginReference] private Plugin ImageLibrary, AdvancedStatus, Vanish, BetterVanish; #region ~Variables~ private int _imgLibCheck = 0; private bool _imgLibIsLoaded = false; private bool _vanishIsLoaded = false; private bool _statusIsLoaded = false; private string _barID = "VanishStatus"; #endregion #region ~Configuration~ private static Configuration _config; private class Configuration { [JsonProperty(PropertyName = "ImageLibrary Counter Check")] public int ImgLibCounter = 5; [JsonProperty(PropertyName = "Status. Bar - Height")] public int Status_Bar_Height = 26; [JsonProperty(PropertyName = "Status. Bar - Order")] public int Status_Bar_Order = 10; [JsonProperty("Status. Background - Color")] public string Status_Background_Color = "#15AC9D"; [JsonProperty(PropertyName = "Status. Background - Transparency")] public float Status_Background_Transparency = 0.7f; [JsonProperty(PropertyName = "Status. Background - Material(empty to disable)")] public string Status_Background_Material = ""; [JsonProperty("Status. Image - URL")] public string Status_Image_Url = "https://i.imgur.com/3D1JIaU.png"; [JsonProperty(PropertyName = "Status. Image - Sprite(empty to use image from URL)")] public string Status_Image_Sprite = ""; [JsonProperty("Status. Image - Is raw image")] public bool Status_Image_IsRawImage = false; [JsonProperty("Status. Image - Color")] public string Status_Image_Color = "#15AC9D"; [JsonProperty(PropertyName = "Status. Text - Size")] public int Status_Text_Size = 12; [JsonProperty("Status. Text - Color")] public string Status_Text_Color = "#FFFFFF"; [JsonProperty(PropertyName = "Status. Text - Font(https://umod.org/guides/rust/basic-concepts-of-gui#fonts)")] public string Status_Text_Font = "RobotoCondensed-Bold.ttf"; public Oxide.Core.VersionNumber Version; } protected override void LoadConfig() { base.LoadConfig(); try { _config = Config.ReadObject(); if (_config == null) { PrintWarning("Configuration file not found. Creating a new one..."); LoadDefaultConfig(); } else if (_config.Version < Version) { PrintWarning($"Your configuration file version({_config.Version}) is outdated. Updating it to {Version}."); LoadDefaultConfig(); _config.Version = Version; PrintWarning($"The configuration file has been successfully updated to version {_config.Version}!"); } SaveConfig(); } catch { PrintError("Your configuration file contains an error. Using default configuration values."); LoadDefaultConfig(); } } protected override void SaveConfig() => Config.WriteObject(_config); protected override void LoadDefaultConfig() => _config = new Configuration(); #endregion #region ~Language~ protected override void LoadDefaultMessages() { lang.RegisterMessages(new Dictionary { ["MsgText"] = "You are invisible" }, this); lang.RegisterMessages(new Dictionary { ["MsgText"] = "Вы невидимы" }, this, "ru"); } #endregion #region ~Methods~ private void ImgLibCheck() { if (ImageLibrary == null || !ImageLibrary.IsLoaded) { _imgLibCheck++; if (_imgLibCheck >= _config.ImgLibCounter) { PrintError("ImageLibrary appears to be missing or occupied by other plugins load orders. For full plugin functionality, it is recommended to load ImageLibrary plugin.", _config.ImgLibCounter, Name); return; } timer.In(60, ImgLibCheck); if (ImageLibrary == null) PrintWarning("ImageLibrary is NOT loaded! Waiting ImageLibrary..."); else PrintWarning("ImageLibrary appears to be occupied will check again in 1 minute."); return; } _imgLibCheck = 0; _imgLibIsLoaded = true; LoadImgs(); } private void LoadImgs() { if (_config.Status_Image_Url.StartsWithAny(new string[2] { "http://", "https://" })) { Dictionary imgList = new Dictionary() { { $"{Name}_Vanish", _config.Status_Image_Url } }; ImageLibrary?.Call("ImportImageList", Name, imgList, 0UL, true); } } private void SendBar(BasePlayer player) { if (player.IsNpc || !player.IsConnected || !IsInvisible(player)) return; Dictionary parameters = new Dictionary { { "Id", _barID }, { "Plugin", Name }, { "Order", _config.Status_Bar_Order }, { "Height", _config.Status_Bar_Height }, { "Main_Color", _config.Status_Background_Color }, { "Main_Transparency", _config.Status_Background_Transparency }, { "Main_Material", _config.Status_Background_Material }, { "Image", _imgLibIsLoaded && _config.Status_Image_Url.StartsWithAny(new string[2] { "http://", "https://" }) ? $"{Name}_Vanish" : _config.Status_Image_Url }, { "Image_Sprite", _config.Status_Image_Sprite }, { "Is_RawImage", _config.Status_Image_IsRawImage }, { "Image_Color", _config.Status_Image_Color }, { "Text", lang.GetMessage("MsgText", this, player.UserIDString) }, { "Text_Size", _config.Status_Text_Size }, { "Text_Color", _config.Status_Text_Color }, { "Text_Font", _config.Status_Text_Font } }; AdvancedStatus?.Call("CreateBar", player, parameters); } private void UpdateText(BasePlayer player) { if (player.IsNpc || !player.IsConnected || !IsInvisible(player)) return; Dictionary parameters = new Dictionary { { "Id", _barID }, { "Plugin", Name }, { "Text", lang.GetMessage("MsgText", this, player.UserIDString) } }; AdvancedStatus?.Call("UpdateContent", player, parameters); } private void InitPlayers() { if (!_vanishIsLoaded || !_statusIsLoaded) return; Subscribe(nameof(OnPlayerConnected)); Subscribe(nameof(OnPlayerSetInfo)); Subscribe(nameof(OnVanishReappear)); Subscribe(nameof(OnVanishDisappear)); foreach (var player in BasePlayer.activePlayerList) SendBar(player); } private bool IsInvisible(BasePlayer player) { if (Vanish != null) return (bool)Vanish.Call("IsInvisible", player); if (BetterVanish != null) return (bool)BetterVanish.Call("_IsInvisible", player); return false; } #endregion #region ~Hooks~ void OnPlayerConnected(BasePlayer player) => SendBar(player); void OnPlayerSetInfo(Network.Connection connection, string key, string value) { if (key == "global.language" && BasePlayer.FindByID(connection?.userid ?? 0) is BasePlayer player) UpdateText(player); } void OnVanishReappear(BasePlayer player) { if (player != null && player.IsConnected) AdvancedStatus?.Call("DeleteBar", player, _barID, Name); } void OnVanishDisappear(BasePlayer player) { if (player != null) SendBar(player); } void OnPluginLoaded(Plugin plugin) { if (plugin == ImageLibrary) ImgLibCheck(); else if (plugin == Vanish || plugin == BetterVanish || plugin == AdvancedStatus) { _vanishIsLoaded = (Vanish != null && Vanish.IsLoaded) || (BetterVanish != null && BetterVanish.IsLoaded); _statusIsLoaded = AdvancedStatus != null && AdvancedStatus.IsLoaded; InitPlayers(); } } void OnPluginUnloaded(Plugin plugin) { if (plugin.Name == "ImageLibrary") _imgLibIsLoaded = false; else if (plugin.Name == "Vanish" || plugin.Name == "BetterVanish" || plugin.Name == "AdvancedStatus") { _vanishIsLoaded = (Vanish != null && Vanish.IsLoaded) || (BetterVanish != null && BetterVanish.IsLoaded); _statusIsLoaded = AdvancedStatus != null && AdvancedStatus.IsLoaded; if (_vanishIsLoaded && _statusIsLoaded) return; Unsubscribe(nameof(OnPlayerConnected)); Unsubscribe(nameof(OnPlayerSetInfo)); Unsubscribe(nameof(OnVanishReappear)); Unsubscribe(nameof(OnVanishDisappear)); if (!_vanishIsLoaded && _statusIsLoaded) { foreach (BasePlayer player in BasePlayer.activePlayerList) AdvancedStatus?.Call("DeleteBar", player, _barID, Name); } } } void Init() { Unsubscribe(nameof(OnPlayerConnected)); Unsubscribe(nameof(OnPlayerSetInfo)); Unsubscribe(nameof(OnVanishReappear)); Unsubscribe(nameof(OnVanishDisappear)); } void OnServerInitialized() { _vanishIsLoaded = (Vanish != null && Vanish.IsLoaded) || (BetterVanish != null && BetterVanish.IsLoaded); _statusIsLoaded = AdvancedStatus != null && AdvancedStatus.IsLoaded; ImgLibCheck(); InitPlayers(); } void Unload() => _config = null; #endregion } }