C#:
using Oxide.Game.Rust.Cui;
using System;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using Oxide.Core;
using Newtonsoft.Json;
using Oxide.Core.Plugins;
using System.Collections;
using System.IO;
namespace Oxide.Plugins {
[Info("HudController", "Amino", "1.0.3")]
[Description("An advanced hud UI")]
public class HudController : RustPlugin {
[PluginReference]
Plugin ServerRewards, Economics, ImageLibrary;
#region Config
public class Configuration {
public string ServerName = "Random Server Name";
[JsonProperty(PropertyName = "Currency (ServerRewards, Economics)")]
public string Currency {
get;
set;
}
= "ServerRewards";
public string INFO {
get;
set;
}
= "Do not change the Name of the normal events!";
public List<EventsClass> Events {
get;
set;
}
= new List<EventsClass>();
public List<ExternalHookClass> ExternalHooks {
get;
set;
}
= new List<ExternalHookClass>();
public List<SetCommands> Commands {
get;
set;
}
= new List<SetCommands>();
public UIPositions UIPositions {
get;
set;
}
= new UIPositions();
public static Configuration DefaultConfig() {
return new Configuration() {
Events = new List<EventsClass>() {
new EventsClass() {
Enabled = true, Name = "Patrol", RunningIcon = "https://i.ibb.co/tmVxkk8/Heli-Active1.png", NotRunningIcon = "https://i.ibb.co/h78TSz3/Heli-Inactive1.png"
}
, new EventsClass() {
Enabled = true, Name = "Cargo", RunningIcon = "https://i.ibb.co/h8NRcbK/Cargo-Active1.png", NotRunningIcon = "https://i.ibb.co/JKcwkYS/Cargo-Inactive1.png"
}
, new EventsClass() {
Enabled = true, Name = "Bradley", RunningIcon = "https://i.ibb.co/Q6nMNdG/Bradley-Active1.png", NotRunningIcon = "https://i.ibb.co/mrpQGFh/Bradley-Inactive1.png"
}
, new EventsClass() {
Enabled = true, Name = "CH47", NotRunningIcon = "https://i.ibb.co/w6NsNsw/Chinook-Inactive1.png", RunningIcon = "https://i.ibb.co/SK8FZJS/Chinook-Active1.png"
}
}
, ExternalHooks = new List<ExternalHookClass>() {
new ExternalHookClass() {
Enabled = true, ExternalHookStart = "OnConvoyStart", ExternalHookEnd = "OnConvoyStop", RunningIcon = "https://i.ibb.co/nBxqhWz/Convoy-Active.png", NotRunningIcon = "https://i.ibb.co/kmPh9cb/Convoy-Inactive.png", HookName = "Convoy"
}
}
, Commands = new List<SetCommands>() {
new SetCommands() {
CommandName = "INFO", Command = "chat.say /info"
}
, new SetCommands() {
CommandName = "SHOP", Command = "chat.say /shop"
}
, new SetCommands() {
CommandName = "KITS", Command = "chat.say /kits"
}
,
}
}
;
}
}
private static Configuration _config;
protected override void LoadConfig() {
base.LoadConfig();
try {
_config = Config.ReadObject<Configuration>();
if (_config == null) LoadDefaultConfig();
SaveConfig();
}
catch (Exception e) {
Debug.LogException(e);
PrintWarning("Creating new config file.");
LoadDefaultConfig();
}
}
protected override void LoadDefaultConfig() => _config = Configuration.DefaultConfig();
protected override void SaveConfig() => Config.WriteObject(_config);
#endregion
#region UI Class
public class UIPositions {
public Dictionary<string, UIParts> UIPartsDict {
get;
set;
}
= new Dictionary<string, UIParts>();
public UIPositions() {
UIPartsDict.Add("BackgroundPanel", new UIParts {
Anchors = {
XMin =.007, YMin =.99, XMax =.007, YMax =.99
}
, Offsets = {
UseOffsets = true, XMax = 250, YMin = -30
}
, PanelSettings = {
Color = "0 0 0 0", Parent = "Overlay"
}
, IsEditable = false, Section = "Main", AlwaysDisplay = true
}
);
UIPartsDict.Add("OpenCloseButton", new UIParts {
Anchors = {
XMin = 0, YMin = -.06, XMax =.13, YMax = 1.06
}
, PanelSettings = {
Color = "0 0 0.6", Parent = "BackgroundPanel", FadeIn =.5f
}
, CommandSettings = {
Command = "hc_main close", IsButton = true
}
, IsEditable = false, Section = "Main", AlwaysDisplay = true, Image = "https://i.ibb.co/Rb4RLTP/HomeIcon.png"
}
);
UIPartsDict.Add("NextSectionButton", new UIParts {
Anchors = {
XMin = 0, YMin = -.5, XMax =.13, YMax = -.16
}
, PanelSettings = {
Color = "0 0 0.6", Parent = "BackgroundPanel", FadeIn =.5f
}
, CommandSettings = {
Command = "hc_main extend", IsButton = true
}
, IsEditable = false, Section = "Main", DisplayWithMoreThanOneSection = false, Image = "https://i.ibb.co/Sx7fqYb/DropIcon.png"
}
);
UIPartsDict.Add("PanelIconBar", new UIParts {
Anchors = {
XMin =.14, YMin = 0, XMax = 1, YMax = 1
}
, PanelSettings = {
Color = "0 0 0.6", Parent = "BackgroundPanel", FadeIn = 1f
}
, IsEditable = false, Section = "Main"
}
);
UIPartsDict.Add("PanelIconLayout", new UIParts {
Anchors = {
XMin =.01, YMin =.05, XMax =.135, YMax =.92
}
, PanelSettings = {
Color = "0 0 0 0", Parent = "PanelIconBar", FadeIn = 1f
}
, IsEditable = true, Section = "Main", ListSettings = {
ButtonSpacing =.135, Vertical = false, IsList = true
}
, ActuallyLoad = false
}
);
UIPartsDict.Add("CommandsPanel1", new UIParts {
Anchors = {
XMin = 0, YMin = -2, XMax =.13, YMax = -.15
}
, PanelSettings = {
Color = "0 0 0.6", Parent = "BackgroundPanel", FadeIn =.5f
}
, IsEditable = false, Section = "CommandsDropdown"
}
);
UIPartsDict.Add("CommandsPanel2", new UIParts {
Anchors = {
XMin =.14, YMin = -2, XMax = 1, YMax = -.15
}
, PanelSettings = {
Color = "0 0 0.6", Parent = "BackgroundPanel", FadeIn = 1f
}
, IsEditable = false, Section = "CommandsDropdown"
}
);
UIPartsDict.Add("PlayersPanel", new UIParts {
Anchors = {
XMin =.1, YMin =.66, XMax =.3, YMax =.98
}
, Text = {
Text = "{onlinePlayers}", Alignment = TextAnchor.MiddleLeft
}
, PanelSettings = {
Color = "0 0 0 0", Parent = "CommandsPanel2"
}
, IsEditable = false, Section = "CommandsDropdown"
}
);
UIPartsDict.Add("PlayersPanelIcon", new UIParts {
Anchors = {
XMin =.01, YMin =.7, XMax =.08, YMax =.95
}
, Image = "https://i.ibb.co/jv5L2r7/Players-Online.png", PanelSettings = {
Color = "0 0 0 0", Parent = "CommandsPanel2"
}
, IsEditable = false, Section = "CommandsDropdown"
}
);
UIPartsDict.Add("PlayersJoiningPanel", new UIParts {
Anchors = {
XMin =.1, YMin =.33, XMax =.3, YMax =.64
}
, Text = {
Text = "{joiningPlayers}", Alignment = TextAnchor.MiddleLeft
}
, PanelSettings = {
Color = "0 0 0 0", Parent = "CommandsPanel2"
}
, IsEditable = false, Section = "CommandsDropdown"
}
);
UIPartsDict.Add("PlayersJoiningPanelIcon", new UIParts {
Anchors = {
XMin =.01, YMin =.38, XMax =.08, YMax =.6
}
, Image = "https://i.ibb.co/t3FwGZM/Players-Joining.png", PanelSettings = {
Color = "0 0 0 0", Parent = "CommandsPanel2"
}
, IsEditable = false, Section = "CommandsDropdown"
}
);
UIPartsDict.Add("PlayersQueuedPanel", new UIParts {
Anchors = {
XMin =.1, YMin =.04, XMax =.3, YMax =.3
}
, Text = {
Text = "{queuedPlayers}", Alignment = TextAnchor.MiddleLeft
}
, PanelSettings = {
Color = "0 0 0 0", Parent = "CommandsPanel2"
}
, IsEditable = false, Section = "CommandsDropdown"
}
);
UIPartsDict.Add("PlayersQueuedPanelIcon", new UIParts {
Anchors = {
XMin =.01, YMin = 0, XMax =.08, YMax =.33
}
, Image = "https://i.ibb.co/QbzYKHw/Players-Queued.png", PanelSettings = {
Color = "0 0 0 0", Parent = "CommandsPanel2"
}
, IsEditable = false, Section = "CommandsDropdown"
}
);
UIPartsDict.Add("ServerNamePanel", new UIParts {
Anchors = {
XMin =.3, YMin =.04, XMax = 1, YMax =.3
}
, Text = {
Text = "{configServerName}", Alignment = TextAnchor.MiddleLeft
}
, PanelSettings = {
Color = "0 0 0 0", Parent = "CommandsPanel2"
}
, IsEditable = false, Section = "CommandsDropdown"
}
);
UIPartsDict.Add("ServerTimePanel", new UIParts {
Anchors = {
XMin =.3, YMin =.33, XMax =.66, YMax =.66
}
, Text = {
Text = "{serverTime}", Alignment = TextAnchor.MiddleLeft
}
, PanelSettings = {
Color = "0 0 0 0", Parent = "CommandsPanel2"
}
, IsEditable = false, Section = "CommandsDropdown"
}
);
UIPartsDict.Add("PlayerGridPanel", new UIParts {
Anchors = {
XMin =.66, YMin =.33, XMax = 1, YMax =.66
}
, Text = {
Text = "{playerGrid}", Alignment = TextAnchor.MiddleLeft
}
, PanelSettings = {
Color = "0 0 0 0", Parent = "CommandsPanel2"
}
, IsEditable = false, Section = "CommandsDropdown"
}
);
UIPartsDict.Add("PlayerCurrencyPanel", new UIParts {
Anchors = {
XMin =.3, YMin =.66, XMax = 1, YMax =.98
}
, Text = {
Text = "{playerCurrency}", Alignment = TextAnchor.MiddleLeft
}
, PanelSettings = {
Color = "0 0 0 0", Parent = "CommandsPanel2"
}
, IsEditable = false, Section = "CommandsDropdown"
}
);
UIPartsDict.Add("LastSectionButton", new UIParts {
Anchors = {
XMin = 0, YMin = -2.43, XMax =.13, YMax = -2.09
}
, PanelSettings = {
Color = "0 0 0.6", Parent = "BackgroundPanel", FadeIn =.5f
}
, CommandSettings = {
Command = "hc_main extend", IsButton = true
}
, IsEditable = false, Section = "CommandsDropdown", Image = "https://i.ibb.co/rcY7NZy/ColICon.png"
}
);
UIPartsDict.Add("CommandsSectionButton", new UIParts {
Anchors = {
XMin =.14, YMin = -2.43, XMax = 1, YMax = -2.09
}
, PanelSettings = {
Color = "0 0 0.6", Parent = "BackgroundPanel", FadeIn =.5f
}
, Text = {
Text = "COMMANDS", Size = 10
}
, CommandSettings = {
Command = "hc_main commands", IsButton = true
}
, IsEditable = false, Section = "CommandsDropdown"
}
);
UIPartsDict.Add("CommandsDropdown", new UIParts {
Anchors = {
XMin =.14, YMin = -3.25, XMax = 1, YMax = -2.52
}
, PanelSettings = {
Color = "0 0 0.6", Parent = "BackgroundPanel", FadeIn =.5f
}
, IsEditable = false, Section = "CommandsList"
}
);
UIPartsDict.Add("CommandsPanelLayout", new UIParts {
Anchors = {
XMin =.01, YMin =.1, XMax =.99, YMax =.96
}
, Text = {
Size = 12
}
, PanelSettings = {
Color = "0 0 0.3", Parent = "CommandsDropdown", FadeIn = 1f
}
, IsEditable = true, Section = "CommandsList", ListSettings = {
ButtonSpacing =.04, Vertical = true, IsList = true
}
, CommandSettings = {
Command = "hc_main command {0}", IsButton = true
}
, ActuallyLoad = false
}
);
}
public UIPositions Clone() {
UIPositions clonedPositions = new UIPositions();
clonedPositions.UIPartsDict.Clear();
foreach (var entry in UIPartsDict) {
clonedPositions.UIPartsDict.Add(entry.Key, entry.Value.Clone());
}
return clonedPositions;
}
public List<string> GetAllUIPartNames() {
return UIPartsDict.Keys.ToList();
}
}
public class UIParts {
public bool IsEditable = true;
public bool ActuallyLoad = true;
public string Image {
get;
set;
}
= null;
public string Section = String.Empty;
public bool DisplayWithMoreThanOneSection = true;
public bool AlwaysDisplay = false;
public CommandSettings CommandSettings = new CommandSettings();
public PanelSettings PanelSettings = new PanelSettings();
public ListSettings ListSettings = new ListSettings();
public UIAnchors Anchors = new UIAnchors();
public UIOffsets Offsets = new UIOffsets();
public UIText Text = new UIText();
public UIParts Clone() {
UIParts clone = new UIParts();
clone.ActuallyLoad = this.ActuallyLoad;
clone.IsEditable = this.IsEditable;
clone.Image = this.Image;
clone.Section = this.Section;
clone.DisplayWithMoreThanOneSection = this.DisplayWithMoreThanOneSection;
clone.AlwaysDisplay = this.AlwaysDisplay;
clone.CommandSettings = this.CommandSettings.Clone();
clone.PanelSettings = this.PanelSettings.Clone();
clone.ListSettings = this.ListSettings.Clone();
clone.Anchors = this.Anchors.Clone();
clone.Offsets = this.Offsets.Clone();
clone.Text = this.Text.Clone();
return clone;
}
}
public class CommandSettings {
public bool IsButton = false;
public string Command = string.Empty;
public CommandSettings Clone() {
return (CommandSettings)this.MemberwiseClone();
}
}
public class PanelSettings {
public string Color {
get;
set;
}
= "0 0 0.1";
public bool Blur {
get;
set;
}
= false;
public float FadeIn {
get;
set;
}
= 0f;
public string Parent = "Overlay";
public PanelSettings Clone() {
PanelSettings clone = new PanelSettings();
clone.Color = this.Color;
clone.Blur = this.Blur;
clone.FadeIn = this.FadeIn;
clone.Parent = this.Parent;
return clone;
}
}
public class ListSettings {
public bool Vertical = true;
public double ButtonSpacing =.15;
public bool IsList = false;
public ListSettings Clone() {
return (ListSettings)this.MemberwiseClone();
}
}
public class UIAnchors {
public double XMin {
get;
set;
}
= 0;
public double XMax {
get;
set;
}
= 0;
public double YMin {
get;
set;
}
= 0;
public double YMax {
get;
set;
}
= 0;
public UIAnchors Clone() {
return (UIAnchors)this.MemberwiseClone();
}
}
public class UIOffsets {
public bool UseOffsets = false;
public double XMin {
get;
set;
}
public double XMax {
get;
set;
}
public double YMin {
get;
set;
}
public double YMax {
get;
set;
}
public UIOffsets Clone() {
return (UIOffsets)this.MemberwiseClone();
}
}
public class UIText {
public string Text {
get;
set;
}
= String.Empty;
public string Color {
get;
set;
}
= "1 1 1 1";
public int Size {
get;
set;
}
= 15;
public string Font = "robotocondensed-bold.ttf";
public TextAnchor Alignment {
get;
set;
}
= TextAnchor.MiddleCenter;
public UIText Clone() {
return (UIText)this.MemberwiseClone();
}
}
#endregion
#region Class' & Constructors
public Dictionary<ulong, EditUIPositions> _editPeople = new Dictionary<ulong, EditUIPositions>();
public Dictionary<ulong, PlayerOptions> _playerOptions = new Dictionary<ulong, PlayerOptions>();
public Dictionary<string, bool> _events = new Dictionary<string, bool>();
public Dictionary<string, bool> _oldEvents = new Dictionary<string, bool>();
public ServerInfoParsed _serverInfo = new ServerInfoParsed();
System.Random rnd = new System.Random();
public double lastUpdated = 0;
public class SetCommands {
public string CommandName {
get;
set;
}
= string.Empty;
public string Command {
get;
set;
}
= string.Empty;
public string CommandImage {
get;
set;
}
= string.Empty;
}
public class PlayerOptions {
public List<string> neededPanels = new List<string>() {
"Main"
}
;
public List<string> lastOpened = new List<string>() {
"Main"
}
;
}
public class ServerInfoParsed {
public string Hostname {
get;
set;
}
public int MaxPlayers {
get;
set;
}
public int Players {
get;
set;
}
public int JoiningPlayers {
get;
set;
}
public int QueuedPlayers {
get;
set;
}
public string Time {
get;
set;
}
}
public class EditUIPositions {
public string ActiveUI {
get;
set;
}
public string ActiveUIPage {
get;
set;
}
public int EditPage {
get;
set;
}
public UIAnchors EditPanelPosition = new UIAnchors {
XMin =.01, YMin =.02, XMax =.3, YMax =.7
}
;
public UIPositions UIPositions = new UIPositions();
}
public class EventsClass {
public bool Enabled {
get;
set;
}
= false;
public string Name {
get;
set;
}
= string.Empty;
public string RunningIcon {
get;
set;
}
= string.Empty;
public string NotRunningIcon {
get;
set;
}
= string.Empty;
}
public class ExternalHookClass {
public bool Enabled {
get;
set;
}
= false;
public string HookName {
get;
set;
}
= string.Empty;
public string ExternalHookStart {
get;
set;
}
= string.Empty;
public string ExternalHookEnd {
get;
set;
}
= string.Empty;
public string RunningIcon {
get;
set;
}
= string.Empty;
public string NotRunningIcon {
get;
set;
}
= string.Empty;
}
#endregion
#region Lang
protected override void LoadDefaultMessages() {
lang.RegisterMessages(new Dictionary<string, string> {
["RPPlaceholder"]
= "RP: {0}", ["EconomicsPlaceholder"] = "${0}",
}
, this, "en");
}
private string Lang(string key, string id = null, params object[] args) => string.Format(lang.GetMessage(key, this, id), args);
#endregion
#region Hooks
private void Unload() {
if (!Interface.Oxide.IsShuttingDown) foreach (BasePlayer player in BasePlayer.activePlayerList) {
foreach (var UIPart in _config.UIPositions.UIPartsDict) {
if (UIPart.Value.PanelSettings.Parent.Equals("Overlay", StringComparison.OrdinalIgnoreCase)) CuiHelper.DestroyUi(player, UIPart.Key);
if (UIPart.Value.PanelSettings.Parent.Equals("Hud", StringComparison.OrdinalIgnoreCase)) CuiHelper.DestroyUi(player, UIPart.Key);
}
if (_editPeople.ContainsKey(player.userID)) {
foreach (var panel in _editPeople[player.userID].UIPositions.UIPartsDict.Where(x => x.Value.PanelSettings.Parent == "Overlay" || x.Value.PanelSettings.Parent == "Hud")) CuiHelper.DestroyUi(player, panel.Key);
CuiHelper.DestroyUi(player, "HCEditBG");
_editPeople.Remove(player.userID);
}
CuiHelper.DestroyUi(player, "HCEditBG");
}
_config = null;
}
void OnPlayerConnected(BasePlayer player) {
if(!_playerOptions.ContainsKey(player.userID)) _playerOptions.Add(player.userID, new PlayerOptions());
ServerMgr.Instance.StartCoroutine(CreateMainUI(player, false));
}
void OnPlayerDisconnected(BasePlayer player, string reason) {
if (player == null || !player.userID.Get().IsSteamId()) return;
if (_playerOptions.ContainsKey(player.userID)) _playerOptions.Remove(player.userID);
}
void OnServerInitialized(bool initial) {
RegisterCommandsAndPermissions();
ImportImages();
foreach (var evnt in _config.Events) if(evnt.Enabled) _events.Add(evnt.Name, false);
foreach (var extHook in _config.ExternalHooks) if(extHook.Enabled) _events.Add(extHook.HookName, false);
foreach (var player in BasePlayer.activePlayerList) {
_playerOptions.Add(player.userID, new PlayerOptions());
ServerMgr.Instance.StartCoroutine(CreateMainUI(player, false));
}
WriteNewHooks();
StartUpdateServerInfo();
CheckAllEntities();
StartUpdateUI();
}
void OnEntitySpawned(BaseCombatEntity entity) {
if (entity == null) return;
if (entity is PatrolHelicopter && _events.ContainsKey("Patrol")) _events["Patrol"] = true;
else if (entity is BradleyAPC && _events.ContainsKey("Bradley")) _events["Bradley"] = true;
else if (entity is CH47Helicopter && _events.ContainsKey("CH47")) _events["CH47"] = true;
else if (entity is CargoShip && _events.ContainsKey("Cargo")) _events["Cargo"] = true;
}
void OnEntityKill(BaseCombatEntity entity) {
if (entity == null) return;
if (entity is PatrolHelicopter && _events.ContainsKey("Patrol")) _events["Patrol"] = false;
else if (entity is BradleyAPC && _events.ContainsKey("Bradley")) _events["Bradley"] = false;
else if (entity is CH47Helicopter && _events.ContainsKey("CH47")) _events["CH47"] = false;
else if (entity is CargoShip && _events.ContainsKey("Cargo")) _events["Cargo"] = false;
}
void OnEntityDeath(BaseCombatEntity entity, HitInfo info) {
if (entity == null) return;
if (entity is PatrolHelicopter && _events.ContainsKey("Patrol")) _events["Patrol"] = false;
else if (entity is BradleyAPC && _events.ContainsKey("Bradley")) _events["Bradley"] = false;
else if (entity is CH47Helicopter && _events.ContainsKey("CH47")) _events["CH47"] = false;
else if (entity is CargoShip && _events.ContainsKey("Cargo")) _events["Cargo"] = false;
}
#endregion
#region Methods
void StartUpdateUI() => timer.Every(3, UpdateAllUI);
void StartUpdateServerInfo() => timer.Every(30, GetServerInfo);
void UpdateAllUI() {
var currentTime = DateTimeOffset.Now.ToUnixTimeSeconds();
if (!_oldEvents.SequenceEqual(_events) || currentTime - 30 > lastUpdated) {
GetServerInfo();
foreach (var player in BasePlayer.activePlayerList) {
ServerMgr.Instance.StartCoroutine(CreateMainUI(player, false));
}
lastUpdated = currentTime;
_oldEvents = new Dictionary<string, bool>(_events);
}
}
private void CheckAllEntities() {
foreach (var entity in BaseNetworkable.serverEntities) {
if (entity == null || entity.IsDestroyed) continue;
ServerMgr.Instance.StartCoroutine(CheckEntity(entity));
}
}
IEnumerator CheckEntity(BaseNetworkable entity) {
if (entity is PatrolHelicopter && _events.ContainsKey("Patrol")) _events["Patrol"] = true;
else if (entity is BradleyAPC && _events.ContainsKey("Bradley")) _events["Bradley"] = true;
else if (entity is CH47Helicopter && _events.ContainsKey("CH47")) _events["CH47"] = true;
else if (entity is CargoShip && _events.ContainsKey("Cargo")) _events["Cargo"] = true;
yield return null;
}
string GetReplacedText(BasePlayer player, string text = null) {
if (text == null) text = string.Empty;
var replacements = new Dictionary<string, string> {
{
"{serverName}", $"{_serverInfo.Hostname} "
}
, {
"{configServerName}", $"{_config.ServerName} "
}
, {
"{onlinePlayers}", $"{_serverInfo.Players}"
}
, {
"{joiningPlayers}", $"{_serverInfo.JoiningPlayers}"
}
, {
"{queuedPlayers}", $"{_serverInfo.QueuedPlayers}"
}
, {
"{totalPlayers}", $"{_serverInfo.QueuedPlayers + _serverInfo.JoiningPlayers + _serverInfo.Players}"
}
, {
"{playerGrid}", "getGrid"
}
, {
"{serverTime}", $"{_serverInfo.Time}"
}
, {
"{playerCurrency}", $"getCurrency"
}
, {
"{maxPlayers}", $"{_serverInfo.MaxPlayers}"
}
}
;
foreach (var placeholder in replacements) {
if (text.Contains(placeholder.Key)) {
var theValue = placeholder.Value;
if (placeholder.Value == "getGrid") theValue = GatherGrid(player);
else if (placeholder.Value == "getCurrency") theValue = GetPlayerCurrency(player);
text = text.Replace(placeholder.Key, theValue);
}
}
return text;
}
private void GetServerInfo() {
var serverInfo = ConVar.Admin.ServerInfo();
var timeParts = serverInfo.GameTime.Split(' ')[1].Split(':');
var theHour = int.Parse(timeParts[0]);
var timeInfo = theHour >= 12 ? $"{(theHour > 12 ? theHour - 12 : theHour)}:{timeParts[1]}PM" : $"{theHour}:{timeParts[1]}AM";
_serverInfo = new ServerInfoParsed {
Hostname = serverInfo.Hostname, MaxPlayers = serverInfo.MaxPlayers, Players = serverInfo.Players, JoiningPlayers = serverInfo.Joining, QueuedPlayers = serverInfo.Queued, Time = $"{timeInfo}"
}
;
}
string GatherGrid(BasePlayer player) {
var worldSize = ConVar.Server.worldsize;
var position = player.transform.position;
char gridLetter = 'A';
var xPos = Mathf.Floor((position.x + (worldSize / 2)) / 146.3f) % 26;
var zPos = (Mathf.Floor(worldSize / 146.3f)) - Mathf.Floor((position.z + (worldSize / 2)) / 146.3f) - 1;
gridLetter = (char)(gridLetter + xPos);
return $"{gridLetter}{zPos}";
}
string GetPlayerCurrency(BasePlayer player) {
var points = " ";
if (_config.Currency.Equals("serverrewards", StringComparison.OrdinalIgnoreCase)) {
var checkPoints = ServerRewards?.Call("CheckPoints", (ulong)player.userID);
points = Lang("RPPlaceholder", player.UserIDString, checkPoints == null ? "N/A" : $"{checkPoints}");
}
else if (_config.Currency.Equals("economics", StringComparison.OrdinalIgnoreCase)) {
var checkPoints = Economics?.Call("Balance", (ulong)player.userID);
points = Lang("EconomicsPlaceholder", player.UserIDString, checkPoints == null ? "N/A" : $"{checkPoints}");
}
return points;
}
private void OnCustomHookCalled(string hookName, bool isStart) => _events[hookName] = isStart;
private void WriteNewHooks() {
var sourcePath = $"{Interface.Oxide.PluginDirectory}{Path.DirectorySeparatorChar}HudController.cs";
var sourceFile = File.ReadAllText(sourcePath);
int regionStart = sourceFile.LastIndexOf("#region AddedPluginHooks"), regionEnd = sourceFile.LastIndexOf("#endregion");
if (regionStart == -1 || regionEnd == -1) return;
var newCode = "#region AddedPluginHooks\n";
foreach (var hook in _config.ExternalHooks) {
if (!hook.Enabled) continue;
newCode += $"private void {hook.ExternalHookStart}()=>OnCustomHookCalled(\"{hook.HookName}\", true);";
newCode += $"private void {hook.ExternalHookEnd}()=>OnCustomHookCalled(\"{hook.HookName}\", false);";
}
newCode += "\n#endregion";
var oldCode = sourceFile.Substring(regionStart, regionEnd - regionStart + 10);
if (oldCode.Equals(newCode)) return;
File.WriteAllText(sourcePath, sourceFile.Replace(oldCode, newCode));
}
PlayerOptions GetOrLoadPlayerOptions(BasePlayer player) {
if(!_playerOptions.ContainsKey(player.userID)) _playerOptions.Add(player.userID, new PlayerOptions());
return _playerOptions[player.userID];
}
EditUIPositions GetOrLoadEditPositions(BasePlayer player, bool loadNew = false) {
if (!loadNew && _editPeople.ContainsKey(player.userID)) {
return _editPeople[player.userID];
}
else {
EditUIPositions editPos = new EditUIPositions {
UIPositions = _config.UIPositions.Clone(), ActiveUI = _config.UIPositions.UIPartsDict.First().Key
}
;
_editPeople[player.userID] = editPos;
return editPos;
}
}
void CheckIfPanelsInclude(PlayerOptions playerOptions, string section) {
playerOptions.lastOpened = playerOptions.neededPanels.Select(x => x).ToList();
if (playerOptions.neededPanels.Contains(section)) {
if(section.Equals("CommandsDropdown", StringComparison.OrdinalIgnoreCase)) playerOptions.neededPanels.Remove("CommandsList");
playerOptions.neededPanels.Remove(section);
}
else playerOptions.neededPanels.Add(section);
}
private void RegisterNewImage(string imageName, string imageUrl) {
ImageLibrary?.Call("AddImage", imageUrl, imageName, 0UL, null);
}
void RegisterCommandsAndPermissions() {
permission.RegisterPermission("hudcontroller.admin", this);
cmd.AddChatCommand("hudedit", this, UIOpenEditMenu);
}
private string GetImage(string imageName) {
if (ImageLibrary == null) {
PrintError("Could not load images due to no Image Library");
return null;
}
return ImageLibrary?.Call<string>("GetImage", "UI" + imageName, 0UL, false);
}
void ImportImages() {
Dictionary<string, string> images = new Dictionary<string, string> {
{
"UILeftArrow", "https://i.ibb.co/pLsjbtx/Left-Arrow.png"
}
, {
"UIRightArrow", "https://i.ibb.co/mCRYNQG/Right-Arrow.png"
}
, {
"UIUpArrow", "https://i.ibb.co/HdvBXCz/UpArrow.png"
}
, {
"UIDownArrow", "https://i.ibb.co/0GGnFNB/Down-Arrow.png"
}
}
;
foreach (var item in _config.UIPositions.UIPartsDict.Where(x => !string.IsNullOrEmpty(x.Value.Image))) images.Add($"UI{item.Key}", item.Value.Image);
foreach (var image in _config.Events) {
images.Add($"UIActive{image.Name}", image.RunningIcon);
images.Add($"UIInactive{image.Name}", image.NotRunningIcon);
}
foreach (var image in _config.ExternalHooks) {
images.Add($"UIActive{image.HookName}", image.RunningIcon);
images.Add($"UIInactive{image.HookName}", image.NotRunningIcon);
}
ImageLibrary?.Call("ImportImageList", "HudController", images, 0UL, true, null);
}
#endregion
#region Commands
[ConsoleCommand("hc_main")]
private void CMDHudMenu(ConsoleSystem.Arg arg) {
var player = arg.Player();
if (player == null) return;
switch (arg.Args[0]) {
case "extend" : var playerOptions = GetOrLoadPlayerOptions(player);
CheckIfPanelsInclude(playerOptions, "CommandsDropdown");
playerOptions.lastOpened = playerOptions.neededPanels.Select(x => x).ToList();
break;
case "close" : playerOptions = GetOrLoadPlayerOptions(player);
playerOptions.lastOpened = playerOptions.neededPanels.Select(x => x).ToList();
if (playerOptions.neededPanels.Count > 0) playerOptions.neededPanels = new List<string>();
else playerOptions.neededPanels = new List<string>() {
"Main"
}
;
break;
case "commands" : playerOptions = GetOrLoadPlayerOptions(player);
CheckIfPanelsInclude(playerOptions, "CommandsList");
playerOptions.lastOpened = playerOptions.neededPanels.Select(x => x).ToList();
break;
case "command" : player.SendConsoleCommand(String.Join(" ", arg.Args.Skip(1)));
break;
}
if (arg.Args[0] != "command") ServerMgr.Instance.StartCoroutine(CreateMainUI(player, false));
}
[ConsoleCommand("hc_edit")]
private void CMDHudEdit(ConsoleSystem.Arg arg) {
var player = arg.Player();
if (player == null) return;
var editUIPositions = GetOrLoadEditPositions(player);
UIParts uiParts = editUIPositions.UIPositions.UIPartsDict[editUIPositions.ActiveUI];
if (arg.Args == null || arg.Args.Count() == 0) {
foreach (var panel in editUIPositions.UIPositions.UIPartsDict.Where(x => x.Value.PanelSettings.Parent == "Overlay" || x.Value.PanelSettings.Parent == "Hud")) CuiHelper.DestroyUi(player, panel.Key);
CuiHelper.DestroyUi(player, "HCEditBG");
_editPeople.Remove(player.userID);
return;
}
switch (arg.Args[0]) {
case "selectparent" : CuiHelper.DestroyUi(player, editUIPositions.ActiveUI);
uiParts.PanelSettings.Parent = string.Join(" ", arg.Args.Skip(1));
if (uiParts.PanelSettings.Parent == "Overlay") uiParts.Section = "Main";
else if (uiParts.PanelSettings.Parent == "Hud") uiParts.Section = "Main";
else uiParts.Section = editUIPositions.UIPositions.UIPartsDict[uiParts.PanelSettings.Parent].Section;
UICreateEditPanelProperties(player, editUIPositions);
break;
case "editpanel" : switch (arg.Args[1]) {
case "up" : editUIPositions.EditPanelPosition.YMax =.93;
editUIPositions.EditPanelPosition.YMin =.25;
break;
case "down" : editUIPositions.EditPanelPosition.YMax =.7;
editUIPositions.EditPanelPosition.YMin =.02;
break;
case "left" : editUIPositions.EditPanelPosition.XMax =.3;
editUIPositions.EditPanelPosition.XMin =.01;
break;
case "right" : editUIPositions.EditPanelPosition.XMax =.99;
editUIPositions.EditPanelPosition.XMin =.7;
break;
}
UICreateEditMenu(player, editUIPositions);
break;
case "editpage" : editUIPositions.EditPage = int.Parse(arg.Args[1]);
break;
case "addpanel" : String b = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String random = "";
for (int i = 0; i < 6; i++) {
int a = rnd.Next(26);
random += b.ElementAt(a);
}
editUIPositions.ActiveUI = random;
editUIPositions.UIPositions.UIPartsDict.Add(random, new UIParts {
Anchors = {
XMax =.1, XMin = 0, YMin = 0, YMax =.1
}
, Section = "Main", PanelSettings = {
Parent = "Hud"
}
}
);
UICreateEditMenu(player, editUIPositions);
break;
case "deletepanel" : if (uiParts.PanelSettings.Parent == "Overlay") CuiHelper.DestroyUi(player, editUIPositions.ActiveUI);
if (uiParts.PanelSettings.Parent == "Hud") CuiHelper.DestroyUi(player, editUIPositions.ActiveUI);
editUIPositions.UIPositions.UIPartsDict.Remove(editUIPositions.ActiveUI);
editUIPositions.ActiveUI = editUIPositions.UIPositions.UIPartsDict.First().Key;
UICreateEditMenu(player, editUIPositions);
break;
case "save" : _config.UIPositions.UIPartsDict = editUIPositions.UIPositions.Clone().UIPartsDict;
SaveConfig();
if (_editPeople.ContainsKey(player.userID)) _editPeople.Remove(player.userID);
CuiHelper.DestroyUi(player, "HCEditBG");
break;
case "panelinfo" : break;
case "close" : foreach (var panel in editUIPositions.UIPositions.UIPartsDict.Where(x => x.Value.PanelSettings.Parent == "Overlay" || x.Value.PanelSettings.Parent == "Hud")) CuiHelper.DestroyUi(player, panel.Key);
CuiHelper.DestroyUi(player, "HCEditBG");
_editPeople.Remove(player.userID);
break;
case "panelcolor" : uiParts.PanelSettings.Color = string.Join(" ", arg.Args.Skip(1));
UICreateEditPanel(player, editUIPositions);
break;
case "panelblur" : if (uiParts.PanelSettings.Blur) uiParts.PanelSettings.Blur = false;
else uiParts.PanelSettings.Blur = true;
UICreateEditPanel(player, editUIPositions);
break;
case "image" : if (arg.Args.Length >= 2 && arg.Args[1] == "img") {
uiParts.Image = arg.Args.Length < 3 ? null : String.Join(" ", arg.Args.Skip(2));
if (uiParts.Image != null) RegisterNewImage($"UI{editUIPositions.ActiveUI}", uiParts.Image);
}
UICreateEditImage(player, editUIPositions);
break;
case "text" : if (arg.Args.Length >= 2) {
switch (arg.Args[1]) {
case "text" : if (arg.Args.Length >= 2) uiParts.Text.Text = arg.Args.Length < 2 ? null : String.Join(" ", arg.Args.Skip(2));
break;
case "size" : uiParts.Text.Size = arg.Args.Length < 3 ? 15 : int.Parse(arg.Args[2]);
break;
case "color" : uiParts.Text.Color = arg.Args.Length < 3 ? null : String.Join(" ", arg.Args.Skip(2));
break;
case "font" : uiParts.Text.Font = String.Join(" ", arg.Args.Skip(2));
break;
case "anchor" : switch (arg.Args[2]) {
case "topleft" : uiParts.Text.Alignment = TextAnchor.UpperLeft;
break;
case "topcenter" : uiParts.Text.Alignment = TextAnchor.UpperCenter;
break;
case "topright" : uiParts.Text.Alignment = TextAnchor.UpperRigh
// ... [Code preview truncated due to length. Full source in attached file] ...