From 4c8e6fd95b82f8e6e2445e53f891b5e05d0c33e6 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 24 Aug 2016 16:15:39 +1000 Subject: [PATCH] Implement proper access permission checking for /perbuild too. --- Commands/World/CmdMuseum.cs | 2 +- Commands/World/PermissionCmd.cs | 12 ++++++ Levels/IO/McfFile.cs | 1 - Levels/Level.Blocks.cs | 75 ++++++++++++--------------------- Levels/LevelAccess.cs | 2 +- Network/Player.Networking.cs | 1 + Player/Player.Fields.cs | 2 + 7 files changed, 44 insertions(+), 51 deletions(-) diff --git a/Commands/World/CmdMuseum.cs b/Commands/World/CmdMuseum.cs index cf1d5056d..940b5db02 100644 --- a/Commands/World/CmdMuseum.cs +++ b/Commands/World/CmdMuseum.cs @@ -41,7 +41,7 @@ namespace MCGalaxy.Commands.World { Level lvl = LvlFile.Load(name, path); lvl.setPhysics(0); lvl.backedup = true; - lvl.permissionbuild = LevelPermission.Admin; + lvl.permissionbuild = LevelPermission.Nobody; lvl.jailx = (ushort)(lvl.spawnx * 32); lvl.jaily = (ushort)(lvl.spawny * 32); diff --git a/Commands/World/PermissionCmd.cs b/Commands/World/PermissionCmd.cs index 517707de2..3a3cd2df0 100644 --- a/Commands/World/PermissionCmd.cs +++ b/Commands/World/PermissionCmd.cs @@ -49,7 +49,9 @@ namespace MCGalaxy.Commands.World { } setter(level, grp.Permission); + UpdateAllowBuild(level); Level.SaveSettings(level); + Server.s.Log(level.name + " " + target + " permission changed to " + grp.Permission + "."); Chat.MessageLevel(level, target + " permission changed to " + grp.ColoredName + "%S."); if (p == null || p.level != level) @@ -95,12 +97,22 @@ namespace MCGalaxy.Commands.World { list.Add(name); other.CaselessRemove(name); + UpdateAllowBuild(level); Level.SaveSettings(level); + string msg = name + " was " + target + " " + mode + "ed"; Server.s.Log(msg + " on " + level.name); Chat.MessageLevel(level, msg); if (p == null || p.level != level) Player.Message(p, msg + " on {0}.", level.name); } + + static void UpdateAllowBuild(Level level) { + Player[] players = PlayerInfo.Online.Items; + foreach (Player p in players) { + if (p.level != level) continue; + p.AllowBuild = level.BuildAccess.Check(p, false); + } + } } } \ No newline at end of file diff --git a/Levels/IO/McfFile.cs b/Levels/IO/McfFile.cs index 9f7689c00..3f5c59cf1 100644 --- a/Levels/IO/McfFile.cs +++ b/Levels/IO/McfFile.cs @@ -41,7 +41,6 @@ namespace MCGalaxy.Levels.IO { ushort height = BitConverter.ToUInt16(header, 4); Level lvl = new Level(name, width, height, length); - lvl.permissionbuild = (LevelPermission)30; lvl.spawnx = BitConverter.ToUInt16(header, 6); lvl.spawnz = BitConverter.ToUInt16(header, 8); lvl.spawny = BitConverter.ToUInt16(header, 10); diff --git a/Levels/Level.Blocks.cs b/Levels/Level.Blocks.cs index c8c9173e1..2614a3685 100644 --- a/Levels/Level.Blocks.cs +++ b/Levels/Level.Blocks.cs @@ -173,28 +173,27 @@ namespace MCGalaxy { return true; } - bool CheckZonePerms(Player p, ushort x, ushort y, ushort z, - ref bool AllowBuild, ref bool inZone, ref string Owners) { + bool CheckZonePerms(Player p, ushort x, ushort y, ushort z, ref bool inZone) { if (p.Rank < LevelPermission.Admin) { - bool foundDel = FindZones(p, x, y, z, ref inZone, ref AllowBuild, ref Owners); - if (!AllowBuild) { - if (p.ZoneSpam <= DateTime.UtcNow) { - if (Owners != "") - Player.Message(p, "This zone belongs to &b" + Owners.Remove(0, 2) + "."); - else - Player.Message(p, "This zone belongs to no one."); - p.ZoneSpam = DateTime.UtcNow.AddSeconds(2); - } - return false; - } + string owners = ""; + bool zoneAllow = FindZones(p, x, y, z, ref inZone, ref owners); + if (zoneAllow) return true; + if (p.ZoneSpam > DateTime.UtcNow) return false; + + if (owners != "") + Player.Message(p, "This zone belongs to &b" + owners.Remove(0, 2) + "."); + else + Player.Message(p, "This zone belongs to no one."); + p.ZoneSpam = DateTime.UtcNow.AddSeconds(2); + return false; } return true; } bool FindZones(Player p, ushort x, ushort y, ushort z, - ref bool inZone, ref bool AllowBuild, ref string Owners) { - if (ZoneList.Count == 0) { AllowBuild = true; return false; } - bool foundDel = false; + ref bool inZone, ref string Owners) { + if (ZoneList.Count == 0) return true; + bool zoneAllow = true; for (int i = 0; i < ZoneList.Count; i++) { Zone zn = ZoneList[i]; @@ -204,39 +203,23 @@ namespace MCGalaxy { inZone = true; if (zn.Owner.Length >= 3 && zn.Owner.StartsWith("grp")) { string grpName = zn.Owner.Substring(3); - if (Group.Find(grpName).Permission <= p.Rank) { - AllowBuild = true; break; - } - AllowBuild = false; + if (Group.Find(grpName).Permission <= p.Rank) return true; Owners += ", " + grpName; } else { - if (zn.Owner.CaselessEq(p.name)) { - AllowBuild = true; break; - } - AllowBuild = false; + if (zn.Owner.CaselessEq(p.name)) return true; Owners += ", " + zn.Owner; } + zoneAllow = false; } - return foundDel; + return zoneAllow; } - bool CheckRank(Player p, bool AllowBuild, bool inZone) { - if (p.Rank < permissionbuild && (!inZone || !AllowBuild)) { - if (p.ZoneSpam <= DateTime.UtcNow) { - Player.Message(p, "Must be at least " + PermissionToName(permissionbuild) + " to build here"); - p.ZoneSpam = DateTime.UtcNow.AddSeconds(2); - } - return false; + bool CheckRank(Player p) { + if (p.ZoneSpam <= DateTime.UtcNow) { + BuildAccess.CheckDetailed(p, false); + p.ZoneSpam = DateTime.UtcNow.AddSeconds(2); } - - if (p.Rank > perbuildmax && (!inZone || !AllowBuild) && !p.group.CanExecute("perbuildmax")) { - if (p.ZoneSpam <= DateTime.UtcNow) { - Player.Message(p, "Your rank must be " + perbuildmax + " or lower to build here!"); - p.ZoneSpam = DateTime.UtcNow.AddSeconds(2); - } - return false; - } - return true; + return p.AllowBuild; } public bool CheckAffectPermissions(Player p, ushort x, ushort y, ushort z, @@ -244,13 +227,9 @@ namespace MCGalaxy { if (!Block.AllowBreak(b) && !Block.canPlace(p, b) && !Block.BuildIn(b)) return false; if (p.PlayingTntWars && !CheckTNTWarsChange(p, x, y, z, ref type)) return false; - string Owners = ""; - bool AllowBuild = true, inZone = false; - if (!CheckZonePerms(p, x, y, z, ref AllowBuild, ref inZone, ref Owners)) - return false; - if (Owners.Length == 0 && !CheckRank(p, AllowBuild, inZone)) - return false; - return true; + bool inZone = false; + if (!CheckZonePerms(p, x, y, z, ref inZone)) return false; + return inZone || CheckRank(p); } public void Blockchange(Player p, ushort x, ushort y, ushort z, diff --git a/Levels/LevelAccess.cs b/Levels/LevelAccess.cs index 70fe33e92..31e3156f4 100644 --- a/Levels/LevelAccess.cs +++ b/Levels/LevelAccess.cs @@ -86,7 +86,7 @@ namespace MCGalaxy { if (p.Rank > Max && !p.group.CanExecute(maxCmd)) { Group grp = Group.findPerm(Max); string grpName = grp == null ? "&f" + Max : grp.ColoredName; - Player.Message(p, "Only {2} and below may {1} in {0}.", name, action, grpName); return false; + Player.Message(p, "Only {2}%S and below may {1} {0}.", name, action, grpName); return false; } return true; } diff --git a/Network/Player.Networking.cs b/Network/Player.Networking.cs index ddb83d50c..c854ac974 100644 --- a/Network/Player.Networking.cs +++ b/Network/Player.Networking.cs @@ -321,6 +321,7 @@ namespace MCGalaxy { bool success = true; useCheckpointSpawn = false; lastCheckpointIndex = -1; + AllowBuild = level.BuildAccess.Check(this, false); try { if (hasBlockDefs) { diff --git a/Player/Player.Fields.cs b/Player/Player.Fields.cs index 4c4c3bc1f..2f467522d 100644 --- a/Player/Player.Fields.cs +++ b/Player/Player.Fields.cs @@ -149,6 +149,8 @@ namespace MCGalaxy { // Only used for possession. //Using for anything else can cause unintended effects! public bool canBuild = true; + /// Whether the player has build permission in the current world. + public bool AllowBuild = true; public int money, loginMoney; public long overallBlocks, TotalDrawn, TotalPlaced, TotalDeleted;