Skip to content

Commit

Permalink
Simplify permission classes a bit and remove obsolete Json.Parse method
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Nov 5, 2022
1 parent 1dc3e80 commit 555cdab
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 57 deletions.
10 changes: 1 addition & 9 deletions MCGalaxy/Config/JSON.cs
Expand Up @@ -296,15 +296,7 @@ public class JsonConfigWriter : JsonWriter {
}

public static class Json {

[Obsolete("Use JsonReader instead")]
public static object Parse(string s, out bool success) {
JsonReader reader = new JsonReader(s);
object obj = reader.Parse();
success = !reader.Failed;
return obj;
}


[Obsolete("Use JsonWriter instead")]
public static void Serialise(TextWriter dst, ConfigElement[] elems, object instance) {
JsonConfigWriter w = new JsonConfigWriter(dst, elems);
Expand Down
18 changes: 9 additions & 9 deletions MCGalaxy/Config/Permissions/BlockPerms.cs
Expand Up @@ -20,23 +20,23 @@
using System.IO;
using BlockID = System.UInt16;

namespace MCGalaxy.Blocks {

namespace MCGalaxy.Blocks
{
/// <summary> Represents which ranks are allowed (and which are disallowed) to use a block. </summary>
public sealed class BlockPerms : ItemPerms {
public sealed class BlockPerms : ItemPerms
{
public BlockID ID;
public override string ItemName { get { return ID.ToString(); } }

static BlockPerms[] List = new BlockPerms[Block.SUPPORTED_COUNT];


public BlockPerms(BlockID id, LevelPermission min, List<LevelPermission> allowed,
List<LevelPermission> disallowed) : base(min, allowed, disallowed) {
public BlockPerms(BlockID id, LevelPermission min) : base(min) {
ID = id;
}

public BlockPerms Copy() {
BlockPerms copy = new BlockPerms(ID, 0, null, null);
BlockPerms copy = new BlockPerms(ID, 0);
CopyTo(copy); return copy;
}

Expand All @@ -49,10 +49,10 @@ public sealed class BlockPerms : ItemPerms {
List<LevelPermission> allowed, List<LevelPermission> disallowed) {
BlockPerms perms = List[b];
if (perms == null) {
List[b] = new BlockPerms(b, min, allowed, disallowed);
} else {
perms.Init(min, allowed, disallowed);
perms = new BlockPerms(b, min);
List[b] = perms;
}
perms.Init(min, allowed, disallowed);
}


Expand Down
36 changes: 19 additions & 17 deletions MCGalaxy/Config/Permissions/CommandExtraPerms.cs
Expand Up @@ -19,48 +19,50 @@
using System.Collections.Generic;
using System.IO;

namespace MCGalaxy.Commands {

namespace MCGalaxy.Commands
{
/// <summary> Represents additional permissions required to perform a special action in a command. </summary>
/// <remarks> For example, /color command has an extra permission for changing the color of other players. </remarks>
public class CommandExtraPerms : ItemPerms {

public class CommandExtraPerms : ItemPerms
{
public string CmdName, Desc = "";
public int Num;
public override string ItemName { get { return CmdName + ":" + Num; } }

public CommandExtraPerms(string cmd, int num, string desc,
LevelPermission min, List<LevelPermission> allowed,
List<LevelPermission> disallowed) : base(min, allowed, disallowed) {
static List<CommandExtraPerms> list = new List<CommandExtraPerms>();


public CommandExtraPerms(string cmd, int num, string desc, LevelPermission min) : base(min) {
CmdName = cmd; Num = num; Desc = desc;
}


public CommandExtraPerms Copy() {
CommandExtraPerms copy = new CommandExtraPerms(CmdName, Num, Desc, 0, null, null);
CommandExtraPerms copy = new CommandExtraPerms(CmdName, Num, Desc, 0);
CopyTo(copy); return copy;
}

static List<CommandExtraPerms> list = new List<CommandExtraPerms>();

public static CommandExtraPerms Find(string cmd, int num) {
foreach (CommandExtraPerms perms in list) {
foreach (CommandExtraPerms perms in list)
{
if (perms.CmdName.CaselessEq(cmd) && perms.Num == num) return perms;
}
return null;
}

public static List<CommandExtraPerms> FindAll(string cmd) {
List<CommandExtraPerms> all = new List<CommandExtraPerms>();
foreach (CommandExtraPerms perms in list) {
foreach (CommandExtraPerms perms in list)
{
if (perms.CmdName.CaselessEq(cmd) && perms.Desc.Length > 0) all.Add(perms);
}
return all;
}


static CommandExtraPerms Add(string cmd, int num, string desc, LevelPermission min,
List<LevelPermission> allowed, List<LevelPermission> disallowed) {
CommandExtraPerms perms = new CommandExtraPerms(cmd, num, desc, min, allowed, disallowed);
static CommandExtraPerms Add(string cmd, int num, string desc, LevelPermission min) {
CommandExtraPerms perms = new CommandExtraPerms(cmd, num, desc, min);
list.Add(perms);
return perms;
}
Expand All @@ -70,17 +72,17 @@ public class CommandExtraPerms : ItemPerms {
List<LevelPermission> allowed, List<LevelPermission> disallowed) {
CommandExtraPerms perms = Find(cmd, num);
if (perms == null) {
Add(cmd, num, desc, min, allowed, disallowed);
perms = Add(cmd, num, desc, min);
} else {
perms.CmdName = cmd; perms.Num = num;
if (!String.IsNullOrEmpty(desc)) perms.Desc = desc;
perms.Init(min, allowed, disallowed);
}
perms.Init(min, allowed, disallowed);
}

/// <summary> Gets or adds the nth extra permission for the given command. </summary>
public static CommandExtraPerms GetOrAdd(string cmd, int num, LevelPermission min) {
return Find(cmd, num) ?? Add(cmd, num, "", min, null, null);
return Find(cmd, num) ?? Add(cmd, num, "", min);
}

public void MessageCannotUse(Player p) {
Expand Down
16 changes: 7 additions & 9 deletions MCGalaxy/Config/Permissions/CommandPerms.cs
Expand Up @@ -30,13 +30,12 @@ public sealed class CommandPerms : ItemPerms
static List<CommandPerms> List = new List<CommandPerms>();


public CommandPerms(string cmd, LevelPermission min, List<LevelPermission> allowed,
List<LevelPermission> disallowed) : base(min, allowed, disallowed) {
public CommandPerms(string cmd, LevelPermission min) : base(min) {
CmdName = cmd;
}

public CommandPerms Copy() {
CommandPerms copy = new CommandPerms(CmdName, 0, null, null);
CommandPerms copy = new CommandPerms(CmdName, 0);
CopyTo(copy); return copy;
}

Expand All @@ -59,9 +58,8 @@ public sealed class CommandPerms : ItemPerms
}


static CommandPerms Add(string cmd, LevelPermission min,
List<LevelPermission> allowed, List<LevelPermission> disallowed) {
CommandPerms perms = new CommandPerms(cmd, min, allowed, disallowed);
static CommandPerms Add(string cmd, LevelPermission min) {
CommandPerms perms = new CommandPerms(cmd, min);
List.Add(perms);
return perms;
}
Expand All @@ -71,16 +69,16 @@ public sealed class CommandPerms : ItemPerms
List<LevelPermission> allowed, List<LevelPermission> disallowed) {
CommandPerms perms = Find(cmd);
if (perms == null) {
Add(cmd, min, allowed, disallowed);
perms = Add(cmd, min);
} else {
perms.CmdName = cmd;
perms.Init(min, allowed, disallowed);
}
perms.Init(min, allowed, disallowed);
}

/// <summary> Gets or adds permissions for the given command. </summary>
public static CommandPerms GetOrAdd(string cmd, LevelPermission min) {
return Find(cmd) ?? Add(cmd, min, null, null);
return Find(cmd) ?? Add(cmd, min);
}


Expand Down
17 changes: 7 additions & 10 deletions MCGalaxy/Config/Permissions/ItemPerms.cs
Expand Up @@ -20,20 +20,16 @@
using System.IO;
using System.Text;

namespace MCGalaxy {

namespace MCGalaxy
{
/// <summary> Represents which ranks are allowed (and which are disallowed) to use an item. </summary>
public class ItemPerms {
public class ItemPerms
{
public virtual string ItemName { get { return ""; } }
public LevelPermission MinRank;
public List<LevelPermission> Allowed, Disallowed;

public ItemPerms(LevelPermission min) { Init(min, null, null); }

public ItemPerms(LevelPermission min, List<LevelPermission> allowed,
List<LevelPermission> disallowed) {
Init(min, allowed, disallowed);
}
public ItemPerms(LevelPermission min) { MinRank = min; }

protected void Init(LevelPermission min, List<LevelPermission> allowed,
List<LevelPermission> disallowed) {
Expand Down Expand Up @@ -111,7 +107,8 @@ public class ItemPerms {
if (input == null || input.Length == 0) return null;

List<LevelPermission> perms = new List<LevelPermission>();
foreach (string perm in input.SplitComma()) {
foreach (string perm in input.SplitComma())
{
perms.Add((LevelPermission)int.Parse(perm));
}
return perms;
Expand Down
7 changes: 4 additions & 3 deletions MCGalaxy/Player/Group.cs
Expand Up @@ -23,10 +23,11 @@
using MCGalaxy.Config;
using MCGalaxy.Events.GroupEvents;

namespace MCGalaxy {
namespace MCGalaxy
{
/// <summary> This is the group object, where ranks and their data are stored </summary>
public sealed partial class Group {

public sealed class Group
{
public static Group BannedRank { get { return Find(LevelPermission.Banned); } }
public static Group GuestRank { get { return Find(LevelPermission.Guest); } }
public static Group DefaultRank;
Expand Down

0 comments on commit 555cdab

Please sign in to comment.