From dda0aea93d621a82f390e36e92b930ba4b03f8ee Mon Sep 17 00:00:00 2001 From: Karl Essinger Date: Tue, 26 May 2020 01:37:18 +0200 Subject: [PATCH] Changed from spaces to tabs --- SCPermissions/SCPermissions.cs | 1128 ++++++++++++++++---------------- 1 file changed, 562 insertions(+), 566 deletions(-) diff --git a/SCPermissions/SCPermissions.cs b/SCPermissions/SCPermissions.cs index ffdb287..411d04e 100644 --- a/SCPermissions/SCPermissions.cs +++ b/SCPermissions/SCPermissions.cs @@ -19,18 +19,18 @@ namespace SCPermissions { - [PluginDetails( - author = "Karl Essinger", - name = "SCPermissions", - description = "A permissions system. Secure, Contain, Permit.", - id = "karlofduty.scpermissions", - version = "1.0.1", - SmodMajor = 3, - SmodMinor = 7, - SmodRevision = 0 - )] - public class SCPermissions : Plugin, IPermissionsHandler - { + [PluginDetails( + author = "Karl Essinger", + name = "SCPermissions", + description = "A permissions system. Secure, Contain, Permit.", + id = "karlofduty.scpermissions", + version = "1.0.1", + SmodMajor = 3, + SmodMinor = 7, + SmodRevision = 0 + )] + public class SCPermissions : Plugin, IPermissionsHandler + { // Contains all registered players userIDs and list of the ranks they have private Dictionary> playerRankDict = new Dictionary>(); @@ -40,10 +40,10 @@ public class SCPermissions : Plugin, IPermissionsHandler // A json object representing the permissions section in the config private JObject permissions = null; - // Other config options - private bool verbose = false; - private bool debug = false; - private string defaultRank = ""; + // Other config options + private bool verbose = false; + private bool debug = false; + private string defaultRank = ""; private HashSet GetPlayerRanks(string userID) { @@ -66,179 +66,175 @@ private HashSet GetPlayerRanks(string userID) return ranks; } - // Called by the permissions manager when any plugin checks the permissions of a player - public short CheckPermission(Player player, string permissionName) - { - return CheckPermission(player.UserId, permissionName); - } + // Called by the permissions manager when any plugin checks the permissions of a player + public short CheckPermission(Player player, string permissionName) + { + return CheckPermission(player.UserId, permissionName); + } // I've split this up so I can easily provide a userID without joining when debugging public short CheckPermission(string userID, string permissionName) - { - this.Debug("Checking permission '" + permissionName + "' on " + userID + "."); + { + this.Debug("Checking permission '" + permissionName + "' on " + userID + "."); - if (permissions == null) - { - this.Warn("Tried to check permission node '" + permissionName + "' but permissions had not been loaded yet."); - return 0; - } + if (permissions == null) + { + this.Warn("Tried to check permission node '" + permissionName + "' but permissions had not been loaded yet."); + return 0; + } HashSet playerRanks = GetPlayerRanks(userID); - // Check if player has any ranks - if (playerRanks.Count > 0) - { - this.Debug("Ranks: " + string.Join(", ", playerRanks)); - // Check every rank from the rank system in the order they are registered in the config until an instance of the permission is found - JProperty[] allRanks = permissions.Properties().ToArray(); - foreach (JProperty rank in allRanks) - { - // Check if the player has the rank - if (playerRanks.Contains(rank.Name) || rank.Name == defaultRank) - { - try - { - JToken permissionNode = permissions.SelectToken(rank.Name + "['" + permissionName + "']"); - if (permissionNode != null) - { - // Returns 1 if permission is allowed, returns -1 if permission is forbidden - if (permissionNode.Value()) - { - this.Debug("Returned 1 from " + rank.Name); - return 1; - } - else - { - this.Debug("Returned -1 from " + rank.Name); - return -1; - } - } - } - catch (Exception e) - { - this.Verbose("Error attempting to parse permission node " + permissionName + " in rank " + rank.Name + ": " + e.Message); - } - } - } - } - this.Debug("Returned 0"); - return 0; - } - - public override void OnDisable() - { - // Useless function - } - - public override void OnEnable() - { - this.AddCommand("scperms_reload", new ReloadCommand(this)); - this.AddCommand("scperms_giverank", new GiveRankCommand(this)); - this.AddCommand("scperms_removerank", new RemoveRankCommand(this)); - this.AddCommand("scperms_givetemprank", new GiveTempRankCommand(this)); - this.AddCommand("scperms_removetemprank", new RemoveTempRankCommand(this)); - this.AddCommand("scperms_listranks", new ListRanksCommand(this)); - this.AddCommand("scperms_verbose", new VerboseCommand(this)); - this.AddCommand("scperms_debug", new DebugCommand(this)); - - this.AddCommand("scpermissions_reload", new ReloadCommand(this)); - this.AddCommand("scpermissions_giverank", new GiveRankCommand(this)); - this.AddCommand("scpermissions_removerank", new RemoveRankCommand(this)); - this.AddCommand("scpermissions_givetemprank", new GiveTempRankCommand(this)); - this.AddCommand("scpermissions_removetemprank", new RemoveTempRankCommand(this)); - this.AddCommand("scpermissions_listranks", new ListRanksCommand(this)); - this.AddCommand("scpermissions_verbose", new VerboseCommand(this)); - this.AddCommand("scpermissions_debug", new DebugCommand(this)); - - LoadConfig(); - LoadPlayerData(); - this.AddEventHandlers(new PlayerJoinHandler(this), Priority.High); + // Check if player has any ranks + if (playerRanks.Count > 0) + { + this.Debug("Ranks: " + string.Join(", ", playerRanks)); + // Check every rank from the rank system in the order they are registered in the config until an instance of the permission is found + JProperty[] allRanks = permissions.Properties().ToArray(); + foreach (JProperty rank in allRanks) + { + // Check if the player has the rank + if (playerRanks.Contains(rank.Name) || rank.Name == defaultRank) + { + try + { + JToken permissionNode = permissions.SelectToken(rank.Name + "['" + permissionName + "']"); + if (permissionNode != null) + { + // Returns 1 if permission is allowed, returns -1 if permission is forbidden + if (permissionNode.Value()) + { + this.Debug("Returned 1 from " + rank.Name); + return 1; + } + else + { + this.Debug("Returned -1 from " + rank.Name); + return -1; + } + } + } + catch (Exception e) + { + this.Verbose("Error attempting to parse permission node " + permissionName + " in rank " + rank.Name + ": " + e.Message); + } + } + } + } + this.Debug("Returned 0"); + return 0; + } + + public override void OnEnable() + { + this.AddCommand("scperms_reload", new ReloadCommand(this)); + this.AddCommand("scperms_giverank", new GiveRankCommand(this)); + this.AddCommand("scperms_removerank", new RemoveRankCommand(this)); + this.AddCommand("scperms_givetemprank", new GiveTempRankCommand(this)); + this.AddCommand("scperms_removetemprank", new RemoveTempRankCommand(this)); + this.AddCommand("scperms_listranks", new ListRanksCommand(this)); + this.AddCommand("scperms_verbose", new VerboseCommand(this)); + this.AddCommand("scperms_debug", new DebugCommand(this)); + + this.AddCommand("scpermissions_reload", new ReloadCommand(this)); + this.AddCommand("scpermissions_giverank", new GiveRankCommand(this)); + this.AddCommand("scpermissions_removerank", new RemoveRankCommand(this)); + this.AddCommand("scpermissions_givetemprank", new GiveTempRankCommand(this)); + this.AddCommand("scpermissions_removetemprank", new RemoveTempRankCommand(this)); + this.AddCommand("scpermissions_listranks", new ListRanksCommand(this)); + this.AddCommand("scpermissions_verbose", new VerboseCommand(this)); + this.AddCommand("scpermissions_debug", new DebugCommand(this)); + + LoadConfig(); + LoadPlayerData(); + this.AddEventHandlers(new PlayerJoinHandler(this), Priority.High); this.Info("Special Containment Permissions loaded."); - } + } + public override void OnDisable() {} - public override void Register() - { - RegisterPermissionsHandler(this); - this.AddConfig(new ConfigSetting("scperms_config_global", true, true, "Whether or not the config should be placed in the global config directory, by default true.")); - this.AddConfig(new ConfigSetting("scperms_playerdata_global", true, true, "Whether or not the player data file should be placed in the global config directory, by default true.")); - } + public override void Register() + { + RegisterPermissionsHandler(this); + this.AddConfig(new ConfigSetting("scperms_config_global", true, true, "Whether or not the config should be placed in the global config directory, by default true.")); + this.AddConfig(new ConfigSetting("scperms_playerdata_global", true, true, "Whether or not the player data file should be placed in the global config directory, by default true.")); + } /// /// Loads the plugin config /// - private void LoadConfig() - { - if (!Directory.Exists(FileManager.GetAppFolder(true, !GetConfigBool("scperms_config_global")) + "SCPermissions")) - { - Directory.CreateDirectory(FileManager.GetAppFolder(true, !GetConfigBool("scperms_config_global")) + "SCPermissions"); - } + private void LoadConfig() + { + if (!Directory.Exists(FileManager.GetAppFolder(true, !GetConfigBool("scperms_config_global")) + "SCPermissions")) + { + Directory.CreateDirectory(FileManager.GetAppFolder(true, !GetConfigBool("scperms_config_global")) + "SCPermissions"); + } - if (!File.Exists(FileManager.GetAppFolder(true, !GetConfigBool("scperms_config_global")) + "SCPermissions/config.yml")) - { - File.WriteAllText(FileManager.GetAppFolder(true, !GetConfigBool("scperms_config_global")) + "SCPermissions/config.yml", Encoding.UTF8.GetString(Resources.config)); - } + if (!File.Exists(FileManager.GetAppFolder(true, !GetConfigBool("scperms_config_global")) + "SCPermissions/config.yml")) + { + File.WriteAllText(FileManager.GetAppFolder(true, !GetConfigBool("scperms_config_global")) + "SCPermissions/config.yml", Encoding.UTF8.GetString(Resources.config)); + } - // Reads config contents into FileStream - FileStream stream = File.OpenRead(FileManager.GetAppFolder(true, !GetConfigBool("scperms_config_global")) + "SCPermissions/config.yml"); + // Reads config contents into FileStream + FileStream stream = File.OpenRead(FileManager.GetAppFolder(true, !GetConfigBool("scperms_config_global")) + "SCPermissions/config.yml"); - // Converts the FileStream into a YAML Dictionary object - IDeserializer deserializer = new DeserializerBuilder().Build(); - object yamlObject = deserializer.Deserialize(new StreamReader(stream)); + // Converts the FileStream into a YAML Dictionary object + IDeserializer deserializer = new DeserializerBuilder().Build(); + object yamlObject = deserializer.Deserialize(new StreamReader(stream)); - // Converts the YAML Dictionary into JSON String - ISerializer serializer = new SerializerBuilder().JsonCompatible().Build(); - string jsonString = serializer.Serialize(yamlObject); + // Converts the YAML Dictionary into JSON String + ISerializer serializer = new SerializerBuilder().JsonCompatible().Build(); + string jsonString = serializer.Serialize(yamlObject); - JObject json = JObject.Parse(jsonString); + JObject json = JObject.Parse(jsonString); - permissions = (JObject)json.SelectToken("permissions"); - verbose = json.SelectToken("verbose").Value(); - debug = json.SelectToken("debug").Value(); - defaultRank = json.SelectToken("defaultRank").Value(); + permissions = (JObject)json.SelectToken("permissions"); + verbose = json.SelectToken("verbose").Value(); + debug = json.SelectToken("debug").Value(); + defaultRank = json.SelectToken("defaultRank").Value(); - this.Info("Config \"" + FileManager.GetAppFolder(true, !GetConfigBool("scperms_config_global")) + "SCPermissions/config.yml\" loaded."); - } + this.Info("Config \"" + FileManager.GetAppFolder(true, !GetConfigBool("scperms_config_global")) + "SCPermissions/config.yml\" loaded."); + } /// /// Loads player data /// - private void LoadPlayerData() - { - if (!Directory.Exists(FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions")) - { - Directory.CreateDirectory(FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions"); - } - if (!File.Exists(FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions/playerdata.yml")) - { - File.WriteAllText(FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions/playerdata.yml", Encoding.UTF8.GetString(Resources.players)); - } - - // Reads config contents into FileStream - FileStream stream = File.OpenRead(FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions/playerdata.yml"); - - // Converts the FileStream into a YAML Dictionary object - IDeserializer deserializer = new DeserializerBuilder().Build(); - playerRankDict = deserializer.Deserialize>>(new StreamReader(stream)) ?? new Dictionary>(); - - this.Info("Player data \"" + FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions/playerdata.yml\" loaded."); - } + private void LoadPlayerData() + { + if (!Directory.Exists(FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions")) + { + Directory.CreateDirectory(FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions"); + } + if (!File.Exists(FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions/playerdata.yml")) + { + File.WriteAllText(FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions/playerdata.yml", Encoding.UTF8.GetString(Resources.players)); + } + + // Reads config contents into FileStream + FileStream stream = File.OpenRead(FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions/playerdata.yml"); + + // Converts the FileStream into a YAML Dictionary object + IDeserializer deserializer = new DeserializerBuilder().Build(); + playerRankDict = deserializer.Deserialize>>(new StreamReader(stream)) ?? new Dictionary>(); + + this.Info("Player data \"" + FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions/playerdata.yml\" loaded."); + } /// /// Saves player data to file /// - private void SavePlayerData() - { - StringBuilder builder = new StringBuilder(); - foreach (KeyValuePair> playerRanks in playerRankDict) - { - if(playerRanks.Value.Count > 0) - { - builder.Append(playerRanks.Key + ": [ \"" + string.Join("\", \"", playerRanks.Value) + "\" ]\n"); - } - } - File.WriteAllText(FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions/playerdata.yml", builder.ToString()); - } + private void SavePlayerData() + { + StringBuilder builder = new StringBuilder(); + foreach (KeyValuePair> playerRanks in playerRankDict) + { + if(playerRanks.Value.Count > 0) + { + builder.Append(playerRanks.Key + ": [ \"" + string.Join("\", \"", playerRanks.Value) + "\" ]\n"); + } + } + File.WriteAllText(FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions/playerdata.yml", builder.ToString()); + } /// /// Checks if the first player has a higher rank than the second. @@ -247,53 +243,53 @@ private void SavePlayerData() /// userID of the player assumed to have the lower rank. /// private bool RankIsHigherThan(string highRankUserID, string lowRankUserID) - { + { HashSet highPlayerRanks = GetPlayerRanks(highRankUserID); HashSet lowPlayerRanks = GetPlayerRanks(lowRankUserID); if (highPlayerRanks.Count == 0) - { - return false; - } - - if(lowPlayerRanks.Count == 0) - { - return true; - } - - JProperty[] ranks = permissions.Properties().ToArray(); - foreach (JProperty rankProperty in ranks) - { - // If this rank is found first it is either higher in rank or equal to the other player, so returns false - if (lowPlayerRanks.Contains(rankProperty.Name)) - { - return false; - } - else if (highPlayerRanks.Contains(rankProperty.Name)) - { - return true; - } - } - return false; - } + { + return false; + } + + if(lowPlayerRanks.Count == 0) + { + return true; + } + + JProperty[] ranks = permissions.Properties().ToArray(); + foreach (JProperty rankProperty in ranks) + { + // If this rank is found first it is either higher in rank or equal to the other player, so returns false + if (lowPlayerRanks.Contains(rankProperty.Name)) + { + return false; + } + else if (highPlayerRanks.Contains(rankProperty.Name)) + { + return true; + } + } + return false; + } /// /// Checks if a rank of this name is registered in the config. /// /// The rank to check. /// - public bool RankExists(string rank) - { - JProperty[] ranks = permissions.Properties().ToArray(); - foreach (JProperty rankProperty in ranks) - { - if(rank == rankProperty.Name) - { - return true; - } - } - return false; - } + public bool RankExists(string rank) + { + JProperty[] ranks = permissions.Properties().ToArray(); + foreach (JProperty rankProperty in ranks) + { + if(rank == rankProperty.Name) + { + return true; + } + } + return false; + } /// /// Gives a rank to a player, refreshes their vanilla rank and saves all players ranks to file @@ -302,23 +298,23 @@ public bool RankExists(string rank) /// Rank to grant. /// [PipeMethod] - public bool GiveRank(string userID, string rank) - { - if(!RankExists(rank)) - { - return false; - } - - if(!playerRankDict.ContainsKey(userID)) - { - playerRankDict.Add(userID, new HashSet()); - } + public bool GiveRank(string userID, string rank) + { + if(!RankExists(rank)) + { + return false; + } + + if(!playerRankDict.ContainsKey(userID)) + { + playerRankDict.Add(userID, new HashSet()); + } playerRankDict[userID].Add(rank); SavePlayerData(); - RefreshVanillaRank(this.Server.GetPlayers(userID).FirstOrDefault()); - return true; - } + RefreshVanillaRank(this.Server.GetPlayers(userID).FirstOrDefault()); + return true; + } /// /// Gives a temporary rank to a player which is not saved to file and refreshes the vanilla rank @@ -352,18 +348,18 @@ public bool GiveTempRank(string userID, string rank) /// [PipeMethod] public bool RemoveRank(string userID, string rank) - { - if (playerRankDict.ContainsKey(userID)) - { - if (playerRankDict[userID].Remove(rank)) - { - SavePlayerData(); - RefreshVanillaRank(this.Server.GetPlayers(userID).FirstOrDefault()); - RemoveTempRank(userID, rank); - return true; - } - } - return RemoveTempRank(userID, rank); + { + if (playerRankDict.ContainsKey(userID)) + { + if (playerRankDict[userID].Remove(rank)) + { + SavePlayerData(); + RefreshVanillaRank(this.Server.GetPlayers(userID).FirstOrDefault()); + RemoveTempRank(userID, rank); + return true; + } + } + return RemoveTempRank(userID, rank); } /// @@ -392,274 +388,274 @@ public bool RemoveTempRank(string userID, string rank) /// /// public void RefreshVanillaRank(Player player) - { - if(player == null) - { - return; - } + { + if(player == null) + { + return; + } this.Debug("Refreshing vanilla ranks for: " + player.Name); HashSet playerRanks = GetPlayerRanks(player.UserId); - if (playerRanks.Count > 0) - { - this.Debug("Ranks: " + string.Join(", ", playerRanks)); - - // Check every rank from the rank system in the order they are registered in the config until a vanillarank entry is found - JProperty[] ranks = permissions.Properties().ToArray(); - foreach (JProperty rankProperty in ranks) - { - // Check if the player has the rank - if (playerRanks.Contains(rankProperty.Name) || rankProperty.Name == defaultRank) - { - try - { - // Checks if the rank has a vanillarank entry - string vanillarank = permissions.SelectToken(rankProperty.Name + ".vanillarank")?.Value(); - if (vanillarank != null) - { - player.SetRank(null, null, vanillarank); - this.Debug("Set vanilla rank for " + player.Name + " to " + rankProperty.Name); - return; - } - } - catch (Exception e) - { - this.Verbose("Error attempting to parse vanilla rank entry of rank " + rankProperty.Name + ": " + e.Message); - } - } - } - } - } + if (playerRanks.Count > 0) + { + this.Debug("Ranks: " + string.Join(", ", playerRanks)); + + // Check every rank from the rank system in the order they are registered in the config until a vanillarank entry is found + JProperty[] ranks = permissions.Properties().ToArray(); + foreach (JProperty rankProperty in ranks) + { + // Check if the player has the rank + if (playerRanks.Contains(rankProperty.Name) || rankProperty.Name == defaultRank) + { + try + { + // Checks if the rank has a vanillarank entry + string vanillarank = permissions.SelectToken(rankProperty.Name + ".vanillarank")?.Value(); + if (vanillarank != null) + { + player.SetRank(null, null, vanillarank); + this.Debug("Set vanilla rank for " + player.Name + " to " + rankProperty.Name); + return; + } + } + catch (Exception e) + { + this.Verbose("Error attempting to parse vanilla rank entry of rank " + rankProperty.Name + ": " + e.Message); + } + } + } + } + } /// /// Logs to console if debug mode is on. /// /// Message to send. - public new void Debug(string message) - { - if(debug) - { - this.Info(message); - } - } + public new void Debug(string message) + { + if(debug) + { + this.Info(message); + } + } /// /// Logs to console if verbose is on. /// /// Message to send. - public void Verbose(string message) - { - if (verbose) - { - this.Info(message); - } - } - - ///////////////////////////////// - // Commands - //////////////////////////////// - private class ReloadCommand : ICommandHandler - { - private SCPermissions plugin; - - public ReloadCommand(SCPermissions plugin) - { - this.plugin = plugin; - } - - public string GetCommandDescription() - { - return "Reloads the config and player data."; - } - - public string GetUsage() - { - return "scperms_reload"; - } - - public string[] OnCall(ICommandSender sender, string[] args) - { - if (sender is Player player) - { - if (!player.HasPermission("scpermissions.reload")) - { - return new[] { "You don't have permission to use that command." }; - } - } - - plugin.Info("Reloading plugin..."); - plugin.LoadConfig(); - plugin.LoadPlayerData(); - return new[] { "Reload complete." }; - } - } - - private class GiveRankCommand : ICommandHandler - { - private SCPermissions plugin; - - public GiveRankCommand(SCPermissions plugin) - { - this.plugin = plugin; - } - - public string GetCommandDescription() - { - return "Gives a rank to a player."; - } - - public string GetUsage() - { - return "scperms_giverank "; - } - - public string[] OnCall(ICommandSender sender, string[] args) - { - if (args.Length > 1) - { - if (sender is Player player) - { - if (!player.HasPermission("scpermissions.giverank")) - { - return new[] { "You don't have permission to use that command." }; - } - - if(!plugin.RankIsHigherThan(player.UserId, args[1])) - { - return new[] { "You are not allowed to edit players with ranks equal or above your own." }; - } - } - - if (plugin.GiveRank(args[1], args[0])) - { - return new[] { "Added the rank " + args[0] + " to " + args[1] + "." }; - } - else - { - return new[] { "Could not add that rank. Does the rank not exist or does the player already have it?" }; - } - } - else - { - return new[] { "Not enough arguments." }; - } - } - } - - private class GiveTempRankCommand : ICommandHandler - { - private SCPermissions plugin; - - public GiveTempRankCommand(SCPermissions plugin) - { - this.plugin = plugin; - } - - public string GetCommandDescription() - { - return "Gives a rank to a player for this session only, does not get saved on server restart."; - } - - public string GetUsage() - { - return "scperms_givetemprank "; - } - - public string[] OnCall(ICommandSender sender, string[] args) - { - try - { - if (args.Length > 1) - { - if (sender is Player player) - { - if (!player.HasPermission("scpermissions.givetemprank")) - { - return new[] {"You don't have permission to use that command."}; - } - - if (!plugin.RankIsHigherThan(player.UserId, args[1])) - { - return new[] {"You are not allowed to edit players with ranks equal or above your own."}; - } - } - - if (plugin.GiveTempRank(args[1], args[0])) - { - return new[] {"Added the rank " + args[0] + " to " + args[1] + "."}; - } - else - { - return new[] { "Could not add that rank. Does the rank not exist or does the player already have it?" }; - } - - } - else - { - return new[] {"Not enough arguments."}; - } - } - catch (Exception e) - { - this.plugin.Error("Error occured: " + e); - return new[] { "Error occured." }; + public void Verbose(string message) + { + if (verbose) + { + this.Info(message); + } + } + + ///////////////////////////////// + // Commands + //////////////////////////////// + private class ReloadCommand : ICommandHandler + { + private SCPermissions plugin; + + public ReloadCommand(SCPermissions plugin) + { + this.plugin = plugin; + } + + public string GetCommandDescription() + { + return "Reloads the config and player data."; + } + + public string GetUsage() + { + return "scperms_reload"; + } + + public string[] OnCall(ICommandSender sender, string[] args) + { + if (sender is Player player) + { + if (!player.HasPermission("scpermissions.reload")) + { + return new[] { "You don't have permission to use that command." }; + } } - } - } + + plugin.Info("Reloading plugin..."); + plugin.LoadConfig(); + plugin.LoadPlayerData(); + return new[] { "Reload complete." }; + } + } + + private class GiveRankCommand : ICommandHandler + { + private SCPermissions plugin; + + public GiveRankCommand(SCPermissions plugin) + { + this.plugin = plugin; + } + + public string GetCommandDescription() + { + return "Gives a rank to a player."; + } + + public string GetUsage() + { + return "scperms_giverank "; + } + + public string[] OnCall(ICommandSender sender, string[] args) + { + if (args.Length > 1) + { + if (sender is Player player) + { + if (!player.HasPermission("scpermissions.giverank")) + { + return new[] { "You don't have permission to use that command." }; + } + + if(!plugin.RankIsHigherThan(player.UserId, args[1])) + { + return new[] { "You are not allowed to edit players with ranks equal or above your own." }; + } + } + + if (plugin.GiveRank(args[1], args[0])) + { + return new[] { "Added the rank " + args[0] + " to " + args[1] + "." }; + } + else + { + return new[] { "Could not add that rank. Does the rank not exist or does the player already have it?" }; + } + } + else + { + return new[] { "Not enough arguments." }; + } + } + } + + private class GiveTempRankCommand : ICommandHandler + { + private SCPermissions plugin; + + public GiveTempRankCommand(SCPermissions plugin) + { + this.plugin = plugin; + } + + public string GetCommandDescription() + { + return "Gives a rank to a player for this session only, does not get saved on server restart."; + } + + public string GetUsage() + { + return "scperms_givetemprank "; + } + + public string[] OnCall(ICommandSender sender, string[] args) + { + try + { + if (args.Length > 1) + { + if (sender is Player player) + { + if (!player.HasPermission("scpermissions.givetemprank")) + { + return new[] {"You don't have permission to use that command."}; + } + + if (!plugin.RankIsHigherThan(player.UserId, args[1])) + { + return new[] {"You are not allowed to edit players with ranks equal or above your own."}; + } + } + + if (plugin.GiveTempRank(args[1], args[0])) + { + return new[] {"Added the rank " + args[0] + " to " + args[1] + "."}; + } + else + { + return new[] { "Could not add that rank. Does the rank not exist or does the player already have it?" }; + } + + } + else + { + return new[] {"Not enough arguments."}; + } + } + catch (Exception e) + { + this.plugin.Error("Error occured: " + e); + return new[] { "Error occured." }; + } + } + } private class RemoveRankCommand : ICommandHandler - { - private SCPermissions plugin; - - public RemoveRankCommand(SCPermissions plugin) - { - this.plugin = plugin; - } - - public string GetCommandDescription() - { - return "Revokes a rank from a player."; - } - - public string GetUsage() - { - return "scperms_removerank "; - } - - public string[] OnCall(ICommandSender sender, string[] args) - { - if (args.Length > 1) - { - if (sender is Player player) - { - if (!player.HasPermission("scpermissions.removerank")) - { - return new[] { "You don't have permission to use that command." }; - } - - if (!plugin.RankIsHigherThan(player.UserId, args[1])) - { - return new[] { "You are not allowed to edit players with ranks equal or above your own." }; - } - } - - if (plugin.RemoveRank(args[1], args[0])) - { - return new[] { "Removed the rank " + args[0] + " from " + args[1] + "." }; - } - else - { - return new[] { "Could not remove that rank. Does the player not have it?" }; - } - - } - else - { - return new[] { "Not enough arguments." }; - } - } - } + { + private SCPermissions plugin; + + public RemoveRankCommand(SCPermissions plugin) + { + this.plugin = plugin; + } + + public string GetCommandDescription() + { + return "Revokes a rank from a player."; + } + + public string GetUsage() + { + return "scperms_removerank "; + } + + public string[] OnCall(ICommandSender sender, string[] args) + { + if (args.Length > 1) + { + if (sender is Player player) + { + if (!player.HasPermission("scpermissions.removerank")) + { + return new[] { "You don't have permission to use that command." }; + } + + if (!plugin.RankIsHigherThan(player.UserId, args[1])) + { + return new[] { "You are not allowed to edit players with ranks equal or above your own." }; + } + } + + if (plugin.RemoveRank(args[1], args[0])) + { + return new[] { "Removed the rank " + args[0] + " from " + args[1] + "." }; + } + else + { + return new[] { "Could not remove that rank. Does the player not have it?" }; + } + + } + else + { + return new[] { "Not enough arguments." }; + } + } + } private class ListRanksCommand : ICommandHandler { @@ -754,100 +750,100 @@ public string[] OnCall(ICommandSender sender, string[] args) } private class VerboseCommand : ICommandHandler - { - private SCPermissions plugin; - - public VerboseCommand(SCPermissions plugin) - { - this.plugin = plugin; - } - - public string GetCommandDescription() - { - return "Toggles verbose messages."; - } - - public string GetUsage() - { - return "scperms_verbose"; - } - - public string[] OnCall(ICommandSender sender, string[] args) - { - if (sender is Player player) - { - if (!player.HasPermission("scpermissions.verbose")) - { - return new[] { "You don't have permission to use that command." }; - } - } - - plugin.verbose = !plugin.verbose; - return new[] { "Verbose messages: " + plugin.verbose }; - } - } - - private class DebugCommand : ICommandHandler - { - private SCPermissions plugin; - - public DebugCommand(SCPermissions plugin) - { - this.plugin = plugin; - } - - public string GetCommandDescription() - { - return "Toggles debug messages."; - } - - public string GetUsage() - { - return "scperms_debug"; - } - - public string[] OnCall(ICommandSender sender, string[] args) - { - if (sender is Player player) - { - if (!player.HasPermission("scpermissions.debug")) - { - return new[] { "You don't have permission to use that command." }; - } - } - - plugin.debug = !plugin.debug; - return new[] { "Debug messages: " + plugin.debug }; - } - } - } + { + private SCPermissions plugin; + + public VerboseCommand(SCPermissions plugin) + { + this.plugin = plugin; + } + + public string GetCommandDescription() + { + return "Toggles verbose messages."; + } + + public string GetUsage() + { + return "scperms_verbose"; + } + + public string[] OnCall(ICommandSender sender, string[] args) + { + if (sender is Player player) + { + if (!player.HasPermission("scpermissions.verbose")) + { + return new[] { "You don't have permission to use that command." }; + } + } + + plugin.verbose = !plugin.verbose; + return new[] { "Verbose messages: " + plugin.verbose }; + } + } + + private class DebugCommand : ICommandHandler + { + private SCPermissions plugin; + + public DebugCommand(SCPermissions plugin) + { + this.plugin = plugin; + } + + public string GetCommandDescription() + { + return "Toggles debug messages."; + } + + public string GetUsage() + { + return "scperms_debug"; + } + + public string[] OnCall(ICommandSender sender, string[] args) + { + if (sender is Player player) + { + if (!player.HasPermission("scpermissions.debug")) + { + return new[] { "You don't have permission to use that command." }; + } + } + + plugin.debug = !plugin.debug; + return new[] { "Debug messages: " + plugin.debug }; + } + } + } /// /// Sets vanilla ranks for joining players /// - internal class PlayerJoinHandler : IEventHandlerPlayerJoin - { - private SCPermissions plugin; - - public PlayerJoinHandler(SCPermissions plugin) - { - this.plugin = plugin; - } - - public void OnPlayerJoin(PlayerJoinEvent ev) - { - try - { - new Task(() => - { - plugin.RefreshVanillaRank(ev.Player); - - }).Start(); - } - catch (Exception e) - { - plugin.Verbose(e.ToString()); - } - } - } + internal class PlayerJoinHandler : IEventHandlerPlayerJoin + { + private SCPermissions plugin; + + public PlayerJoinHandler(SCPermissions plugin) + { + this.plugin = plugin; + } + + public void OnPlayerJoin(PlayerJoinEvent ev) + { + try + { + new Task(() => + { + plugin.RefreshVanillaRank(ev.Player); + + }).Start(); + } + catch (Exception e) + { + plugin.Verbose(e.ToString()); + } + } + } }