using System.Collections.Generic; using System.Linq; using Oxide.Game.Rust.Cui; using UnityEngine; using Oxide.Core.Libraries.Covalence; using Newtonsoft.Json; namespace Oxide.Plugins { [Info("Player List", "Error", "1.0.5")] class PlayerList : RustPlugin { #region Definitions private List onlinePlayers = new List(); private string overlayId = "onlinePlayersOverlay"; private string serverLogoId = "serverLogo"; private PluginConfig config; #endregion #region Configuration private class PluginConfig { [JsonProperty("Player list text")] public string OverlayText = "Online Players:"; [JsonProperty("Max amout of players to show in the list.")] public int MaxPlayersToShow = 30; [JsonProperty("Image URL for the image in the player list header.")] public string playerListLogoUrl = "https://i.imgur.com/5we4NbE.png"; [JsonProperty("Your Own Server Logo URL at the top left of the screen")] public string serverLogoUrl = "https://upload.wikimedia.org/wikipedia/commons/9/9f/Rust_logo.png"; [JsonProperty("Use Server Logo Image")] public bool UseServerLogo = false; public string ToJson() => JsonConvert.SerializeObject(this, Formatting.Indented); } protected override void LoadDefaultConfig() => config = new PluginConfig(); protected override void LoadConfig() { base.LoadConfig(); try { config = Config.ReadObject(); if (config == null) throw new JsonException(); } catch { PrintError($"Invalid configuration file, resetting to default."); LoadDefaultConfig(); } SaveConfig(); } protected override void SaveConfig() { Config.WriteObject(config, true); Puts("Configuration file saved."); } #endregion #region On Server Initialized void OnServerInitialized() { DisplayServerLogo(); UpdateOnlinePlayerList(); } #endregion #region On Player Sleep Ended void OnPlayerSleepEnded(BasePlayer player) { UpdateOnlinePlayerList(); DisplayServerLogo(); } #endregion #region On User Respawned void OnUserRespawned(IPlayer player) { UpdateOnlinePlayerList(); DisplayServerLogo(); } #endregion #region On Player Connected void OnPlayerConnected(BasePlayer player) { if (!onlinePlayers.Contains(player.displayName)) { onlinePlayers.Add(player.displayName); UpdateOnlinePlayerList(); } } #endregion #region On Player Disconnected void OnPlayerDisconnected(BasePlayer player) { if (onlinePlayers.Contains(player.displayName)) { onlinePlayers.Remove(player.displayName); UpdateOnlinePlayerList(); } } #endregion #region Display Server Logo private void DisplayServerLogo() { if (config.UseServerLogo) { var elements = new CuiElementContainer(); elements.Add(new CuiElement { Parent = "Hud", Name = serverLogoId, Components = { new CuiRawImageComponent { Url = config.serverLogoUrl, Sprite = "assets/content/ui/ui.background.tile.psd" }, new CuiRectTransformComponent { AnchorMin = "0.1 0.94", AnchorMax = "0.3 0.99" } } }); foreach (var player in BasePlayer.activePlayerList) { CuiHelper.AddUi(player, elements); } } } #endregion #region Player List CUI & Update Method private void UpdateOnlinePlayerList() { onlinePlayers.Clear(); foreach (var player in BasePlayer.activePlayerList) { onlinePlayers.Add(player.displayName); } string playerList = string.Join(", ", onlinePlayers.Take(config.MaxPlayersToShow)); string headerText = $" {config.OverlayText} ({onlinePlayers.Count})"; var elements = new CuiElementContainer(); // Main Panel var mainPanel = elements.Add(new CuiPanel { RectTransform = { AnchorMin = "0.8 0.78", AnchorMax = "0.98 0.98" }, Image = { Color = "0 0 0 0.5" } }, "Hud", overlayId); var headerPanel = elements.Add(new CuiPanel { RectTransform = { AnchorMin = "0 0.85", AnchorMax = "1 1" }, Image = { Color = "0 0 1 0.8" } }, mainPanel); elements.Add(new CuiElement { Parent = headerPanel, Components = { new CuiRawImageComponent { Url = config.playerListLogoUrl, Sprite = "assets/content/ui/ui.background.tile.psd" }, new CuiRectTransformComponent { AnchorMin = "0.02 0.1", AnchorMax = "0.12 0.85" } } }); elements.Add(new CuiLabel { RectTransform = { AnchorMin = "0.09 0", AnchorMax = "1 1" }, Text = { Text = headerText, FontSize = 12, Align = TextAnchor.MiddleLeft, Color = "1 1 1 1" } }, headerPanel); elements.Add(new CuiLabel { RectTransform = { AnchorMin = "0.05 0", AnchorMax = "1 0.85" }, Text = { Text = playerList, FontSize = 16, Align = TextAnchor.UpperLeft, Color = "1 1 1 1" } }, mainPanel); foreach (var player in BasePlayer.activePlayerList) { CuiHelper.DestroyUi(player, overlayId); CuiHelper.AddUi(player, elements); } } #endregion } }