Advertisement
JimDeadlock

PvXPlayerList.cs

Jan 18th, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.43 KB | None | 0 0
  1. // ReSharper disable RedundantUsingDirective
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Oxide.Core;
  7. using Oxide.Core.Libraries.Covalence;
  8.  
  9. // ReSharper disable once CheckNamespace
  10. namespace Carbon.Plugins
  11. {
  12. [Info("PvXPlayerList", "waayne", "1.1")]
  13. [Description("Shows a list and count of all online and their pvx status by color, non-hidden players")]
  14. internal class PvXPlayerList : CarbonPlugin
  15. {
  16. #region Initialization
  17.  
  18. private const string PERM_ALLOW = "pvxplayerlist.allow";
  19. private const string PERM_HIDE = "pvxplayerlist.hide";
  20.  
  21. private bool _adminSeparate;
  22. private bool _pvxSeparate;
  23. private bool _showPvpFirst;
  24.  
  25. private string _adminColor;
  26. private string _pveColor;
  27. private string _pvpColor;
  28. private string _pveGroupsStr;
  29.  
  30. private string[] _pveGroups = { };
  31.  
  32. protected override void LoadDefaultConfig()
  33. {
  34. Config["Admin List Separate (true/false)"] =
  35. _adminSeparate = GetConfig("Admin List Separate (true/false)", false);
  36. Config["Separate PvE from PvP players (true/false)"] =
  37. _pvxSeparate = GetConfig("Separate PvE from PvP players (true/false)", true);
  38. Config["Show PvP players first (only applies if PvX is seperated) (true/false)"] =
  39. _showPvpFirst = GetConfig("Show PvP players first (only applies if PvX is seperated) (true/false)", true);
  40. Config["Admin Color (Hex Format or Name)"] =
  41. _adminColor = GetConfig("Admin Color (Hex Format or Name)", "e68c17");
  42. Config["PvE Color (Hex Format or Name)"] =
  43. _pveColor = GetConfig("PvE Color (Hex Format or Name)", "green");
  44. Config["PvP Color (Hex Format or Name)"] =
  45. _pvpColor = GetConfig("PvP Color (Hex Format or Name)", "red");
  46. Config["PvE group (if more than one seperate with comma: pve,nodmg)"] =
  47. _pveGroupsStr = GetConfig("PvE group (if more than one seperate with comma: pve,nodmg)", "pve");
  48.  
  49. _pveGroups = _pveGroupsStr.Split(',');
  50.  
  51. SaveConfig();
  52. }
  53.  
  54. private void Init()
  55. {
  56. LoadDefaultConfig();
  57. LoadDefaultMessages();
  58. permission.RegisterPermission(PERM_ALLOW, this);
  59. permission.RegisterPermission(PERM_HIDE, this);
  60. }
  61.  
  62. #endregion
  63.  
  64. #region Localization
  65.  
  66. private new void LoadDefaultMessages()
  67. {
  68. // English
  69. lang.RegisterMessages(new Dictionary<string, string>
  70. {
  71. ["AdminCount"] = "{0} admin online",
  72. ["AdminList"] = "Admin online ({0}): {1}",
  73. ["NobodyOnline"] = "No players are currently online",
  74. ["NotAllowed"] = "You are not allowed to use the '{0}' command",
  75. ["OnlyYou"] = "You are the only one online!",
  76. ["PlayerCount"] = "{0} player(s) online",
  77. ["PlayerList"] = "Players online ({0}): {1}"
  78. }, this);
  79.  
  80. // French
  81. lang.RegisterMessages(new Dictionary<string, string>
  82. {
  83. ["AdminCount"] = "{0} administrateurs en ligne",
  84. ["AdminList"] = "Administrateurs en ligne ({0}) : {1}",
  85. ["NobodyOnline"] = "Aucuns joueurs ne sont actuellement en ligne",
  86. ["NotAllowed"] = "Vous n’êtes pas autorisé à utiliser la commande « {0} »",
  87. ["OnlyYou"] = "Vous êtes la seule personne en ligne !",
  88. ["PlayerCount"] = "{0} joueur(s) en ligne",
  89. ["PlayerList"] = "Joueurs en ligne ({0}) : {1}"
  90. }, this, "fr");
  91.  
  92. // German
  93. lang.RegisterMessages(new Dictionary<string, string>
  94. {
  95. ["AdminCount"] = "{0} Administratoren online",
  96. ["AdminList"] = "Administratoren online ({0}): {1}",
  97. ["NobodyOnline"] = "Keine Spieler sind gerade online",
  98. ["NotAllowed"] = "Sie sind nicht berechtigt, verwenden Sie den Befehl '{0}'",
  99. ["OnlyYou"] = "Du bist der einzige Online!",
  100. ["PlayerCount"] = "{0} Spieler online",
  101. ["PlayerList"] = "Spieler online ({0}): {1}"
  102. }, this, "de");
  103.  
  104. // Russian
  105. lang.RegisterMessages(new Dictionary<string, string>
  106. {
  107. ["AdminCount"] = "{0} администраторы онлайн",
  108. ["AdminList"] = "Администраторы онлайн ({0}): {1}",
  109. ["NobodyOnline"] = "Ни один из игроков онлайн",
  110. ["NotAllowed"] = "Нельзя использовать команду «{0}»",
  111. ["OnlyYou"] = "Вы являетесь единственным онлайн!",
  112. ["PlayerCount"] = "{0} игрока (ов) онлайн",
  113. ["PlayerList"] = "Игроков онлайн ({0}): {1}"
  114. }, this, "ru");
  115.  
  116. // Spanish
  117. lang.RegisterMessages(new Dictionary<string, string>
  118. {
  119. ["AdminCount"] = "{0} administradores en línea",
  120. ["AdminList"] = "Los administradores en línea ({0}): {1}",
  121. ["NobodyOnline"] = "No hay jugadores están actualmente en línea",
  122. ["NotAllowed"] = "No se permite utilizar el comando '{0}'",
  123. ["OnlyYou"] = "Usted es el único en línea!",
  124. ["PlayerCount"] = "{0} jugadores en línea",
  125. ["PlayerList"] = "Jugadores en línea ({0}): {1}"
  126. }, this, "es");
  127. }
  128.  
  129. #endregion
  130.  
  131. #region Commands
  132.  
  133. [Command("online")]
  134. private void OnlineCommand(IPlayer player, string command, string[] args)
  135. {
  136. if (!player.HasPermission(PERM_ALLOW))
  137. {
  138. player.Reply(Lang("NotAllowed", player.Id, command));
  139. return;
  140. }
  141.  
  142. int adminCount = covalence.Players.Connected.Count(p => p.IsAdmin && !p.HasPermission(PERM_HIDE));
  143. int playerCount = covalence.Players.Connected.Count(p => !p.IsAdmin && !p.HasPermission(PERM_HIDE));
  144.  
  145. player.Reply($"{Lang("AdminCount", player.Id, adminCount)}, {Lang("PlayerCount", player.Id, playerCount)}");
  146. }
  147.  
  148. [Command("players", "who", "pop")]
  149. private void PlayersCommand(IPlayer player, string command, string[] args)
  150. {
  151. if (!player.HasPermission(PERM_ALLOW))
  152. {
  153. player.Reply(Lang("NotAllowed", player.Id, command));
  154. return;
  155. }
  156.  
  157. uint adminCount = 0;
  158. uint playerCount = 0;
  159. bool isPlayerPve = false;
  160. Dictionary<IPlayer, string> playerColorMap = new();
  161. foreach (var p in covalence.Players.Connected)
  162. {
  163. if (p.IsAdmin && !p.HasPermission(PERM_HIDE))
  164. {
  165. playerColorMap.Add(p, _adminColor);
  166. adminCount++;
  167. }
  168.  
  169. if (p.IsAdmin || p.HasPermission(PERM_HIDE))
  170. continue;
  171.  
  172. foreach (string pveGroup in _pveGroups)
  173. if (p.BelongsToGroup(pveGroup))
  174. isPlayerPve = true;
  175.  
  176. playerColorMap.Add(p, isPlayerPve ? _pveColor : _pvpColor);
  177. isPlayerPve = false;
  178. playerCount++;
  179. }
  180.  
  181. uint totalCount = adminCount + playerCount;
  182.  
  183. if (totalCount == 0)
  184. {
  185. player.Reply(Lang("NobodyOnline", player.Id));
  186. return;
  187. }
  188.  
  189. if (totalCount == 1 && player.Id != "server_console")
  190. {
  191. player.Reply(Lang("OnlyYou", player.Id));
  192. return;
  193. }
  194.  
  195. var adminList = Concat(playerColorMap, _adminColor);
  196.  
  197. string playerList;
  198. if (_pvxSeparate)
  199. {
  200. var pveList = Concat(playerColorMap, _pveColor);
  201. var pvpList = Concat(playerColorMap, _pvpColor);
  202.  
  203. if (!string.IsNullOrEmpty(pveList) && string.IsNullOrEmpty(pvpList))
  204. playerList = pveList;
  205. else if (string.IsNullOrEmpty(pveList) && !string.IsNullOrEmpty(pvpList))
  206. playerList = pvpList;
  207. else
  208. if(_showPvpFirst)
  209. playerList = string.Concat(pvpList, ", ", pveList);
  210. else
  211. playerList = string.Concat(pveList, ", ", pvpList);
  212. }
  213. else
  214. {
  215. playerList = Concat(playerColorMap, _adminColor, false);
  216. }
  217.  
  218. if (_adminSeparate && !string.IsNullOrEmpty(adminList))
  219. player.Reply(Lang("AdminList", player.Id, adminCount, adminList.TrimEnd(' ').TrimEnd(',')));
  220. else
  221. {
  222. if (adminCount > 0)
  223. {
  224. playerCount = adminCount + playerCount;
  225. playerList = string.Concat(adminList, ", ", playerList);
  226. }
  227. }
  228.  
  229. if (!string.IsNullOrEmpty(playerList))
  230. player.Reply(Lang("PlayerList", player.Id, playerCount, playerList.TrimEnd(' ').TrimEnd(',')));
  231. }
  232.  
  233. #endregion
  234.  
  235. #region Helpers
  236.  
  237. private string Concat(Dictionary<IPlayer, string> playerColorMap, string color, bool eq=true)
  238. {
  239. var players = playerColorMap.Where(pair => eq ? pair.Value == color : pair.Value != color).ToList();
  240. players.Sort((x, y) => string.Compare(x.Key.Name.Sanitize(), y.Key.Name.Sanitize(), StringComparison.Ordinal));
  241. return string.Join(", ", players.Select(pair => covalence.FormatText($"[#{pair.Value}]{pair.Key.Name.Sanitize()}[/#]"))
  242. .ToArray());
  243.  
  244. }
  245.  
  246. private T GetConfig<T>(string name, T value) =>
  247. Config[name] == null ? value : (T) Convert.ChangeType(Config[name], typeof(T));
  248.  
  249. private string Lang(string key, string id = null, params object[] args) =>
  250. string.Format(lang.GetMessage(key, this, id), args);
  251.  
  252. #endregion
  253. }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement