using Newtonsoft.Json; using Oxide.Core.Libraries.Covalence; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Oxide.Plugins { [Info("StaffList", "Reheight", "1.0.0")] [Description("Show the currently active staff on your server.")] class StaffList : CovalencePlugin { private List ActiveStaffDirectory = Facepunch.Pool.GetList(); private string StaffListString; PluginConfig _config; private void Init() { _config = Config.ReadObject(); } private void Unload() { Facepunch.Pool.FreeList(ref ActiveStaffDirectory); } protected override void LoadDefaultConfig() => _config = GetDefaultConfig(); protected override void LoadConfig() { base.LoadConfig(); try { _config = Config.ReadObject(); if (_config == null) { throw new JsonException(); } if (!_config.ToDictionary().Keys.SequenceEqual(Config.ToDictionary(x => x.Key, x => x.Value).Keys)) { PrintWarning($"PluginConfig file {Name}.json updated."); SaveConfig(); } } catch { LoadDefaultConfig(); PrintError("Config file contains an error and has been replaced with the default file."); } } protected override void SaveConfig() => Config.WriteObject(_config, true); private class PluginConfig { [JsonProperty(PropertyName = "Commands", Order = 0)] public string[] CommandList { get; set; } [JsonProperty(PropertyName = "Permission Required", Order = 1)] public bool PermissionRequired { get; set; } [JsonProperty(PropertyName = "Permission", Order = 2)] public string UsePermission { get; set; } [JsonProperty(PropertyName = "Staff Ranks", Order = 3)] public List StaffRanks { get; set; } [JsonProperty(PropertyName = "Prefix", Order = 4)] public string Prefix { get; set; } public string ToJson() => JsonConvert.SerializeObject(this); public Dictionary ToDictionary() => JsonConvert.DeserializeObject>(ToJson()); } private PluginConfig GetDefaultConfig() { return new PluginConfig { CommandList = new string[] { "staff", "stafflist", "staffonline", "activestaff", "as", "sl" }, PermissionRequired = true, UsePermission = "stafflist.use", StaffRanks = new List() { "owner", "administrator", "moderator" }, Prefix = "StaffList:" }; } protected override void LoadDefaultMessages() { lang.RegisterMessages(new Dictionary { ["NoPermission"] = "You do not have permission to do this!", ["StaffList"] = "You are viewing a list of the active staff!\n\n{0}", ["StaffIndividual"] = "{0}", ["StaffSeparator"] = ", " }, this); } private string Lang(string key, string pid, params object[] args) => _config.Prefix + " " + String.Format(lang.GetMessage(key, this, pid), args); private string LangNoPFX(string key, string pid, params object[] args) => String.Format(lang.GetMessage(key, this, pid), args); private void OnServerInitialized() { AddCovalenceCommand(_config.CommandList, nameof(StaffListCommand)); permission.RegisterPermission(_config.UsePermission, this); foreach (IPlayer iPlayer in players.Connected) { BasePlayer player = iPlayer.Object as BasePlayer; ProcessPlayer(player); } } private void StaffListCommand(IPlayer player, string command, string[] args) { if (_config.PermissionRequired && !permission.UserHasPermission(player.Id, _config.UsePermission)) { player.Reply(Lang("NoPermission", player.Id)); return; } player.Reply(Lang("StaffList", player.Id, StaffListString)); } private string GenerateStaffListString() { StringBuilder builder = new StringBuilder(); int index = 0; foreach (BasePlayer player in ActiveStaffDirectory) { if (index + 1 != ActiveStaffDirectory.Count()) builder.Append($"{LangNoPFX("StaffIndividual", player.UserIDString, player.displayName)}{LangNoPFX("StaffSeparator", player.UserIDString)}"); else builder.Append(LangNoPFX("StaffIndividual", player.UserIDString, player.displayName)); index++; } return builder.ToString(); } void OnPlayerConnected(BasePlayer player) { ProcessPlayer(player); } void OnPlayerDisconnected(BasePlayer player) { if (player == null) return; if (!ActiveStaffDirectory.Contains(player)) return; else ActiveStaffDirectory.Remove(player); } private void ProcessPlayer(BasePlayer player) { if (player == null) return; foreach (string group in permission.GetUserGroups(player.UserIDString)) { if (ActiveStaffDirectory.Contains(player)) break; if (!_config.StaffRanks.Contains(group)) continue; ActiveStaffDirectory.Add(player); } StaffListString = GenerateStaffListString(); } } }