diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e59d2ae..ecbd7138 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Fixed /time display at the end of Terraria hours (@koneko-nyan) * Added a warning notifying users of the minimum memory required to run TShock (@bartico6) * Added /group rename to allow changing group names (@ColinBohn, @ProfessorXZ) +* Added /region rename and OnRegionRenamed hook (@koneko-nyan, @deadsurgeon42) + ## TShock 4.3.24 * Updated OpenTerraria API to 1.3.5.3 (@DeathCradle) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index c171c1e0..493aa956 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -4554,6 +4554,51 @@ namespace TShockAPI args.Player.SendErrorMessage("Invalid syntax! Proper syntax: {0}region resize ", Specifier); break; } + case "rename": + { + if (args.Parameters.Count != 3) + { + args.Player.SendErrorMessage("Invalid syntax! Proper syntax: {0}region rename ", Specifier); + break; + } + else + { + string oldName = args.Parameters[1]; + string newName = args.Parameters[2]; + + if (oldName == newName) + { + args.Player.SendErrorMessage("Error: both names are the same."); + break; + } + + Region oldRegion = TShock.Regions.GetRegionByName(oldName); + + if (oldRegion == null) + { + args.Player.SendErrorMessage("Invalid region \"{0}\".", oldName); + break; + } + + Region newRegion = TShock.Regions.GetRegionByName(newName); + + if (newRegion != null) + { + args.Player.SendErrorMessage("Region \"{0}\" already exists.", newName); + break; + } + + if(TShock.Regions.RenameRegion(oldName, newName)) + { + args.Player.SendInfoMessage("Region renamed successfully!"); + } + else + { + args.Player.SendErrorMessage("Failed to rename the region."); + } + } + break; + } case "tp": { if (!args.Player.HasPermission(Permissions.tp)) @@ -4594,6 +4639,7 @@ namespace TShockAPI "define - Defines the region with the given name.", "delete - Deletes the given region.", "name [-u][-z][-p] - Shows the name of the region at the given point.", + "rename - Renames the given region.", "list - Lists all regions.", "resize - Resizes a region.", "allow - Allows a user to a region.", diff --git a/TShockAPI/DB/RegionManager.cs b/TShockAPI/DB/RegionManager.cs index 661ec1c5..f94b2a48 100644 --- a/TShockAPI/DB/RegionManager.cs +++ b/TShockAPI/DB/RegionManager.cs @@ -401,7 +401,41 @@ namespace TShockAPI.DB } return false; } + + /// + /// Renames a region + /// + /// Name of the region to rename + /// New name of the region + /// true if renamed successfully, false otherwise + public bool RenameRegion(string oldName, string newName) + { + Region region = null; + string worldID = Main.worldID.ToString(); + bool result = false; + + try + { + int q = database.Query("UPDATE Regions SET RegionName = @0 WHERE RegionName = @1 AND WorldID = @2", + newName, oldName, worldID); + + if (q > 0) + { + region = Regions.First(r => r.Name == oldName && r.WorldID == worldID); + region.Name = newName; + Hooks.RegionHooks.OnRegionRenamed(region, oldName, newName); + result = true; + } + } + catch (Exception ex) + { + TShock.Log.Error(ex.ToString()); + } + + return result; + } + /// /// Removes an allowed user from a region /// diff --git a/TShockAPI/Hooks/RegionHooks.cs b/TShockAPI/Hooks/RegionHooks.cs index 9f1b1e78..dcf8d6ce 100644 --- a/TShockAPI/Hooks/RegionHooks.cs +++ b/TShockAPI/Hooks/RegionHooks.cs @@ -110,5 +110,29 @@ namespace TShockAPI.Hooks RegionDeleted(new RegionDeletedEventArgs(region)); } + + public class RegionRenamedEventArgs + { + public Region Region { get; private set; } + public string OldName { get; private set; } + public string NewName { get; private set; } + + public RegionRenamedEventArgs(Region region, string oldName, string newName) + { + Region = region; + OldName = oldName; + NewName = newName; + } + } + + public delegate void RegionRenamedD(RegionRenamedEventArgs args); + public static event RegionRenamedD RegionRenamed; + public static void OnRegionRenamed(Region region, string oldName, string newName) + { + if (RegionRenamed == null) + return; + + RegionRenamed(new RegionRenamedEventArgs(region, oldName, newName)); + } } }