C#:
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
namespace Oxide.Plugins
{
[Info("OnlineCounter", "Miho", "1.0.0")]
[Description("Displays the realtime online player count in the UI")]
class OnlineCounter : RustPlugin
{
private ConfigData config;
private class ConfigData
{
[JsonProperty(PropertyName = "UI Config")]
public UIConfig uiConfig;
}
private class UIConfig
{
[JsonProperty(PropertyName = "AnchorMin")]
public string AnchorMin;
[JsonProperty(PropertyName = "AnchorMax")]
public string AnchorMax;
[JsonProperty(PropertyName = "Font Size")]
public int TextFontSize;
[JsonProperty(PropertyName = "Text Color")]
public string TextColor;
[JsonProperty(PropertyName = "Text Count")]
public string TextCount;
}
private ConfigData GetDefaultConfig()
{
return new ConfigData
{
uiConfig = new UIConfig
{
AnchorMin = "0.18 0",
AnchorMax = "0.28 0.05",
TextFontSize = 16,
TextColor = "1.0 1.0 1.0 1.0",
TextCount = "Online: "
}
};
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
config = Config.ReadObject<ConfigData>();
if (config == null)
{
LoadDefaultConfig();
}
}
catch
{
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig()
{
PrintError("Configuration file is corrupt(or not exists), creating new one!");
config = GetDefaultConfig();
}
protected override void SaveConfig()
{
Config.WriteObject(config);
}
void OnServerInitialized()
{
UpdateUIForAllPlayers();
}
void Unload()
{
foreach (BasePlayer player in BasePlayer.activePlayerList)
{
CuiHelper.DestroyUi(player, "OnlineCounter");
}
}
void OnPlayerConnected(BasePlayer player)
{
UpdateUIForAllPlayers();
}
void OnPlayerDisconnected(BasePlayer player, string reason)
{
NextTick(UpdateUIForAllPlayers);
}
void UpdateUIForAllPlayers()
{
foreach (BasePlayer current in BasePlayer.activePlayerList)
{
ShowUI(current);
}
}
void ShowUI(BasePlayer player)
{
CuiHelper.DestroyUi(player, "OnlineCounter");
CuiElementContainer container = new CuiElementContainer();
var text = new CuiElement
{
Name = "OnlineCounter",
Parent = "Overlay",
Components =
{
new CuiTextComponent
{
Text = config.uiConfig.TextCount + (BasePlayer.activePlayerList.Count).ToString(),
Align = TextAnchor.MiddleCenter,
Color = config.uiConfig.TextColor,
FontSize = config.uiConfig.TextFontSize
},
new CuiRectTransformComponent
{
AnchorMin = config.uiConfig.AnchorMin,
AnchorMax = config.uiConfig.AnchorMax
},
}
};
container.Add(text);
CuiHelper.AddUi(player, container);
}
}
}