C#:
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<ulong, bool> _pConf;
private Dictionary<ulong, FuelUI> _data;
private Dictionary<string, string> _images = new Dictionary<string, string> () {
{
"cfuelui.bg", "https://i49.servimg.com/u/f49/14/08/21/53/fuel10.png"
}
,
}
;
#endregion
#region Server
/// <summary>
/// Instance and initialize fields
/// </summary>
private void OnServerInitialized () {
_ins = this;
_pConf = new Dictionary<ulong, bool> ();
_data = new Dictionary<ulong, FuelUI> ();
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 );
}
/// <summary>
/// Destroy FuelUI player's object
/// </summary>
private void OnPlayerDisconnected (BasePlayer player ) {
if (_data.ContainsKey (player.userID ) ) UnityEngine.Object.Destroy (_data [player.userID ] );
}
/// <summary>
/// Destroy all FuelUI object
/// </summary>
private void Unload () {
SaveData ();
FuelUI [] objects = UnityEngine.Object.FindObjectsOfType<FuelUI> ();
if (objects != null ) {
foreach (var obj in objects ) UnityEngine.Object.Destroy (obj );
}
}
/// <summary>
/// Instance and initialize fields
/// </summary>
private void OnPlayerConnected (BasePlayer player ) {
if (player == null ) return;
LoadPlayerImage (player );
}
/// <summary>
/// Create object FuelUI when player mounted vehicles
/// </summary>
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<FuelUI> () ) _data [player.userID ] = player.gameObject.AddComponent<FuelUI> ();
}
/// <summary>
/// Destroy object FuelUI when player dismounted vehicles
/// </summary>
private void OnEntityDismounted (BaseMountable entity, BasePlayer player ) {
if (_data.ContainsKey (player.userID ) ) UnityEngine.Object.Destroy (_data [player.userID ] );
}
#endregion
#region Component
/// <summary>
/// FuelUI is object attached to player when mounted vehicle
/// </summary>
private class FuelUI : FacepunchBehaviour {
public BasePlayer player;
public BaseEntity entity;
/// <summary>
/// Initialize component
/// </summary>
private void Awake () {
player = GetComponent<BasePlayer> ();
entity = BaseNetworkable.serverEntities.Find (player.mounted.uid ) as BaseEntity;
CreateUI (true );
InvokeRepeating ("Instance", 0f, 1f );
}
/// <summary>
/// Update component
/// </summary>
private void Update () {
if (_pConf.ContainsKey (player.userID ) && !_pConf [player.userID ] ) Destroy (this );
}
/// <summary>
/// Start coroutine.
/// Refresh UI for progress bar
/// </summary>
public void Instance () {
DestroyUI ();
CreateUI ();
}
/// <summary>
/// Get amount of fuel
/// </summary>
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;
}
/// <summary>
/// Create UI, (bool) true for create all UI
/// </summary>
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 );
}
}
/// <summary>
/// Destroy UI, (bool) true for destroy all UI
/// </summary>
private void DestroyUI (bool all = false ) {
if (all ) {
CuiHelper.DestroyUi (player, "cfuilui.static" );
CuiHelper.DestroyUi (player, "cfuilui.amount" );
}
else CuiHelper.DestroyUi (player, "cfuilui.amount" );
}
/// <summary>
/// Called when object is destroy. Clear all UI
/// </summary>
private void OnDestroy () => DestroyUI (true );
}
#endregion
#region UI
/// <summary>
/// UI helpers tool
/// </summary>
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<string, string> {
["UIOn" ]
= "Display <color=#90ee90>activated</color>", ["UIOff" ] = "Display <color=#f08080>disabled</color>",
}
, this );
//French
lang.RegisterMessages (new Dictionary<string, string> {
["UIOn" ]
= "Affichage <color=#90ee90>activ�</color>", ["UIOff" ] = "Affichage <color=#f08080>d�sactiv�</color>",
}
, 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 = "<color=#aabf7d>SERVER</color> <color=#ebd077>FUEL UI</color> : ", icon = 0, heli = false,
}
, version = Version
}
;
}
protected override void LoadConfig () {
base.LoadConfig ();
cfg = Config.ReadObject<ConfigData> ();
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
/// <summary>
/// Get image in ImageLibrary plugin
/// </summary>
/// <param name="imageName">Contains name of image</param>
/// <returns> Return string formated or empty</returns>
public static string GetImage (string imageName ) => (string )_ins.ImageLibrary?.Call ("GetImage", $"{imageName}" );
/// <summary>
/// Load Image in ImageLibrary plugin
/// </summary>
/// <param name="imageName">Contains name of image</param>
/// <returns> Return string formated or empty</returns>
private void LoadServerImage () {
foreach (var x in _images ) ImageLibrary.Call ("AddImage", x.Value, x.Key );
}
/// <summary>
/// Pre-load image for player in ImageLibrary plugin
/// </summary>
/// <param name="imageName">Contains name of image</param>
private void LoadPlayerImage (BasePlayer player ) {
foreach (var x in _images ) GetImage (x.Key );
}
/// <summary>
/// Get text in player language
/// </summary>
/// <param name="langKey">Dictionary key</param>
/// <param name="playerId">player id for localisation</param>
/// <param name="args">Optional parameters</param>
/// <return>Return string translate</return>
private string GetLang (string langKey, string playerId = null, params object [] args ) => string.Format (lang.GetMessage (langKey, this, playerId ), args );
/// <summary>
/// Send messsage to player
/// </summary>
/// <param name="player">Player informations</param>
/// <param name="langKey">Dictionary key</param>
/// <param name="args">Optional parameters</param>
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 );
}
/// <summary>
/// Load player data
/// </summary>
private void LoadData () => _pConf = Interface.Oxide.DataFileSystem.ReadObject<Dictionary<ulong, bool>> ("CFuelUI/playersConf" );
/// <summary>
/// Save player data
/// </summary>
private void SaveData () => Interface.Oxide.DataFileSystem.WriteObject ("CFuelUI/playersConf", _pConf );
#endregion
}
}