From b56a77b39d97dec159412573e2f3616b7d01636a Mon Sep 17 00:00:00 2001 From: Zack Piispanen Date: Wed, 30 May 2012 20:12:27 -0400 Subject: [PATCH] Removed the Region code. Left todo's for the places where we currently reference it though. --- TShockAPI/Commands.cs | 437 +--------------------- TShockAPI/DB/RegionManager.cs | 661 ---------------------------------- TShockAPI/GetDataHandlers.cs | 5 +- TShockAPI/TShock.cs | 22 +- TShockAPI/TShockAPI.csproj | 5 +- 5 files changed, 17 insertions(+), 1113 deletions(-) delete mode 100644 TShockAPI/DB/RegionManager.cs diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index fa288573..6a911874 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -164,7 +164,6 @@ namespace TShockAPI add(Permissions.pvpfun, Slap, "slap"); add(Permissions.editspawn, ToggleAntiBuild, "antibuild"); add(Permissions.editspawn, ProtectSpawn, "protectspawn"); - add(Permissions.manageregion, Region, "region"); add(Permissions.maintenance, GetVersion, "version"); add(null, ListConnectedPlayers, "playing", "online", "who"); add(null, AuthToken, "auth"); @@ -1917,7 +1916,8 @@ namespace TShockAPI FileTools.SetupConfig(); TShock.HandleCommandLinePostConfigLoad(Environment.GetCommandLineArgs()); TShock.Groups.LoadPermisions(); - TShock.Regions.ReloadAllRegions(); + //todo: Create an event for reloads to propegate to plugins. + //TShock.Regions.ReloadAllRegions(); args.Player.SendSuccessMessage( "Configuration, Permissions, and Regions reload complete. Some changes may require server restart."); } @@ -2116,439 +2116,6 @@ namespace TShockAPI TSPlayer.All.SendSuccessMessage(string.Format("Spawn is now {0}.", (TShock.Config.SpawnProtection ? "protected" : "open"))); } - private static void Region(CommandArgs args) - { - string cmd = "help"; - if (args.Parameters.Count > 0) - { - cmd = args.Parameters[0].ToLower(); - } - switch (cmd) - { - case "name": - { - { - args.Player.SendInfoMessage("Hit a block to get the name of the region"); - args.Player.AwaitingName = true; - } - break; - } - case "set": - { - int choice = 0; - if (args.Parameters.Count == 2 && - int.TryParse(args.Parameters[1], out choice) && - choice >= 1 && choice <= 2) - { - args.Player.SendInfoMessage("Hit a block to Set Point " + choice); - args.Player.AwaitingTempPoint = choice; - } - else - { - args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /region set [1/2]"); - } - break; - } - case "define": - { - if (args.Parameters.Count > 1) - { - if (!args.Player.TempPoints.Any(p => p == Point.Zero)) - { - string regionName = String.Join(" ", args.Parameters.GetRange(1, args.Parameters.Count - 1)); - var x = Math.Min(args.Player.TempPoints[0].X, args.Player.TempPoints[1].X); - var y = Math.Min(args.Player.TempPoints[0].Y, args.Player.TempPoints[1].Y); - var width = Math.Abs(args.Player.TempPoints[0].X - args.Player.TempPoints[1].X); - var height = Math.Abs(args.Player.TempPoints[0].Y - args.Player.TempPoints[1].Y); - - if (TShock.Regions.AddRegion(x, y, width, height, regionName, args.Player.UserAccountName, - Main.worldID.ToString())) - { - args.Player.TempPoints[0] = Point.Zero; - args.Player.TempPoints[1] = Point.Zero; - args.Player.SendSuccessMessage("Set region " + regionName); - } - else - { - args.Player.SendWarningMessage("Region " + regionName + " already exists"); - } - } - else - { - args.Player.SendErrorMessage("Points not set up yet."); - } - } - else - args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /region define [name]"); - break; - } - case "protect": - { - if (args.Parameters.Count == 3) - { - string regionName = args.Parameters[1]; - if (args.Parameters[2].ToLower() == "true") - { - if (TShock.Regions.SetRegionState(regionName, true)) - args.Player.SendSuccessMessage("Protected region " + regionName); - else - args.Player.SendErrorMessage("Could not find specified region"); - } - else if (args.Parameters[2].ToLower() == "false") - { - if (TShock.Regions.SetRegionState(regionName, false)) - args.Player.SendSuccessMessage("Unprotected region " + regionName); - else - args.Player.SendErrorMessage("Could not find specified region"); - } - else - args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /region protect [name] [true/false]"); - } - else - args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /region protect [name] [true/false]"); - break; - } - case "delete": - { - if (args.Parameters.Count > 1) - { - string regionName = String.Join(" ", args.Parameters.GetRange(1, args.Parameters.Count - 1)); - if (TShock.Regions.DeleteRegion(regionName)) - args.Player.SendSuccessMessage("Deleted region " + regionName); - else - args.Player.SendErrorMessage("Could not find specified region"); - } - else - args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /region delete [name]"); - break; - } - case "clear": - { - args.Player.TempPoints[0] = Point.Zero; - args.Player.TempPoints[1] = Point.Zero; - args.Player.SendInfoMessage("Cleared temp area"); - args.Player.AwaitingTempPoint = 0; - break; - } - case "allow": - { - if (args.Parameters.Count > 2) - { - string playerName = args.Parameters[1]; - string regionName = ""; - - for (int i = 2; i < args.Parameters.Count; i++) - { - if (regionName == "") - { - regionName = args.Parameters[2]; - } - else - { - regionName = regionName + " " + args.Parameters[i]; - } - } - if (TShock.Users.GetUserByName(playerName) != null) - { - if (TShock.Regions.AddNewUser(regionName, playerName)) - { - args.Player.SendSuccessMessage("Added user " + playerName + " to " + regionName); - } - else - args.Player.SendErrorMessage("Region " + regionName + " not found"); - } - else - { - args.Player.SendErrorMessage("Player " + playerName + " not found"); - } - } - else - args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /region allow [name] [region]"); - break; - } - case "remove": - if (args.Parameters.Count > 2) - { - string playerName = args.Parameters[1]; - string regionName = ""; - - for (int i = 2; i < args.Parameters.Count; i++) - { - if (regionName == "") - { - regionName = args.Parameters[2]; - } - else - { - regionName = regionName + " " + args.Parameters[i]; - } - } - if (TShock.Users.GetUserByName(playerName) != null) - { - if (TShock.Regions.RemoveUser(regionName, playerName)) - { - args.Player.SendSuccessMessage("Removed user " + playerName + " from " + regionName); - } - else - args.Player.SendErrorMessage("Region " + regionName + " not found"); - } - else - { - args.Player.SendErrorMessage("Player " + playerName + " not found"); - } - } - else - args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /region remove [name] [region]"); - break; - case "allowg": - { - if (args.Parameters.Count > 2) - { - string group = args.Parameters[1]; - string regionName = ""; - - for (int i = 2; i < args.Parameters.Count; i++) - { - if (regionName == "") - { - regionName = args.Parameters[2]; - } - else - { - regionName = regionName + " " + args.Parameters[i]; - } - } - if (TShock.Groups.GroupExists(group)) - { - if (TShock.Regions.AllowGroup(regionName, group)) - { - args.Player.SendSuccessMessage("Added group " + group + " to " + regionName); - } - else - args.Player.SendErrorMessage("Region " + regionName + " not found"); - } - else - { - args.Player.SendErrorMessage("Group " + group + " not found"); - } - } - else - args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /region allow [group] [region]"); - break; - } - case "removeg": - if (args.Parameters.Count > 2) - { - string group = args.Parameters[1]; - string regionName = ""; - - for (int i = 2; i < args.Parameters.Count; i++) - { - if (regionName == "") - { - regionName = args.Parameters[2]; - } - else - { - regionName = regionName + " " + args.Parameters[i]; - } - } - if (TShock.Groups.GroupExists(group)) - { - if (TShock.Regions.RemoveGroup(regionName, group)) - { - args.Player.SendSuccessMessage("Removed group " + group + " from " + regionName); - } - else - args.Player.SendErrorMessage("Region " + regionName + " not found"); - } - else - { - args.Player.SendErrorMessage("Group " + group + " not found"); - } - } - else - args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /region removeg [group] [region])"); - break; - case "list": - { - //How many regions per page - const int pagelimit = 15; - //How many regions per line - const int perline = 5; - //Pages start at 0 but are displayed and parsed at 1 - int page = 0; - - - if (args.Parameters.Count > 1) - { - if (!int.TryParse(args.Parameters[1], out page) || page < 1) - { - args.Player.SendErrorMessage(string.Format("Invalid page number ({0})", page)); - return; - } - page--; //Substract 1 as pages are parsed starting at 1 and not 0 - } - - var regions = TShock.Regions.ListAllRegions(Main.worldID.ToString()); - - // Are there even any regions to display? - if (regions.Count == 0) - { - args.Player.SendWarningMessage("There are currently no regions defined."); - return; - } - - //Check if they are trying to access a page that doesn't exist. - int pagecount = regions.Count/pagelimit; - if (page > pagecount) - { - args.Player.SendWarningMessage(string.Format("Page number exceeds pages ({0}/{1})", page + 1, pagecount + 1)); - return; - } - - //Display the current page and the number of pages. - args.Player.SendSuccessMessage(string.Format("Current Regions ({0}/{1}):", page + 1, pagecount + 1)); - - //Add up to pagelimit names to a list - var nameslist = new List(); - for (int i = (page*pagelimit); (i < ((page*pagelimit) + pagelimit)) && i < regions.Count; i++) - { - nameslist.Add(regions[i].Name); - } - - //convert the list to an array for joining - var names = nameslist.ToArray(); - for (int i = 0; i < names.Length; i += perline) - { - args.Player.SendInfoMessage(string.Join(", ", names, i, Math.Min(names.Length - i, perline))); - } - - if (page < pagecount) - { - args.Player.SendInfoMessage(string.Format("Type /region list {0} for more regions.", (page + 2))); - } - - break; - } - case "info": - { - if (args.Parameters.Count > 1) - { - string regionName = String.Join(" ", args.Parameters.GetRange(1, args.Parameters.Count - 1)); - Region r = TShock.Regions.GetRegionByName(regionName); - - if (r == null) - { - args.Player.SendErrorMessage("Region {0} does not exist"); - break; - } - - args.Player.SendSuccessMessage(r.Name + ": P: " + r.DisableBuild + " X: " + r.Area.X + " Y: " + r.Area.Y + " W: " + - r.Area.Width + " H: " + r.Area.Height); - foreach (int s in r.AllowedIDs) - { - var user = TShock.Users.GetUserByID(s); - args.Player.SendSuccessMessage(r.Name + ": " + (user != null ? user.Name : "Unknown")); - } - } - else - { - args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /region info [name]"); - } - - break; - } - case "z": - { - if (args.Parameters.Count == 3) - { - string regionName = args.Parameters[1]; - int z = 0; - if (int.TryParse(args.Parameters[2], out z ) ) - { - if (TShock.Regions.SetZ(regionName, z)) - args.Player.SendInfoMessage("Region's z is now " + z); - else - args.Player.SendErrorMessage("Could not find specified region"); - } - else - args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /region z [name] [#]"); - } - else - args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /region z [name] [#]"); - break; - } - case "resize": - case "expand": - { - if (args.Parameters.Count == 4) - { - int direction; - switch (args.Parameters[2]) - { - case "u": - case "up": - { - direction = 0; - break; - } - case "r": - case "right": - { - direction = 1; - break; - } - case "d": - case "down": - { - direction = 2; - break; - } - case "l": - case "left": - { - direction = 3; - break; - } - default: - { - direction = -1; - break; - } - } - int addAmount; - int.TryParse(args.Parameters[3], out addAmount); - if (TShock.Regions.resizeRegion(args.Parameters[1], addAmount, direction)) - { - args.Player.SendSuccessMessage("Region Resized Successfully!"); - TShock.Regions.ReloadAllRegions(); - } - else - { - args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /region resize [regionname] [u/d/l/r] [amount]"); - } - } - else - { - args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /region resize [regionname] [u/d/l/r] [amount]1"); - } - break; - } - case "help": - default: - { - args.Player.SendInfoMessage("Avialable region commands:"); - args.Player.SendInfoMessage("/region set [1/2] /region define [name] /region protect [name] [true/false]"); - args.Player.SendInfoMessage("/region name (provides region name)"); - args.Player.SendInfoMessage("/region delete [name] /region clear (temporary region)"); - args.Player.SendInfoMessage("/region allow [name] [regionname]"); - args.Player.SendInfoMessage("/region resize [regionname] [u/d/l/r] [amount]"); - break; - } - } - } - #endregion World Protection Commands #region General Commands diff --git a/TShockAPI/DB/RegionManager.cs b/TShockAPI/DB/RegionManager.cs deleted file mode 100644 index b3eef5fe..00000000 --- a/TShockAPI/DB/RegionManager.cs +++ /dev/null @@ -1,661 +0,0 @@ -/* -TShock, a server mod for Terraria -Copyright (C) 2011-2012 The TShock Team - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ -using System; -using System.Collections.Generic; -using System.Data; -using System.Diagnostics.CodeAnalysis; -using System.IO; -using System.Linq; -using System.Xml; -using MySql.Data.MySqlClient; -using Terraria; - -namespace TShockAPI.DB -{ - public class RegionManager - { - public List Regions = new List(); - - private IDbConnection database; - - public RegionManager(IDbConnection db) - { - database = db; - var table = new SqlTable("Regions", - new SqlColumn("X1", MySqlDbType.Int32), - new SqlColumn("Y1", MySqlDbType.Int32), - new SqlColumn("width", MySqlDbType.Int32), - new SqlColumn("height", MySqlDbType.Int32), - new SqlColumn("RegionName", MySqlDbType.VarChar, 50) {Primary = true}, - new SqlColumn("WorldID", MySqlDbType.Text), - new SqlColumn("UserIds", MySqlDbType.Text), - new SqlColumn("Protected", MySqlDbType.Int32), - new SqlColumn("Groups", MySqlDbType.Text), - new SqlColumn("Owner", MySqlDbType.VarChar, 50), - new SqlColumn("Z", MySqlDbType.Int32){ DefaultValue = "0" } - ); - var creator = new SqlTableCreator(db, - db.GetSqlType() == SqlType.Sqlite - ? (IQueryBuilder) new SqliteQueryCreator() - : new MysqlQueryCreator()); - creator.EnsureExists(table); - - ReloadAllRegions(); - } - - public void ReloadAllRegions() - { - try - { - using (var reader = database.QueryReader("SELECT * FROM Regions WHERE WorldID=@0", Main.worldID.ToString())) - { - Regions.Clear(); - while (reader.Read()) - { - int X1 = reader.Get("X1"); - int Y1 = reader.Get("Y1"); - int height = reader.Get("height"); - int width = reader.Get("width"); - int Protected = reader.Get("Protected"); - string mergedids = reader.Get("UserIds"); - string name = reader.Get("RegionName"); - string owner = reader.Get("Owner"); - string groups = reader.Get("Groups"); - int z = reader.Get("Z"); - - string[] splitids = mergedids.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries); - - Region r = new Region(new Rectangle(X1, Y1, width, height), name, owner, Protected != 0, Main.worldID.ToString(), z); - r.SetAllowedGroups(groups); - try - { - for (int i = 0; i < splitids.Length; i++) - { - int id; - - if (Int32.TryParse(splitids[i], out id)) // if unparsable, it's not an int, so silently skip - r.AllowedIDs.Add(id); - else - Log.Warn("One of your UserIDs is not a usable integer: " + splitids[i]); - } - } - catch (Exception e) - { - Log.Error("Your database contains invalid UserIDs (they should be ints)."); - Log.Error("A lot of things will fail because of this. You must manually delete and re-create the allowed field."); - Log.Error(e.ToString()); - Log.Error(e.StackTrace); - } - - Regions.Add(r); - } - } - } - catch (Exception ex) - { - Log.Error(ex.ToString()); - } - } - - public void ReloadForUnitTest(String n) - { - using (var reader = database.QueryReader("SELECT * FROM Regions WHERE WorldID=@0", n)) - { - Regions.Clear(); - while (reader.Read()) - { - int X1 = reader.Get("X1"); - int Y1 = reader.Get("Y1"); - int height = reader.Get("height"); - int width = reader.Get("width"); - int Protected = reader.Get("Protected"); - string MergedIDs = reader.Get("UserIds"); - string name = reader.Get("RegionName"); - string[] SplitIDs = MergedIDs.Split(','); - string owner = reader.Get("Owner"); - string groups = reader.Get("Groups"); - int z = reader.Get("Z"); - - Region r = new Region(new Rectangle(X1, Y1, width, height), name, owner, Protected != 0, Main.worldID.ToString(), z); - r.SetAllowedGroups(groups); - try - { - for (int i = 0; i < SplitIDs.Length; i++) - { - int id; - - if (Int32.TryParse(SplitIDs[i], out id)) // if unparsable, it's not an int, so silently skip - r.AllowedIDs.Add(id); - else if (SplitIDs[i] == "") // Split gotcha, can return an empty string with certain conditions - // but we only want to let the user know if it's really a nonparsable integer. - Log.Warn("UnitTest: One of your UserIDs is not a usable integer: " + SplitIDs[i]); - } - } - catch (Exception e) - { - Log.Error("Your database contains invalid UserIDs (they should be ints)."); - Log.Error("A lot of things will fail because of this. You must manually delete and re-create the allowed field."); - Log.Error(e.Message); - Log.Error(e.StackTrace); - } - - Regions.Add(r); - } - } - } - - public bool AddRegion(int tx, int ty, int width, int height, string regionname, string owner, string worldid, int z = 0) - { - if (GetRegionByName(regionname) != null) - { - return false; - } - try - { - database.Query( - "INSERT INTO Regions (X1, Y1, width, height, RegionName, WorldID, UserIds, Protected, Groups, Owner, Z) VALUES (@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10);", - tx, ty, width, height, regionname, worldid, "", 1, "", owner, z); - Regions.Add(new Region(new Rectangle(tx, ty, width, height), regionname, owner, true, worldid, z)); - return true; - } - catch (Exception ex) - { - Log.Error(ex.ToString()); - } - return false; - } - - public bool DeleteRegion(string name) - { - try - { - database.Query("DELETE FROM Regions WHERE RegionName=@0 AND WorldID=@1", name, Main.worldID.ToString()); - var worldid = Main.worldID.ToString(); - Regions.RemoveAll(r => r.Name == name && r.WorldID == worldid); - return true; - } - catch (Exception ex) - { - Log.Error(ex.ToString()); - } - return false; - } - - public bool SetRegionState(string name, bool state) - { - try - { - database.Query("UPDATE Regions SET Protected=@0 WHERE RegionName=@1 AND WorldID=@2", state ? 1 : 0, name, - Main.worldID.ToString()); - var region = GetRegionByName(name); - if (region != null) - region.DisableBuild = state; - return true; - } - catch (Exception ex) - { - Log.Error(ex.ToString()); - return false; - } - } - - public bool SetRegionStateTest(string name, string world, bool state) - { - try - { - database.Query("UPDATE Regions SET Protected=@0 WHERE RegionName=@1 AND WorldID=@2", state ? 1 : 0, name, world); - var region = GetRegionByName(name); - if (region != null) - region.DisableBuild = state; - return true; - } - catch (Exception ex) - { - Log.Error(ex.ToString()); - return false; - } - } - - public bool CanBuild(int x, int y, TSPlayer ply) - { - if (!ply.Group.HasPermission(Permissions.canbuild)) - { - return false; - } - Region top = null; - for (int i = 0; i < Regions.Count; i++) - { - if (Regions[i].InArea(x,y) ) - { - if (top == null) - top = Regions[i]; - else - { - if (Regions[i].Z > top.Z) - top = Regions[i]; - } - } - } - return top == null || top.HasPermissionToBuildInRegion(ply); - } - - public bool InArea(int x, int y) - { - foreach (Region region in Regions) - { - if (x >= region.Area.Left && x <= region.Area.Right && - y >= region.Area.Top && y <= region.Area.Bottom && - region.DisableBuild) - { - return true; - } - } - return false; - } - - public List InAreaRegionName(int x, int y) - { - List regions = new List() { }; - foreach (Region region in Regions) - { - if (x >= region.Area.Left && x <= region.Area.Right && - y >= region.Area.Top && y <= region.Area.Bottom && - region.DisableBuild) - { - regions.Add(region.Name); - } - } - return regions; - } - - public static List ListIDs(string MergedIDs) - { - return MergedIDs.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries).ToList(); - } - - public bool resizeRegion(string regionName, int addAmount, int direction) - { - //0 = up - //1 = right - //2 = down - //3 = left - int X = 0; - int Y = 0; - int height = 0; - int width = 0; - try - { - using ( - var reader = database.QueryReader("SELECT X1, Y1, height, width FROM Regions WHERE RegionName=@0 AND WorldID=@1", - regionName, Main.worldID.ToString())) - { - if (reader.Read()) - X = reader.Get("X1"); - width = reader.Get("width"); - Y = reader.Get("Y1"); - height = reader.Get("height"); - } - if (!(direction == 0)) - { - if (!(direction == 1)) - { - if (!(direction == 2)) - { - if (!(direction == 3)) - { - return false; - } - else - { - X -= addAmount; - width += addAmount; - } - } - else - { - height += addAmount; - } - } - else - { - width += addAmount; - } - } - else - { - Y -= addAmount; - height += addAmount; - } - int q = - database.Query( - "UPDATE Regions SET X1 = @0, Y1 = @1, width = @2, height = @3 WHERE RegionName = @4 AND WorldID=@5", X, Y, width, - height, regionName, Main.worldID.ToString()); - if (q > 0) - return true; - } - catch (Exception ex) - { - Log.Error(ex.ToString()); - } - return false; - } - - public bool RemoveUser(string regionName, string userName) - { - Region r = GetRegionByName(regionName); - if (r != null) - { - r.RemoveID(TShock.Users.GetUserID(userName)); - string ids = string.Join(",", r.AllowedIDs); - int q = database.Query("UPDATE Regions SET UserIds=@0 WHERE RegionName=@1 AND WorldID=@2", ids, - regionName, Main.worldID.ToString()); - if (q > 0) - return true; - } - return false; - } - - public bool AddNewUser(string regionName, String userName) - { - try - { - string MergedIDs = string.Empty; - using ( - var reader = database.QueryReader("SELECT * FROM Regions WHERE RegionName=@0 AND WorldID=@1", regionName, - Main.worldID.ToString())) - { - if (reader.Read()) - MergedIDs = reader.Get("UserIds"); - } - - if (string.IsNullOrEmpty(MergedIDs)) - MergedIDs = Convert.ToString(TShock.Users.GetUserID(userName)); - else - MergedIDs = MergedIDs + "," + Convert.ToString(TShock.Users.GetUserID(userName)); - - int q = database.Query("UPDATE Regions SET UserIds=@0 WHERE RegionName=@1 AND WorldID=@2", MergedIDs, - regionName, Main.worldID.ToString()); - foreach (var r in Regions) - { - if (r.Name == regionName && r.WorldID == Main.worldID.ToString()) - r.setAllowedIDs(MergedIDs); - } - return q != 0; - } - catch (Exception ex) - { - Log.Error(ex.ToString()); - } - return false; - } - - /// - /// Gets all the regions names from world - /// - /// World name to get regions from - /// List of regions with only their names - public List ListAllRegions(string worldid) - { - var regions = new List(); - try - { - using (var reader = database.QueryReader("SELECT RegionName FROM Regions WHERE WorldID=@0", worldid)) - { - while (reader.Read()) - regions.Add(new Region {Name = reader.Get("RegionName")}); - } - } - catch (Exception ex) - { - Log.Error(ex.ToString()); - } - return regions; - } - - public Region GetRegionByName(String name) - { - return Regions.FirstOrDefault(r => r.Name.Equals(name) && r.WorldID == Main.worldID.ToString()); - } - - public Region ZacksGetRegionByName(String name) - { - foreach (Region r in Regions) - { - if (r.Name.Equals(name)) - return r; - } - return null; - } - - public bool ChangeOwner(string regionName, string newOwner) - { - var region = GetRegionByName(regionName); - if (region != null) - { - region.Owner = newOwner; - int q = database.Query("UPDATE Regions SET Owner=@0 WHERE RegionName=@1 AND WorldID=@2", newOwner, - regionName, Main.worldID.ToString()); - if (q > 0) - return true; - } - return false; - } - - public bool AllowGroup(string regionName, string groups) - { - string groupsNew = ""; - using ( - var reader = database.QueryReader("SELECT * FROM Regions WHERE RegionName=@0 AND WorldID=@1", regionName, - Main.worldID.ToString())) - { - if (reader.Read()) - groupsNew = reader.Get("Groups"); - } - if (groupsNew != "") - groupsNew += ","; - groupsNew += groups; - - int q = database.Query("UPDATE Regions SET Groups=@0 WHERE RegionName=@1 AND WorldID=@2", groupsNew, - regionName, Main.worldID.ToString()); - - Region r = GetRegionByName(regionName); - if (r != null) - { - r.SetAllowedGroups(groupsNew); - } - else - { - return false; - } - - return q > 0; - } - - public bool RemoveGroup(string regionName, string group) - { - Region r = GetRegionByName(regionName); - if (r != null) - { - r.RemoveGroup(group); - string groups = string.Join(",", r.AllowedGroups); - int q = database.Query("UPDATE Regions SET Groups=@0 WHERE RegionName=@1 AND WorldID=@2", groups, - regionName, Main.worldID.ToString()); - if (q > 0) - return true; - } - return false; - } - - public Region GetTopRegion( List regions ) - { - Region ret = null; - foreach( Region r in regions) - { - if (ret == null) - ret = r; - else - { - if (r.Z > ret.Z) - ret = r; - } - } - return ret; - } - - public bool SetZ( string name, int z ) - { - try - { - database.Query("UPDATE Regions SET Z=@0 WHERE RegionName=@1 AND WorldID=@2", z, name, - Main.worldID.ToString()); - - var region = GetRegionByName(name); - if (region != null) - region.Z = z; - return true; - } - catch (Exception ex) - { - Log.Error(ex.ToString()); - return false; - } - } - } - - public class Region - { - public Rectangle Area { get; set; } - public string Name { get; set; } - public string Owner { get; set; } - public bool DisableBuild { get; set; } - public string WorldID { get; set; } - public List AllowedIDs { get; set; } - public List AllowedGroups { get; set; } - public int Z { get; set; } - - public Region(Rectangle region, string name, string owner, bool disablebuild, string RegionWorldIDz, int z) - : this() - { - Area = region; - Name = name; - Owner = owner; - DisableBuild = disablebuild; - WorldID = RegionWorldIDz; - Z = z; - } - - public Region() - { - Area = Rectangle.Empty; - Name = string.Empty; - DisableBuild = true; - WorldID = string.Empty; - AllowedIDs = new List(); - AllowedGroups = new List(); - Z = 0; - } - - public bool InArea(Rectangle point) - { - if (Area.Contains(point.X, point.Y)) - { - return true; - } - return false; - } - - public bool InArea(int x, int y) //overloaded with x,y - { - - if (x >= Area.Left && x <= Area.Right && y >= Area.Top && y <= Area.Bottom) - { - return true; - } - - return false; - } - - - public bool HasPermissionToBuildInRegion(TSPlayer ply) - { - if (!ply.IsLoggedIn) - { - if (!ply.HasBeenNaggedAboutLoggingIn) - { - ply.SendMessage("You must be logged in to take advantage of protected regions.", Color.Red); - ply.HasBeenNaggedAboutLoggingIn = true; - } - return false; - } - if (!DisableBuild) - { - return true; - } - - return AllowedIDs.Contains(ply.UserID) || AllowedGroups.Contains(ply.Group.Name) || Owner == ply.UserAccountName || - ply.Group.HasPermission("manageregion"); - } - - public void setAllowedIDs(String ids) - { - String[] id_arr = ids.Split(','); - List id_list = new List(); - foreach (String id in id_arr) - { - int i = 0; - int.TryParse(id, out i); - if (i != 0) - id_list.Add(i); - } - AllowedIDs = id_list; - } - - public void SetAllowedGroups(String groups) - { - // prevent null pointer exceptions - if (!string.IsNullOrEmpty(groups)) - { - List groupArr = groups.Split(',').ToList(); - - for (int i = 0; i < groupArr.Count; i++) - groupArr[i] = groupArr[i].Trim(); - - AllowedGroups = groupArr; - } - } - - public void RemoveID(int id) - { - var index = -1; - for (int i = 0; i < AllowedIDs.Count; i++) - { - if (AllowedIDs[i] == id) - { - index = i; - break; - } - } - AllowedIDs.RemoveAt(index); - } - - public bool RemoveGroup(string groupName) - { - return AllowedGroups.Remove(groupName); - } - } -} diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index 24541aa9..f99cc013 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -1648,7 +1648,8 @@ namespace TShockAPI if (args.Player.AwaitingName) { - var protectedregions = TShock.Regions.InAreaRegionName(tileX, tileY); + //todo: We need to replace this call to the region manage plugins call + /*var protectedregions = TShock.Regions.InAreaRegionName(tileX, tileY); if (protectedregions.Count == 0) { args.Player.SendMessage("Region is not protected", Color.Yellow); @@ -1657,7 +1658,7 @@ namespace TShockAPI { string regionlist = string.Join(",", protectedregions.ToArray()); args.Player.SendMessage("Region Name(s): " + regionlist, Color.Yellow); - } + }*/ args.Player.SendTileSquare(tileX, tileY); args.Player.AwaitingName = false; return true; diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 59f5f49d..17b42c3e 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -51,7 +51,6 @@ namespace TShockAPI public static TSPlayer[] Players = new TSPlayer[Main.maxPlayers]; public static BanManager Bans; public static WarpManager Warps; - public static RegionManager Regions; public static BackupManager Backups; public static GroupManager Groups; public static UserManager Users; @@ -190,7 +189,6 @@ namespace TShockAPI Warps = new WarpManager(DB); Users = new UserManager(DB); Groups = new GroupManager(DB); - Regions = new RegionManager(DB); Itembans = new ItemManager(DB); RememberedPos = new RemeberedPosManager(DB); InventoryDB = new InventoryManager(DB); @@ -521,7 +519,6 @@ namespace TShockAPI { AuthToken = 0; } - Regions.ReloadAllRegions(); StatTracker.CheckIn(); FixChestStacks(); @@ -1321,16 +1318,17 @@ namespace TShockAPI return true; } - if (!player.Group.HasPermission(Permissions.editspawn) && !Regions.CanBuild(tileX, tileY, player) && + //todo: Replace with calls to the new region plugin. + /*if (!player.Group.HasPermission(Permissions.editspawn) && !Regions.CanBuild(tileX, tileY, player) && Regions.InArea(tileX, tileY)) { - if (((DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond) - player.RPm) > 2000){ - player.SendMessage("Region protected from changes.", Color.Red); - player.RPm=DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; + if (((DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond) - player.RPm) > 2000){ + player.SendMessage("Region protected from changes.", Color.Red); + player.RPm=DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; + } -} return true; - } + }*/ if (Config.DisableBuild) { if (!player.Group.HasPermission(Permissions.editspawn)) @@ -1372,8 +1370,8 @@ namespace TShockAPI } return true; } - - if (!player.Group.HasPermission(Permissions.editspawn) && !Regions.CanBuild(tileX, tileY, player) && + //todo: Replace with calls to the new region plugin. + /*if (!player.Group.HasPermission(Permissions.editspawn) && !Regions.CanBuild(tileX, tileY, player) && Regions.InArea(tileX, tileY)) { @@ -1383,7 +1381,7 @@ namespace TShockAPI player.RPm=DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; } return true; - } + }*/ if (Config.DisableBuild) { diff --git a/TShockAPI/TShockAPI.csproj b/TShockAPI/TShockAPI.csproj index de62800c..2a137ee9 100644 --- a/TShockAPI/TShockAPI.csproj +++ b/TShockAPI/TShockAPI.csproj @@ -114,7 +114,6 @@ - @@ -188,7 +187,7 @@ - + - + \ No newline at end of file