diff --git a/MCGalaxy/Commands/Moderation/ZoneCmds.cs b/MCGalaxy/Commands/Moderation/ZoneCmds.cs index 1ae91c120..16f2cc942 100644 --- a/MCGalaxy/Commands/Moderation/ZoneCmds.cs +++ b/MCGalaxy/Commands/Moderation/ZoneCmds.cs @@ -16,7 +16,7 @@ permissions and limitations under the Licenses. */ using System; -using MCGalaxy.Commands.Moderation; +using MCGalaxy.Commands.World; using MCGalaxy.Maths; namespace MCGalaxy.Commands.Moderation { @@ -32,29 +32,38 @@ namespace MCGalaxy.Commands.Moderation { if (args[0].CaselessEq("add")) { if (args.Length == 1) { Help(p); return; } - if (!CheckAdd(p, args, "Zone add")) return; - - Player.Message(p, "Place or break two blocks to determine the edges."); - Player.Message(p, "Zone for: &b" + args[1] + "."); - p.MakeSelection(2, args[1], AddZone); + CreateZone(p, args, 1); } else if (args[0].CaselessEq("del")) { Player.Message(p, "Place a block where you would like to delete a zone."); p.MakeSelection(1, null, DeleteZone); } else { - if (!CheckAdd(p, args, "Zone add")) return; // TODO: broken - - Player.Message(p, "Place or break two blocks to determine the edges."); - Player.Message(p, "Zone for: &b" + args[0] + "."); - p.MakeSelection(2, args[0], AddZone); + CreateZone(p, args, 0); } } - bool CheckAdd(Player p, string[] args, string cmd) { - if (!Formatter.ValidName(p, args[1], "player or rank")) return false; - - string reason = args.Length > 2 ? args[2] : ""; - args[1] = FindZoneOwner(p, cmd, args[1], ref reason); - return args[1] != null; + void CreateZone(Player p, string[] args, int offset) { + Zone z = new Zone(p.level); + z.Config.Name = args[offset]; + PermissionCmd.Do(p, args, offset + 1, false, z.Access); + + Player.Message(p, "Creating zone " + z.ColoredName); + Player.Message(p, "Place or break two blocks to determine the edges."); + p.MakeSelection(2, z, AddZone); + } + + bool AddZone(Player p, Vec3S32[] marks, object state, ExtBlock block) { + Zone z = (Zone)state; + z.MinX = (ushort)Math.Min(marks[0].X, marks[1].X); + z.MinY = (ushort)Math.Min(marks[0].Y, marks[1].Y); + z.MinZ = (ushort)Math.Min(marks[0].Z, marks[1].Z); + z.MaxX = (ushort)Math.Max(marks[0].X, marks[1].X); + z.MaxY = (ushort)Math.Max(marks[0].Y, marks[1].Y); + z.MaxZ = (ushort)Math.Max(marks[0].Z, marks[1].Z); + + p.level.Zones.Add(z); + p.level.Save(true); + Player.Message(p, "Created zone " + z.ColoredName); + return false; } bool DeleteZone(Player p, Vec3S32[] marks, object state, ExtBlock block) { @@ -66,7 +75,7 @@ namespace MCGalaxy.Commands.Moderation { Zone zn = lvl.Zones[i]; if (P.X < zn.MinX || P.X > zn.MaxX || P.Y < zn.MinY || P.Y > zn.MaxY || P.Z < zn.MinZ || P.Z > zn.MaxZ) continue; - if (!zn.Acess.CheckDetailed(p)) { + if (!zn.Access.CheckDetailed(p)) { Player.Message(p, "Hence, you cannot delete this zone."); continue; } @@ -80,29 +89,6 @@ namespace MCGalaxy.Commands.Moderation { return false; } - bool AddZone(Player p, Vec3S32[] marks, object state, ExtBlock block) { - Zone z = Zone.Create(); - z.MinX = (ushort)Math.Min(marks[0].X, marks[1].X); - z.MinY = (ushort)Math.Min(marks[0].Y, marks[1].Y); - z.MinZ = (ushort)Math.Min(marks[0].Z, marks[1].Z); - z.MaxX = (ushort)Math.Max(marks[0].X, marks[1].X); - z.MaxY = (ushort)Math.Max(marks[0].Y, marks[1].Y); - z.MaxZ = (ushort)Math.Max(marks[0].Z, marks[1].Z); - z.Owner = (string)state; - z.Config.Name = state + state; - - p.level.Zones.Add(z); - p.level.Save(true); - Player.Message(p, "Added zone for &b" + (string)state); - return false; - } - - internal static string FindZoneOwner(Player p, string cmd, string name, ref string reason) { - if (Group.Find(name) != null) - return "grp" + Group.Find(name).Name; - return ModActionCmd.FindName(p, "zone", cmd, "", name, ref reason); - } - public override void Help(Player p) { Player.Message(p, "%T/Zone add [name] %H- Creates a zone only [name] can build in"); Player.Message(p, "%T/Zone add [rank] %H- Creates a zone only [rank]+ can build in"); @@ -131,7 +117,7 @@ namespace MCGalaxy.Commands.Moderation { if (!z.Contains(P.X, P.Y, P.Z)) continue; found = true; - AccessResult status = z.Acess.Check(p); + AccessResult status = z.Access.Check(p); bool allowed = status == AccessResult.Allowed || status == AccessResult.Whitelisted; Player.Message(p, " Zone {0} %S- {1}{2}", z.ColoredName, allowed ? "&a" : "&c", status ); } diff --git a/MCGalaxy/Commands/World/PermissionCmds.cs b/MCGalaxy/Commands/World/PermissionCmds.cs index 890a9d8fa..2451152b2 100644 --- a/MCGalaxy/Commands/World/PermissionCmds.cs +++ b/MCGalaxy/Commands/World/PermissionCmds.cs @@ -24,7 +24,7 @@ namespace MCGalaxy.Commands.World { public override bool museumUsable { get { return false; } } public override LevelPermission defaultRank { get { return LevelPermission.Operator; } } - protected static void Do(Player p, string[] args, int offset, bool max, AccessController access) { + public static void Do(Player p, string[] args, int offset, bool max, AccessController access) { for (int i = offset; i < args.Length; i++) { string arg = args[i]; if (arg[0] == '+' || arg[0] == '-') { diff --git a/MCGalaxy/Levels/IO/Importers/LvlImporter.cs b/MCGalaxy/Levels/IO/Importers/LvlImporter.cs index bd5b843de..afabcab14 100644 --- a/MCGalaxy/Levels/IO/Importers/LvlImporter.cs +++ b/MCGalaxy/Levels/IO/Importers/LvlImporter.cs @@ -142,7 +142,7 @@ namespace MCGalaxy.Levels.IO { if (count == 0) return; for (int i = 0; i < count; i++) { - Zone z = Zone.Create(); + Zone z = new Zone(lvl); if (!TryRead_U16(buffer, gs, ref z.MinX) || !TryRead_U16(buffer, gs, ref z.MaxX)) return; if (!TryRead_U16(buffer, gs, ref z.MinY) || !TryRead_U16(buffer, gs, ref z.MaxY)) return; if (!TryRead_U16(buffer, gs, ref z.MinZ) || !TryRead_U16(buffer, gs, ref z.MaxZ)) return; diff --git a/MCGalaxy/Levels/Level.Blocks.cs b/MCGalaxy/Levels/Level.Blocks.cs index 839468a26..2b90e22f3 100644 --- a/MCGalaxy/Levels/Level.Blocks.cs +++ b/MCGalaxy/Levels/Level.Blocks.cs @@ -205,7 +205,7 @@ namespace MCGalaxy { for (int i = 0; i < Zones.Count; i++) { Zone zn = Zones[i]; if (x < zn.MinX || x > zn.MaxX || y < zn.MinY || y > zn.MaxY || z < zn.MinZ || z > zn.MaxZ) continue; - AccessResult access = zn.Acess.Check(p); + AccessResult access = zn.Access.Check(p); if (access == AccessResult.Allowed || access == AccessResult.Whitelisted) return true; } @@ -213,11 +213,11 @@ namespace MCGalaxy { for (int i = 0; i < Zones.Count; i++) { Zone zn = Zones[i]; if (x < zn.MinX || x > zn.MaxX || y < zn.MinY || y > zn.MaxY || z < zn.MinZ || z > zn.MaxZ) continue; - AccessResult access = zn.Acess.Check(p); + AccessResult access = zn.Access.Check(p); if (access == AccessResult.Allowed || access == AccessResult.Whitelisted) continue; if (p.ZoneSpam > DateTime.UtcNow) return false; - zn.Acess.CheckDetailed(p); + zn.Access.CheckDetailed(p); p.ZoneSpam = DateTime.UtcNow.AddSeconds(2); return false; } diff --git a/MCGalaxy/Levels/LevelDB.cs b/MCGalaxy/Levels/LevelDB.cs index 7106bb729..e1bd45a7e 100644 --- a/MCGalaxy/Levels/LevelDB.cs +++ b/MCGalaxy/Levels/LevelDB.cs @@ -41,10 +41,11 @@ namespace MCGalaxy { internal static void LoadZones(Level level, string name) { if (!Database.TableExists("Zone" + name)) return; int id = 0; - object ; // add to map perbuild. + bool changedPerbuild = false; + using (DataTable table = Database.Backend.GetRows("Zone" + name, "*")) { foreach (DataRow row in table.Rows) { - Zone z = Zone.Create(); + Zone z = new Zone(level); z.MinX = ushort.Parse(row["SmallX"].ToString()); z.MinY = ushort.Parse(row["SmallY"].ToString()); z.MinZ = ushort.Parse(row["SmallZ"].ToString()); @@ -55,10 +56,14 @@ namespace MCGalaxy { string owner = row["Owner"].ToString(); if (owner.StartsWith("grp")) { Group grp = Group.Find(owner.Substring(3)); - if (grp != null) z.Config.BuildMin = grp.Permission; + if (grp != null) z.Access.Min = grp.Permission; + } else if (z.CoversMap(level)) { + level.BuildAccess.Whitelisted.Add(owner); + changedPerbuild = true; + continue; } else { - z.Config.BuildWhitelist.Add(owner); - z.Config.BuildMin = LevelPermission.Admin; + z.Access.Whitelisted.Add(owner); + z.Access.Min = LevelPermission.Admin; } z.Config.Name = "Zone" + id; @@ -67,6 +72,7 @@ namespace MCGalaxy { } } + if (changedPerbuild) Level.SaveSettings(level); if (level.Zones.Count > 0 && !level.Save(true)) return; Database.Backend.DeleteTable("Zone" + name); Logger.Log(LogType.SystemActivity, "Upgraded zones for map " + name); diff --git a/MCGalaxy/Levels/Zone.cs b/MCGalaxy/Levels/Zone.cs index f8b15d0fc..ab8ecfeee 100644 --- a/MCGalaxy/Levels/Zone.cs +++ b/MCGalaxy/Levels/Zone.cs @@ -82,23 +82,26 @@ namespace MCGalaxy { void Update() { lvl.Save(true); } } - public struct Zone { + public class Zone { public ushort MinX, MinY, MinZ; public ushort MaxX, MaxY, MaxZ; public ZoneConfig Config; - public ZoneAccessController Acess; + public ZoneAccessController Access; public string ColoredName { get { return Config.Color + Config.Name; } } public bool Contains(int x, int y, int z) { return x >= MinX && x <= MaxX && y >= MinY && y <= MaxY && z >= MinZ && z <= MaxZ; } - public static Zone Create() { - Zone zone = new Zone(); - zone.Config = new ZoneConfig(); - zone.Acess = new ZoneAccessController(); - return zone; + public bool CoversMap(Level lvl) { + return MinX == 0 && MinY == 0 && MinZ == 0 && + MaxX == lvl.Width - 1 && MaxY == lvl.Height - 1 && MaxZ == lvl.Length - 1; + } + + public Zone(Level lvl) { + Config = new ZoneConfig(); + Access = new ZoneAccessController(lvl, Config); } } } \ No newline at end of file