Allow users to add extra command permissions

This commit is contained in:
UnknownShadow200 2017-04-14 11:24:19 +10:00
parent 2d9675c268
commit 16d0028498
3 changed files with 37 additions and 19 deletions

View File

@ -785,7 +785,7 @@ txtBackupLocation.Text = folderDialog.SelectedPath;
Command cmd = Command.all.Find(listCommandsExtraCmdPerms.SelectedItem.ToString());
oldcmd = cmd;
skipExtraPermChanges = true;
extracmdpermnumber.Maximum = CommandExtraPerms.GetMaxNumber(cmd);
extracmdpermnumber.Maximum = MaxExtraPermissionNumber(cmd);
extracmdpermnumber.ReadOnly = extracmdpermnumber.Maximum == 1;
extracmdpermnumber.Value = 1;
skipExtraPermChanges = false;
@ -793,6 +793,16 @@ txtBackupLocation.Text = folderDialog.SelectedPath;
ExtraPermSetDescriptions(cmd, 1);
oldnumber = (int)extracmdpermnumber.Value;
}
private int MaxExtraPermissionNumber(Command cmd) {
var all = CommandExtraPerms.FindAll(cmd.name);
int maxNum = 0;
foreach (CommandExtraPerms.ExtraPerms perms in all) {
maxNum = Math.Max(maxNum, perms.Number);
}
return maxNum;
}
private void SaveOldExtraCustomCmdChanges() {
if (oldcmd == null || skipExtraPermChanges) return;

View File

@ -63,6 +63,16 @@ namespace MCGalaxy {
/// <summary> Returns the lowest rank that has the nth extra permission for a given command. </summary>
public static LevelPermission MinPerm(string cmd, int num = 1) { return Find(cmd, num).MinRank; }
/// <summary> Finds all extra permissions for a given command. </summary>
/// <remarks> list is empty when no extra permissions are found, not null. </remarks>
public static List<ExtraPerms> FindAll(string cmd) {
List<ExtraPerms> allPerms = new List<ExtraPerms>();
foreach (ExtraPerms perms in list) {
if (perms.Command.CaselessEq(cmd)) allPerms.Add(perms);
}
return allPerms;
}
/// <summary> Sets the nth extra permission for a given command. </summary>
public static void Set(string cmd, LevelPermission perm, string desc, int num = 1) {
@ -80,20 +90,13 @@ namespace MCGalaxy {
perms.Description = desc;
perms.Number = num;
}
/// <summary> Returns the highest number/identifier of an extra permission for the given command. </summary>
public static int GetMaxNumber(Command cmd) {
for (int i = 1; ; i++) {
if (Find(cmd.name, i) == null) return (i - 1);
}
}
static readonly object saveLock = new object();
static readonly object ioLock = new object();
/// <summary> Saves the list of all extra permissions. </summary>
public static void Save() {
lock (saveLock) {
lock (ioLock) {
try {
SaveCore();
} catch (Exception ex) {
@ -115,15 +118,21 @@ namespace MCGalaxy {
w.WriteLine("#");
foreach (ExtraPerms perms in list) {
w.WriteLine(perms.Command + ":" + perms.Number + ":" + (int)perms.MinRank + ":" + perms.Description);
w.WriteLine(perms.Command + ":" + perms.Number + ":" + (int)perms.MinRank + ":" + perms.Description);
}
}
}
/// <summary> Loads the list of all extra permissions. </summary>
public static void Load() {
if (!File.Exists(file)) Save();
lock (ioLock) {
if (!File.Exists(file)) Save();
LoadCore();
}
}
static void LoadCore() {
using (StreamReader r = new StreamReader(file)) {
string line;
while ((line = r.ReadLine()) != null) {

View File

@ -52,14 +52,13 @@ namespace MCGalaxy {
Player.Message(p, builder.ToString());
PrintAliases(p, cmd);
CommandPerm[] addPerms = cmd.ExtraPerms;
if (addPerms == null) return;
List<CommandExtraPerms.ExtraPerms> extraPerms = CommandExtraPerms.FindAll(cmd.name);
if (extraPerms.Count == 0) return;
Player.Message(p, "%TExtra permissions:");
for (int i = 0; i < addPerms.Length; i++) {
CommandExtraPerms.ExtraPerms extra = CommandExtraPerms.Find(cmd.name, i + 1);
Player.Message(p, "{0}) {1}%S{2}", i + 1,
Group.GetColoredName(extra.MinRank), extra.Description);
foreach (CommandExtraPerms.ExtraPerms extra in extraPerms) {
Player.Message(p, "{0}) {1}%S{2}", extra.Number,
Group.GetColoredName(extra.MinRank), extra.Description);
}
}