From 406abce30c0d522d6378d7f73a36df89345d05f7 Mon Sep 17 00:00:00 2001 From: Zack Piispanen Date: Sun, 1 Mar 2015 22:27:49 -0500 Subject: [PATCH] Add hooks for Region creation/deletion. Add hooks for Region Enter/Leave. Add member to TSPlayer displaying current region. Set private setters in AccountHooks. --- TShockAPI/DB/RegionManager.cs | 23 ++++++- TShockAPI/Hooks/AccountHooks.cs | 4 +- TShockAPI/Hooks/RegionHooks.cs | 109 ++++++++++++++++++++++++++++++++ TShockAPI/TSPlayer.cs | 5 ++ TShockAPI/TShock.cs | 16 +++++ TShockAPI/TShockAPI.csproj | 3 +- 6 files changed, 156 insertions(+), 4 deletions(-) create mode 100644 TShockAPI/Hooks/RegionHooks.cs diff --git a/TShockAPI/DB/RegionManager.cs b/TShockAPI/DB/RegionManager.cs index deb365b9..4655b753 100755 --- a/TShockAPI/DB/RegionManager.cs +++ b/TShockAPI/DB/RegionManager.cs @@ -115,6 +115,18 @@ namespace TShockAPI.DB } } + /// + /// Adds a region to the database. + /// + /// TileX of the top left corner. + /// TileY of the top left corner. + /// Width of the region in tiles. + /// Height of the region in tiles. + /// The name of the region. + /// The User Account Name of the person who created this region. + /// The world id that this region is in. + /// The Z index of the region. + /// Whether the region was created and added successfully. public bool AddRegion(int tx, int ty, int width, int height, string regionname, string owner, string worldid, int z = 0) { if (GetRegionByName(regionname) != null) @@ -126,7 +138,9 @@ namespace TShockAPI.DB 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)); + var region = new Region(new Rectangle(tx, ty, width, height), regionname, owner, true, worldid, z); + Regions.Add(region); + Hooks.RegionHooks.OnRegionCreated(region); return true; } catch (Exception ex) @@ -136,13 +150,20 @@ namespace TShockAPI.DB return false; } + /// + /// Deletes the region from this world with a given name. + /// + /// The name of the region to delete. + /// Whether the region was successfully deleted. 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(); + var region = Regions.FirstOrDefault(r => r.Name == name && r.WorldID == worldid); Regions.RemoveAll(r => r.Name == name && r.WorldID == worldid); + Hooks.RegionHooks.OnRegionDeleted(region); return true; } catch (Exception ex) diff --git a/TShockAPI/Hooks/AccountHooks.cs b/TShockAPI/Hooks/AccountHooks.cs index e9bf76d2..8c82d0ed 100755 --- a/TShockAPI/Hooks/AccountHooks.cs +++ b/TShockAPI/Hooks/AccountHooks.cs @@ -21,7 +21,7 @@ namespace TShockAPI.Hooks { public class AccountDeleteEventArgs { - public User User { get; set; } + public User User { get; private set; } public AccountDeleteEventArgs(User user) { @@ -31,7 +31,7 @@ namespace TShockAPI.Hooks public class AccountCreateEventArgs { - public User User { get; set; } + public User User { get; private set; } public AccountCreateEventArgs(User user) { diff --git a/TShockAPI/Hooks/RegionHooks.cs b/TShockAPI/Hooks/RegionHooks.cs new file mode 100644 index 00000000..9a76bf2f --- /dev/null +++ b/TShockAPI/Hooks/RegionHooks.cs @@ -0,0 +1,109 @@ +/* +TShock, a server mod for Terraria +Copyright (C) 2011-2015 Nyx Studios (fka. 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 TShockAPI.DB; + +namespace TShockAPI.Hooks +{ + class RegionHooks + { + internal class RegionEnteredEventArgs + { + public TSPlayer Player { get; private set; } + + public RegionEnteredEventArgs(TSPlayer ply) + { + Player = ply; + } + } + + public delegate void RegionEnteredD(RegionEnteredEventArgs args); + public static event RegionEnteredD RegionEntered; + public static void OnRegionEntered(TSPlayer player) + { + if (RegionEntered == null) + { + return; + } + + RegionEntered(new RegionEnteredEventArgs(player)); + } + + internal class RegionLeftEventArgs + { + public TSPlayer Player { get; private set; } + + public RegionLeftEventArgs(TSPlayer ply) + { + Player = ply; + } + } + + public delegate void RegionLeftD(RegionLeftEventArgs args); + public static event RegionLeftD RegionLeft; + public static void OnRegionLeft(TSPlayer player) + { + if (RegionLeft == null) + { + return; + } + + RegionLeft(new RegionLeftEventArgs(player)); + } + + internal class RegionCreatedEventArgs + { + public Region Region { get; private set; } + + public RegionCreatedEventArgs(Region region) + { + Region = region; + } + } + + public delegate void RegionCreatedD(RegionCreatedEventArgs args); + public static event RegionCreatedD RegionCreated; + public static void OnRegionCreated(Region region) + { + if (RegionCreated == null) + return; + + RegionCreated(new RegionCreatedEventArgs(region)); + } + + internal class RegionDeletedEventArgs + { + public Region Region { get; private set; } + + public RegionDeletedEventArgs(Region region) + { + Region = region; + } + } + + public delegate void RegionDeletedD(RegionDeletedEventArgs args); + public static event RegionDeletedD RegionDeleted; + public static void OnRegionDeleted(Region region) + { + if (RegionDeleted == null) + return; + + RegionDeleted(new RegionDeletedEventArgs(region)); + } + } +} diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index a6f10250..bf9d460a 100755 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -303,6 +303,11 @@ namespace TShockAPI /// The last projectile type this player tried to kill. /// public int LastKilledProjectile = 0; + + /// + /// The current region this player is in, or null if none. + /// + public Region CurrentRegion = null; /// /// Whether the player is a real, human, player on the server. diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 195ba40d..6e2977d6 100755 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -818,6 +818,22 @@ namespace TShockAPI { player.SetBuff(23, 120); //Cursed } + + var oldRegion = player.CurrentRegion; + player.CurrentRegion = Regions.GetTopRegion(Regions.InAreaRegion(player.TileX, player.TileY)); + + if (oldRegion != player.CurrentRegion) + { + if (oldRegion != null) + { + Hooks.RegionHooks.OnRegionLeft(player); + } + + if (player.CurrentRegion != null) + { + Hooks.RegionHooks.OnRegionEntered(player); + } + } } } SetConsoleTitle(false); diff --git a/TShockAPI/TShockAPI.csproj b/TShockAPI/TShockAPI.csproj index 6a886a38..176e11dd 100644 --- a/TShockAPI/TShockAPI.csproj +++ b/TShockAPI/TShockAPI.csproj @@ -79,6 +79,7 @@ + @@ -185,7 +186,7 @@ - +