From 04b8e7966462d00aae6955ab245daec7007cc759 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 30 Nov 2016 17:24:31 +1100 Subject: [PATCH] Style: Make LevelAccess cleaner --- MCGalaxy/Commands/Moderation/RankCmd.cs | 5 +-- MCGalaxy/Commands/World/CmdFixGrass.cs | 2 +- MCGalaxy/Commands/World/PermissionCmd.cs | 2 +- MCGalaxy/Commands/other/CmdSummon.cs | 14 ++++--- MCGalaxy/Drawing/DrawOps/DrawOp.Performer.cs | 2 +- MCGalaxy/Levels/Level.Blocks.cs | 8 ++-- MCGalaxy/Levels/Level.Fields.cs | 2 +- MCGalaxy/Levels/Level.cs | 4 +- MCGalaxy/Levels/LevelAccess.cs | 44 ++++++++++---------- MCGalaxy/Network/Player.Networking.cs | 5 +-- 10 files changed, 43 insertions(+), 45 deletions(-) diff --git a/MCGalaxy/Commands/Moderation/RankCmd.cs b/MCGalaxy/Commands/Moderation/RankCmd.cs index f2572bc4c..0a2b6c8ae 100644 --- a/MCGalaxy/Commands/Moderation/RankCmd.cs +++ b/MCGalaxy/Commands/Moderation/RankCmd.cs @@ -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)); diff --git a/MCGalaxy/Commands/World/CmdFixGrass.cs b/MCGalaxy/Commands/World/CmdFixGrass.cs index 5f70d647a..a83b6196d 100644 --- a/MCGalaxy/Commands/World/CmdFixGrass.cs +++ b/MCGalaxy/Commands/World/CmdFixGrass.cs @@ -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; } diff --git a/MCGalaxy/Commands/World/PermissionCmd.cs b/MCGalaxy/Commands/World/PermissionCmd.cs index a28373cd9..f800a1476 100644 --- a/MCGalaxy/Commands/World/PermissionCmd.cs +++ b/MCGalaxy/Commands/World/PermissionCmd.cs @@ -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] == '+'; diff --git a/MCGalaxy/Commands/other/CmdSummon.cs b/MCGalaxy/Commands/other/CmdSummon.cs index 66a07f8ce..2b1f86560 100644 --- a/MCGalaxy/Commands/other/CmdSummon.cs +++ b/MCGalaxy/Commands/other/CmdSummon.cs @@ -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); diff --git a/MCGalaxy/Drawing/DrawOps/DrawOp.Performer.cs b/MCGalaxy/Drawing/DrawOps/DrawOp.Performer.cs index fd49e675e..49224fe4e 100644 --- a/MCGalaxy/Drawing/DrawOps/DrawOp.Performer.cs +++ b/MCGalaxy/Drawing/DrawOps/DrawOp.Performer.cs @@ -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; diff --git a/MCGalaxy/Levels/Level.Blocks.cs b/MCGalaxy/Levels/Level.Blocks.cs index f17ca663e..203bdef04 100644 --- a/MCGalaxy/Levels/Level.Blocks.cs +++ b/MCGalaxy/Levels/Level.Blocks.cs @@ -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, diff --git a/MCGalaxy/Levels/Level.Fields.cs b/MCGalaxy/Levels/Level.Fields.cs index d5dea130d..40826481a 100644 --- a/MCGalaxy/Levels/Level.Fields.cs +++ b/MCGalaxy/Levels/Level.Fields.cs @@ -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; diff --git a/MCGalaxy/Levels/Level.cs b/MCGalaxy/Levels/Level.cs index b6faedaf1..3e811c55f 100644 --- a/MCGalaxy/Levels/Level.cs +++ b/MCGalaxy/Levels/Level.cs @@ -93,8 +93,8 @@ namespace MCGalaxy { rotx = 0; roty = 0; ZoneList = new List(); - 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); } diff --git a/MCGalaxy/Levels/LevelAccess.cs b/MCGalaxy/Levels/LevelAccess.cs index 6fbc6f4cc..c024c31c7 100644 --- a/MCGalaxy/Levels/LevelAccess.cs +++ b/MCGalaxy/Levels/LevelAccess.cs @@ -21,14 +21,14 @@ using System.Collections.Generic; namespace MCGalaxy { /// Encapuslates access permissions (visit or build) for a level. - public sealed class LevelAccess { + public sealed class LevelAccessController { /// Whether these access permissions apply to /// visit (true) or build (false) permission for the level. 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 { /// Returns the allowed state for the given player. - 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; } /// Returns whether the given player is allowed for these access permissions. /// If the player is not allowed by these access permissions, /// sends a message to the player describing why they are not. 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 { /// The player is whitelisted and always allowed. Whitelisted, diff --git a/MCGalaxy/Network/Player.Networking.cs b/MCGalaxy/Network/Player.Networking.cs index ec3536187..11fefac15 100644 --- a/MCGalaxy/Network/Player.Networking.cs +++ b/MCGalaxy/Network/Player.Networking.cs @@ -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) {