C#:
using Oxide.Core;
using UnityEngine;
using Oxide.Game.Rust.Cui;
using System.Collections.Generic;
namespace Oxide.Plugins
{
[Info("HUDTime", "Bojack", "1.0.1")]
[Description("adds a simple in-game display HUD to your Rust server.")]
public class HUDTime : RustPlugin
{
private Dictionary<ulong, bool> hudTimeEnabled = new Dictionary<ulong, bool>();
private Dictionary<ulong, CuiElementContainer> hudTimeContainers = new Dictionary<ulong, CuiElementContainer>();
private double lastPlayerCountUpdateTime = 0;
private int lastPlayerCount = -1;
private void OnPlayerHUD(BasePlayer player)
{
if (player == null || player.IsNpc)
return;
bool isEnabled = GetHUDTimeEnabled(player);
if (isEnabled)
{
string timeString = GetServerTimeFormatted();
int playerCount = BasePlayer.activePlayerList.Count;
// Check if it's time to update the player count (every minute)
if (playerCount != lastPlayerCount || (Time.realtimeSinceStartup - lastPlayerCountUpdateTime) >= 30f)
{
lastPlayerCount = playerCount;
lastPlayerCountUpdateTime = Time.realtimeSinceStartup;
}
CuiElementContainer container = new CuiElementContainer();
// Create a label for the player count above the time
container.Add(new CuiLabel
{
RectTransform = { AnchorMin = "0.875 0.94", AnchorMax = "0.99 0.97" },
Text = { Text = $"Players: {playerCount}", FontSize = 10, Align = TextAnchor.MiddleRight }
}, "Overlay", "hud_playercount");
// Create a label for the server time
container.Add(new CuiLabel
{
RectTransform = { AnchorMin = "0.875 0.95", AnchorMax = "0.99 0.99" },
Text = { Text = $"Time: {timeString}", FontSize = 10, Align = TextAnchor.MiddleRight }
}, "Overlay", "hud_time");
// Remove existing UI elements with the same names
CuiHelper.DestroyUi(player, "hud_playercount");
CuiHelper.DestroyUi(player, "hud_time");
// Add the new UI elements to the player's screen
CuiHelper.AddUi(player, container);
if (!hudTimeContainers.ContainsKey(player.userID))
{
hudTimeContainers.Add(player.userID, container);
}
else
{
hudTimeContainers[player.userID] = container;
}
}
else
{
// Remove both the player count and time displays
CuiHelper.DestroyUi(player, "hud_playercount");
CuiHelper.DestroyUi(player, "hud_time");
if (hudTimeContainers.ContainsKey(player.userID))
{
hudTimeContainers.Remove(player.userID);
}
}
}
void OnPlayerInit(BasePlayer player)
{
OnPlayerHUD(player);
}
[ChatCommand("showhud")]
private void ShowTimeCommand(BasePlayer player, string cmd, string[] args)
{
bool isEnabled = GetHUDTimeEnabled(player);
isEnabled = !isEnabled;
SetHUDTimeEnabled(player, isEnabled);
if (isEnabled)
{
SendReply(player, "HUD is now enabled.");
OnPlayerHUD(player);
}
else
{
SendReply(player, "HUD is now disabled.");
CuiHelper.DestroyUi(player, "hud_playercount");
CuiHelper.DestroyUi(player, "hud_time");
if (hudTimeContainers.ContainsKey(player.userID))
{
hudTimeContainers.Remove(player.userID);
}
}
}
private string GetServerTimeFormatted()
{
var currentTime = TOD_Sky.Instance.Cycle.DateTime;
return currentTime.ToString("HH:mm");
}
private bool GetHUDTimeEnabled(BasePlayer player)
{
ulong playerId = player.userID;
if (hudTimeEnabled.ContainsKey(playerId))
{
return hudTimeEnabled[playerId];
}
return true;
}
private void SetHUDTimeEnabled(BasePlayer player, bool isEnabled)
{
ulong playerId = player.userID;
hudTimeEnabled[playerId] = isEnabled;
SaveData();
}
private void LoadData()
{
hudTimeEnabled = Interface.Oxide.DataFileSystem.ReadObject<Dictionary<ulong, bool>>("HUDTime_Data");
}
private void SaveData()
{
Interface.Oxide.DataFileSystem.WriteObject("HUDTime_Data", hudTimeEnabled);
}
private void Unload()
{
foreach (var player in BasePlayer.activePlayerList)
{
CuiHelper.DestroyUi(player, "hud_playercount");
CuiHelper.DestroyUi(player, "hud_time");
if (hudTimeContainers.ContainsKey(player.userID))
{
hudTimeContainers.Remove(player.userID);
}
}
SaveData();
}
private void OnServerInitialized()
{
LoadData();
foreach (var player in BasePlayer.activePlayerList)
{
OnPlayerHUD(player);
}
timer.Every(5f, () =>
{
foreach (var player in BasePlayer.activePlayerList)
{
OnPlayerHUD(player);
}
});
}
}
}