using System; using System.Globalization; using Oxide.Core; using Newtonsoft.Json; using System.Collections.Generic; using Oxide.Core.Plugins; using Oxide.Game.Rust.Cui; using UnityEngine; namespace Oxide.Plugins { [Info ( "CFuelUI", "TF Crazy - Raul-Sorin Sorban Edit", "1.0.6" )] [Description ( "Show fuel amount when player pilot a vehicle" )] public class CFuelUI : RustPlugin { #region Fields [PluginReference] Plugin ImageLibrary; private static CFuelUI _ins; private static Dictionary _pConf; private Dictionary _data; private Dictionary _images = new Dictionary () { {"cfuelui.bg","https://i49.servimg.com/u/f49/14/08/21/53/fuel10.png"}, }; #endregion #region Server /// /// Instance and initialize fields /// private void OnServerInitialized () { _ins = this; _pConf = new Dictionary (); _data = new Dictionary (); LoadData (); cmd.AddChatCommand ( cfg.config.command, this, nameof ( CFuelUIPlayerConfCommand ) ); if ( !ImageLibrary ) { PrintError ( "ImageLibrary not found, image not loaded !" ); return; } LoadServerImage (); foreach ( BasePlayer player in BasePlayer.activePlayerList ) OnPlayerConnected ( player ); } /// /// Destroy FuelUI player's object /// private void OnPlayerDisconnected ( BasePlayer player ) { if ( _data.ContainsKey ( player.userID ) ) UnityEngine.Object.Destroy ( _data [ player.userID ] ); } /// /// Destroy all FuelUI object /// private void Unload () { SaveData (); FuelUI [] objects = UnityEngine.Object.FindObjectsOfType (); if ( objects != null ) { foreach ( var obj in objects ) UnityEngine.Object.Destroy ( obj ); } } /// /// Instance and initialize fields /// private void OnPlayerConnected ( BasePlayer player ) { if ( player == null ) return; LoadPlayerImage ( player ); } /// /// Create object FuelUI when player mounted vehicles /// private void OnEntityMounted ( BaseMountable entity, BasePlayer player ) { if ( !( entity.GetParentEntity () is MiniCopter || entity.GetParentEntity () is Snowmobile || entity.GetParentEntity () is ModularCar || entity.GetParentEntity () is MotorRowboat ) ) return; if ( cfg.config.heli && entity.GetParentEntity () is MiniCopter ) return; if ( cfg.config.snow && entity.GetParentEntity () is Snowmobile ) return; if ( !( entity.ShortPrefabName == "miniheliseat" | entity.ShortPrefabName == "modularcardriverseat" | entity.ShortPrefabName == "transporthelipilot" | entity.ShortPrefabName == "transporthelicopilot" | entity.ShortPrefabName == "driverseat" | entity.ShortPrefabName == "smallboatdriver" | entity.ShortPrefabName == "snowmobiledriverseat" ) ) return; if ( !player.GetComponent () ) _data [ player.userID ] = player.gameObject.AddComponent (); } /// /// Destroy object FuelUI when player dismounted vehicles /// private void OnEntityDismounted ( BaseMountable entity, BasePlayer player ) { if ( _data.ContainsKey ( player.userID ) ) UnityEngine.Object.Destroy ( _data [ player.userID ] ); } #endregion #region Component /// /// FuelUI is object attached to player when mounted vehicle /// private class FuelUI : FacepunchBehaviour { public BasePlayer player; public BaseEntity entity; /// /// Initialize component /// private void Awake () { player = GetComponent (); entity = BaseNetworkable.serverEntities.Find ( player.mounted.uid ) as BaseEntity; CreateUI ( true ); InvokeRepeating ( "Instance", 0f, 1f ); } /// /// Update component /// private void Update () { if ( _pConf.ContainsKey ( player.userID ) && !_pConf [ player.userID ] ) Destroy ( this ); } /// /// Start coroutine. /// Refresh UI for progress bar /// public void Instance () { DestroyUI (); CreateUI (); } /// /// Get amount of fuel /// public int GetFuelAmount () { if ( entity == null ) return 0; if ( entity.GetParentEntity () is MiniCopter ) return ( entity.GetParentEntity () as MiniCopter ).GetFuelSystem ()?.GetFuelAmount () ?? 0; if ( entity.GetParentEntity () is Snowmobile ) return ( entity.GetParentEntity () as Snowmobile ).GetFuelSystem ()?.GetFuelAmount () ?? 0; if ( entity.GetParentEntity () is ModularCar ) return ( entity.GetParentEntity () as ModularCar ).GetFuelSystem ()?.GetFuelAmount () ?? 0; if ( entity.GetParentEntity () is MotorRowboat ) return ( entity.GetParentEntity () as MotorRowboat ).fuelSystem?.GetFuelAmount () ?? 0; return 0; } /// /// Create UI, (bool) true for create all UI /// private void CreateUI ( bool all = false ) { if ( all ) { var container = UI.CreateElementContainer ( "cfuilui.static", "0 0 0 0", "0.45 0.12", "0.53 0.21" ); UI.Image ( ref container, "cfuilui.static", GetImage ( "cfuelui.bg" ), "0 0", "1 1" ); CuiHelper.AddUi ( player, container ); } else { var container = UI.CreateElementContainer ( "cfuilui.amount", "0 0 0 0", "0.45 0.12", "0.53 0.21" ); UI.Label ( ref container, "cfuilui.amount", $"{GetFuelAmount ()}", 12, "0.3 0.28", "0.52 0.53" ); CuiHelper.AddUi ( player, container ); } } /// /// Destroy UI, (bool) true for destroy all UI /// private void DestroyUI ( bool all = false ) { if ( all ) { CuiHelper.DestroyUi ( player, "cfuilui.static" ); CuiHelper.DestroyUi ( player, "cfuilui.amount" ); } else CuiHelper.DestroyUi ( player, "cfuilui.amount" ); } /// /// Called when object is destroy. Clear all UI /// private void OnDestroy () => DestroyUI ( true ); } #endregion #region UI /// /// UI helpers tool /// static class UI { public static CuiElementContainer CreateElementContainer ( string name, string color, string aMin, string aMax ) { var element = new CuiElementContainer () { { new CuiPanel { Image = { Color = color }, RectTransform = { AnchorMin = aMin, AnchorMax = aMax }, CursorEnabled = false }, new CuiElement().Parent = "Hud", name } }; return element; } public static void Panel ( ref CuiElementContainer container, string panel, string color, string aMin, string aMax, bool cursor = false ) { container.Add ( new CuiPanel { Image = { Color = color }, RectTransform = { AnchorMin = aMin, AnchorMax = aMax }, CursorEnabled = cursor }, panel, CuiHelper.GetGuid () ); } public static void Label ( ref CuiElementContainer container, string panel, string text, int size, string aMin, string aMax, TextAnchor align = TextAnchor.MiddleCenter, string color = null ) { container.Add ( new CuiLabel { Text = { FontSize = size, Font = "robotocondensed-regular.ttf", Align = align, Text = text }, RectTransform = { AnchorMin = aMin, AnchorMax = aMax } }, panel, CuiHelper.GetGuid () ); } public static void Image ( ref CuiElementContainer container, string panel, string png, string aMin, string aMax ) { container.Add ( new CuiElement { Name = CuiHelper.GetGuid (), Parent = panel, Components = { new CuiRawImageComponent { Png = png }, new CuiRectTransformComponent { AnchorMin = aMin, AnchorMax = aMax } } } ); } } #endregion #region Command private void CFuelUIPlayerConfCommand ( BasePlayer player, string command, string [] args ) { if ( !_pConf.ContainsKey ( player.userID ) ) _pConf.Add ( player.userID, false ); if ( !_pConf [ player.userID ] ) { _pConf [ player.userID ] = true; Message ( player, "UIOn" ); } else { _pConf [ player.userID ] = false; Message ( player, "UIOff" ); } } #endregion #region Localization protected override void LoadDefaultMessages () { //English lang.RegisterMessages ( new Dictionary { [ "UIOn" ] = "Display activated", [ "UIOff" ] = "Display disabled", }, this ); //French lang.RegisterMessages ( new Dictionary { [ "UIOn" ] = "Affichage activé", [ "UIOff" ] = "Affichage désactivé", }, this, "fr" ); } #endregion #region Config private static ConfigData cfg; private class ConfigData { [JsonProperty ( "CONFIG" )] public ConfigList config { get; set; } [JsonProperty ( PropertyName = "VERSION" )] public VersionNumber version { get; set; } } private class ConfigList { [JsonProperty ( PropertyName = "Chat command" )] public string command { get; set; } [JsonProperty ( PropertyName = "Prefix" )] public string prefix { get; set; } [JsonProperty ( PropertyName = "Icon" )] public ulong icon { get; set; } [JsonProperty ( PropertyName = "Disable for minicopter and scrap heli" )] public bool heli { get; set; } [JsonProperty ( PropertyName = "Disable for snowmobiles" )] public bool snow { get; set; } } private ConfigData GetBaseConfig () { return new ConfigData { config = new ConfigList { command = "fuel", prefix = "SERVER FUEL UI : ", icon = 0, heli = false, }, version = Version }; } protected override void LoadConfig () { base.LoadConfig (); cfg = Config.ReadObject (); if ( cfg.version < Version ) UpdateConfigValues (); Config.WriteObject ( cfg, true ); } protected override void LoadDefaultConfig () => cfg = GetBaseConfig (); protected override void SaveConfig () => Config.WriteObject ( cfg, true ); private void UpdateConfigValues () { PrintWarning ( "Config update detected! Updating config values..." ); ConfigData baseConfig = GetBaseConfig (); cfg.version = Version; PrintWarning ( "Config update completed!" ); } #endregion #region Helpers /// /// Get image in ImageLibrary plugin /// /// Contains name of image /// Return string formated or empty public static string GetImage ( string imageName ) => ( string )_ins.ImageLibrary?.Call ( "GetImage", $"{imageName}" ); /// /// Load Image in ImageLibrary plugin /// /// Contains name of image /// Return string formated or empty private void LoadServerImage () { foreach ( var x in _images ) ImageLibrary.Call ( "AddImage", x.Value, x.Key ); } /// /// Pre-load image for player in ImageLibrary plugin /// /// Contains name of image private void LoadPlayerImage ( BasePlayer player ) { foreach ( var x in _images ) GetImage ( x.Key ); } /// /// Get text in player language /// /// Dictionary key /// player id for localisation /// Optional parameters /// Return string translate private string GetLang ( string langKey, string playerId = null, params object [] args ) => string.Format ( lang.GetMessage ( langKey, this, playerId ), args ); /// /// Send messsage to player /// /// Player informations /// Dictionary key /// Optional parameters private static void Message ( BasePlayer player, string langKey, params object [] args ) { if ( player.IsConnected ) _ins.Player.Message ( player, cfg.config.prefix + _ins.GetLang ( langKey, player.IPlayer.Id, args ), cfg.config.icon ); } /// /// Load player data /// private void LoadData () => _pConf = Interface.Oxide.DataFileSystem.ReadObject> ( "CFuelUI/playersConf" ); /// /// Save player data /// private void SaveData () => Interface.Oxide.DataFileSystem.WriteObject ( "CFuelUI/playersConf", _pConf ); #endregion } }