From d652fd6c83558e2b058f3788714fe53f6c960e7f Mon Sep 17 00:00:00 2001 From: koneko-nyan <31385587+koneko-nyan@users.noreply.github.com> Date: Mon, 2 Oct 2017 17:36:50 +0200 Subject: [PATCH 1/5] Add OnRegionRenamed hook --- TShockAPI/Hooks/RegionHooks.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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)); + } } } From f5f516d409cc17b74d6f1e358dae7e00e813c068 Mon Sep 17 00:00:00 2001 From: koneko-nyan <31385587+koneko-nyan@users.noreply.github.com> Date: Mon, 2 Oct 2017 17:39:07 +0200 Subject: [PATCH 2/5] Add a CHANGELOG.md entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e455dab..94698e0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Updated to new stat tracking system with more data so we can actually make informed software decisions (Jordan Coulam) * 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 /region rename and OnRegionRenamed hook (@koneko-nyan, @deadsurgeon42) ## TShock 4.3.24 * API: Changed `PlayerHooks` permission hook mechanisms to allow negation from hooks (@deadsurgeon42) From 30475be268d1be3ddeb938ae56386d12f7f011a8 Mon Sep 17 00:00:00 2001 From: koneko-nyan <31385587+koneko-nyan@users.noreply.github.com> Date: Mon, 2 Oct 2017 17:42:22 +0200 Subject: [PATCH 3/5] Add RegionRename --- TShockAPI/DB/RegionManager.cs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/TShockAPI/DB/RegionManager.cs b/TShockAPI/DB/RegionManager.cs index 661ec1c5..8649dedf 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 /// From aaa84d1ba9c010dae726b207a1dab6bfc0de518e Mon Sep 17 00:00:00 2001 From: koneko-nyan <31385587+koneko-nyan@users.noreply.github.com> Date: Mon, 2 Oct 2017 17:45:57 +0200 Subject: [PATCH 4/5] Add 'rename' sub-command to /region --- TShockAPI/Commands.cs | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index fbc6c061..615ba59f 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -4530,6 +4530,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)) @@ -4570,6 +4615,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.", From 9d12461f8d8f1ca3a57c8c5b5c0b150324767e28 Mon Sep 17 00:00:00 2001 From: koneko-nyan <31385587+koneko-nyan@users.noreply.github.com> Date: Fri, 13 Oct 2017 09:12:56 +0200 Subject: [PATCH 5/5] Fix some spaces --- TShockAPI/DB/RegionManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TShockAPI/DB/RegionManager.cs b/TShockAPI/DB/RegionManager.cs index 8649dedf..f94b2a48 100644 --- a/TShockAPI/DB/RegionManager.cs +++ b/TShockAPI/DB/RegionManager.cs @@ -417,7 +417,7 @@ namespace TShockAPI.DB try { - int q = database.Query("UPDATE Regions SET RegionName = @0 WHERE RegionName=@1 AND WorldID=@2", + int q = database.Query("UPDATE Regions SET RegionName = @0 WHERE RegionName = @1 AND WorldID = @2", newName, oldName, worldID); if (q > 0)