Style: Make LevelAccess cleaner

This commit is contained in:
UnknownShadow200 2016-11-30 17:24:31 +11:00
parent 11ccfe430c
commit 04b8e79664
10 changed files with 43 additions and 45 deletions

View File

@ -38,9 +38,8 @@ namespace MCGalaxy.Commands.Moderation {
who.color = newRank.color;
who.group = newRank;
LevelAccessResult access = who.level.BuildAccess.Check(who, false);
who.AllowBuild = access == LevelAccessResult.Whitelisted
|| access == LevelAccessResult.Allowed;
LevelAccess access = who.level.BuildAccess.Check(who);
who.AllowBuild = access == LevelAccess.Whitelisted || access == LevelAccess.Allowed;
who.SetPrefix();
who.Send(Packet.UserType(who));

View File

@ -27,7 +27,7 @@ namespace MCGalaxy.Commands.World {
public override void Use(Player p, string message) {
int totalFixed = 0;
Level lvl = p.level;
if (p != null && !lvl.BuildAccess.CheckDetailed(p, false)) {
if (p != null && !lvl.BuildAccess.CheckDetailed(p)) {
Player.Message(p, "Hence you cannot use /fixgrass on this map"); return;
}

View File

@ -44,7 +44,7 @@ namespace MCGalaxy.Commands.World {
}
Level level = args.Length == 1 ? p.level : LevelInfo.FindMatches(p, args[0]);
if (level == null) return;
LevelAccess access = isVisit ? level.VisitAccess : level.BuildAccess;
LevelAccessController access = isVisit ? level.VisitAccess : level.BuildAccess;
string name = args.Length == 1 ? args[0] : args[1];
bool include = name[0] == '+';

View File

@ -78,18 +78,20 @@ namespace MCGalaxy.Commands {
}
static bool CheckVisitPerm(Player p, Player who, bool confirmed) {
LevelAccessResult result = p.level.VisitAccess.Check(who, confirmed);
if (result == LevelAccessResult.Allowed) return true;
if (result == LevelAccessResult.Whitelisted) return true;
LevelAccess result = p.level.VisitAccess.Check(who);
if (result == LevelAccess.Allowed) return true;
if (result == LevelAccess.Whitelisted) return true;
if (result == LevelAccess.AboveMaxRank && confirmed) return true;
if (result == LevelAccess.BelowMinRank && confirmed) return true;
if (result == LevelAccessResult.Blacklisted) {
if (result == LevelAccess.Blacklisted) {
Player.Message(p, "{0} %Sis blacklisted from visiting this map.", who.ColoredName);
return false;
} else if (result == LevelAccessResult.BelowMinRank) {
} else if (result == LevelAccess.BelowMinRank) {
Player.Message(p, "Only {0}%S+ may normally visit this map. {1}%S is ranked {2}",
Group.GetColoredName(p.level.permissionvisit),
who.ColoredName, who.group.ColoredName);
} else if (result == LevelAccessResult.AboveMaxRank) {
} else if (result == LevelAccess.AboveMaxRank) {
Player.Message(p, "Only {0}%S and below may normally visit this map. {1}%S is ranked {2}",
Group.GetColoredName(p.level.pervisitmax),
who.ColoredName, who.group.ColoredName);

View File

@ -52,7 +52,7 @@ namespace MCGalaxy.Drawing.Ops {
Player.Message(p, "Drawing commands are turned off on this map.");
return false;
}
if (op.Level != null && op.Level.BuildAccess.Check(p) == LevelAccessResult.Blacklisted) {
if (op.Level != null && op.Level.BuildAccess.Check(p) == LevelAccess.Blacklisted) {
Player.Message(p, "You are blacklisted from building in this map, " +
"hence you cannot draw in this map");
return false;

View File

@ -228,14 +228,14 @@ namespace MCGalaxy {
bool CheckRank(Player p) {
if (p.ZoneSpam <= DateTime.UtcNow) {
BuildAccess.CheckDetailed(p, false);
BuildAccess.CheckDetailed(p);
p.ZoneSpam = DateTime.UtcNow.AddSeconds(2);
}
if (p.level == this) return p.AllowBuild;
LevelAccessResult access = BuildAccess.Check(p, false);
return access == LevelAccessResult.Whitelisted
|| access == LevelAccessResult.Allowed;
LevelAccess access = BuildAccess.Check(p);
return access == LevelAccess.Whitelisted
|| access == LevelAccess.Allowed;
}
public bool CheckAffectPermissions(Player p, ushort x, ushort y, ushort z,

View File

@ -147,7 +147,7 @@ namespace MCGalaxy {
public bool Buildable = true;
[ConfigBool("Deletable", "Permissions", null, true)]
public bool Deletable = true;
public LevelAccess VisitAccess, BuildAccess;
public LevelAccessController VisitAccess, BuildAccess;
[ConfigPerm("PerBuildMax", "Permissions", null, LevelPermission.Nobody, true)]
public LevelPermission perbuildmax = LevelPermission.Nobody;

View File

@ -93,8 +93,8 @@ namespace MCGalaxy {
rotx = 0; roty = 0;
ZoneList = new List<Zone>();
VisitAccess = new LevelAccess(this, true);
BuildAccess = new LevelAccess(this, false);
VisitAccess = new LevelAccessController(this, true);
BuildAccess = new LevelAccessController(this, false);
listCheckExists = new SparseBitSet(Width, Height, Length);
listUpdateExists = new SparseBitSet(Width, Height, Length);
}

View File

@ -21,14 +21,14 @@ using System.Collections.Generic;
namespace MCGalaxy {
/// <summary> Encapuslates access permissions (visit or build) for a level. </summary>
public sealed class LevelAccess {
public sealed class LevelAccessController {
/// <summary> Whether these access permissions apply to
/// visit (true) or build (false) permission for the level. </summary>
public readonly bool IsVisit;
readonly Level lvl;
public LevelAccess(Level lvl, bool isVisit) {
public LevelAccessController(Level lvl, bool isVisit) {
this.lvl = lvl;
IsVisit = isVisit;
}
@ -63,38 +63,38 @@ namespace MCGalaxy {
/// <summary> Returns the allowed state for the given player. </summary>
public LevelAccessResult Check(Player p, bool ignoreRankPerm = false) {
public LevelAccess Check(Player p) {
if (Blacklisted.CaselessContains(p.name))
return LevelAccessResult.Blacklisted;
return LevelAccess.Blacklisted;
if (Whitelisted.CaselessContains(p.name))
return LevelAccessResult.Whitelisted;
if (ignoreRankPerm)
return LevelAccessResult.Allowed;
return LevelAccess.Whitelisted;
if (p.Rank < Min)
return LevelAccessResult.BelowMinRank;
return LevelAccess.BelowMinRank;
string maxCmd = IsVisit ? "pervisitmax" : "perbuildmax";
if (p.Rank > Max && !p.group.CanExecute(maxCmd))
return LevelAccessResult.AboveMaxRank;
return LevelAccessResult.Allowed;
return LevelAccess.AboveMaxRank;
return LevelAccess.Allowed;
}
/// <summary> Returns whether the given player is allowed for these access permissions. </summary>
/// <remarks> If the player is not allowed by these access permissions,
/// sends a message to the player describing why they are not. </remarks>
public bool CheckDetailed(Player p, bool ignoreRankPerm = false) {
LevelAccessResult result = Check(p, ignoreRankPerm);
if (result == LevelAccessResult.Allowed) return true;
if (result == LevelAccessResult.Whitelisted) return true;
LevelAccess result = Check(p);
if (result == LevelAccess.Allowed) return true;
if (result == LevelAccess.Whitelisted) return true;
if (result == LevelAccess.AboveMaxRank && ignoreRankPerm) return true;
if (result == LevelAccess.BelowMinRank && ignoreRankPerm) return true;
if (result == LevelAccessResult.Blacklisted) {
if (result == LevelAccess.Blacklisted) {
string action = IsVisit ? "going to" : "building in";
Player.Message(p, "You are blacklisted from {1} {0}.", lvl.name, action);
} else if (result == LevelAccessResult.BelowMinRank) {
} else if (result == LevelAccess.BelowMinRank) {
string action = IsVisit? "go to" : "build in";
Player.Message(p, "Only {2}%S+ may {1} {0}.",
lvl.name, action, Group.GetColoredName(Min));
} else if (result == LevelAccessResult.AboveMaxRank) {
} else if (result == LevelAccess.AboveMaxRank) {
string action = IsVisit? "go to" : "build in";
Player.Message(p, "Only {2} %Sand below may {1} {0}.",
lvl.name, action, Group.GetColoredName(Max));
@ -227,9 +227,8 @@ namespace MCGalaxy {
foreach (Player p in players) {
if (p.level != lvl) continue;
LevelAccessResult access = Check(p, false);
p.AllowBuild = access == LevelAccessResult.Whitelisted
|| access == LevelAccessResult.Allowed;
LevelAccess access = Check(p);
p.AllowBuild = access == LevelAccess.Whitelisted || access == LevelAccess.Allowed;
}
}
@ -239,9 +238,8 @@ namespace MCGalaxy {
foreach (Player p in players) {
if (p.level != lvl) continue;
LevelAccessResult access = Check(p, false);
bool allowVisit = access == LevelAccessResult.Whitelisted
|| access == LevelAccessResult.Allowed;
LevelAccess access = Check(p);
bool allowVisit = access == LevelAccess.Whitelisted || access == LevelAccess.Allowed;
if (allowVisit) continue;
Player.Message(p, "&cNo longer allowed to visit %S{0}", lvl.name);
@ -250,7 +248,7 @@ namespace MCGalaxy {
}
}
public enum LevelAccessResult {
public enum LevelAccess {
/// <summary> The player is whitelisted and always allowed. </summary>
Whitelisted,

View File

@ -312,9 +312,8 @@ namespace MCGalaxy {
useCheckpointSpawn = false;
lastCheckpointIndex = -1;
LevelAccessResult access = level.BuildAccess.Check(this, false);
AllowBuild = access == LevelAccessResult.Whitelisted
|| access == LevelAccessResult.Allowed;
LevelAccess access = level.BuildAccess.Check(this);
AllowBuild = access == LevelAccess.Whitelisted || access == LevelAccess.Allowed;
try {
if (hasBlockDefs) {