From 11a2a3e21b6978d1eb451817b624323da77c2a30 Mon Sep 17 00:00:00 2001 From: Patrikkk Date: Thu, 18 Jun 2020 16:34:33 +0200 Subject: [PATCH 1/9] GetDataHandler - Code formatting. Modify order of Handler to respect PacketType value. --- TShockAPI/GetDataHandlers.cs | 67 ++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index 4a1c90fd..d0024e51 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -1870,6 +1870,39 @@ namespace TShockAPI return args.Handled; } + /// For use in an Emoji event. + /// + public class EmojiEventArgs : GetDataHandledEventArgs + { + /// + /// The player index in the packet, who sends the emoji. + /// + public byte PlayerIndex { get; set; } + /// + /// The ID of the emoji, that is being received. + /// + public byte EmojiID { get; set; } + } + /// + /// Called when a player sends an emoji. + /// + public static HandlerList Emoji = new HandlerList(); + private static bool OnEmoji(TSPlayer player, MemoryStream data, byte playerIndex, byte emojiID) + { + if (Emoji == null) + return false; + + var args = new EmojiEventArgs + { + Player = player, + Data = data, + PlayerIndex = playerIndex, + EmojiID = emojiID + }; + Emoji.Invoke(null, args); + return args.Handled; + } + /// /// For use in a SyncTilePicking event. /// @@ -1912,40 +1945,6 @@ namespace TShockAPI return args.Handled; } - - /// For use in an Emoji event. - /// - public class EmojiEventArgs : GetDataHandledEventArgs - { - /// - /// The player index in the packet, who sends the emoji. - /// - public byte PlayerIndex { get; set; } - /// - /// The ID of the emoji, that is being received. - /// - public byte EmojiID { get; set; } - } - /// - /// Called when a player sends an emoji. - /// - public static HandlerList Emoji = new HandlerList(); - private static bool OnEmoji(TSPlayer player, MemoryStream data, byte playerIndex, byte emojiID) - { - if (Emoji == null) - return false; - - var args = new EmojiEventArgs - { - Player = player, - Data = data, - PlayerIndex = playerIndex, - EmojiID = emojiID - }; - Emoji.Invoke(null, args); - return args.Handled; - } - /// /// For use in a LandBallInCup event. /// From 3012c923706d271110fdf8787a522a80f526714f Mon Sep 17 00:00:00 2001 From: Patrikkk Date: Thu, 18 Jun 2020 16:48:22 +0200 Subject: [PATCH 2/9] GetDataHandlers - Add RequestTileEntityInteraction hook. --- TShockAPI/GetDataHandlers.cs | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index d0024e51..3276d86d 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -152,6 +152,7 @@ namespace TShockAPI { PacketTypes.PlayerHurtV2, HandlePlayerDamageV2 }, { PacketTypes.PlayerDeathV2, HandlePlayerKillMeV2 }, { PacketTypes.Emoji, HandleEmoji }, + { PacketTypes.RequestTileEntityInteraction, HandleRequestTileEntityInteraction }, { PacketTypes.SyncTilePicking, HandleSyncTilePicking }, { PacketTypes.SyncRevengeMarker, HandleSyncRevengeMarker }, { PacketTypes.LandGolfBallInCup, HandleLandGolfBallInCup }, @@ -1903,6 +1904,39 @@ namespace TShockAPI return args.Handled; } + /// For use in an OnRequestTileEntityInteraction event. + /// + public class RequestTileEntityInteractionEventArgs : GetDataHandledEventArgs + { + /// + /// The ID of the TileEntity that the player is requesting interaction with. + /// + public int TileEntityID { get; set; } + /// + /// The player index in the packet who requests interaction with the TileEntity. + /// + public byte PlayerIndex { get; set; } + } + /// + /// Called when a player requests interaction with a TileEntity. + /// + public static HandlerList RequestTileEntityInteraction = new HandlerList(); + private static bool OnRequestTileEntityInteraction(TSPlayer player, MemoryStream data, int tileEntityID, byte playerIndex) + { + if (RequestTileEntityInteraction == null) + return false; + + var args = new RequestTileEntityInteractionEventArgs + { + Player = player, + Data = data, + PlayerIndex = playerIndex, + TileEntityID = tileEntityID + }; + RequestTileEntityInteraction.Invoke(null, args); + return args.Handled; + } + /// /// For use in a SyncTilePicking event. /// @@ -3702,6 +3736,17 @@ namespace TShockAPI return false; } + private static bool HandleRequestTileEntityInteraction(GetDataHandlerArgs args) + { + int tileEntityID = args.Data.ReadInt32(); + byte playerIndex = args.Data.ReadInt8(); + + if (OnRequestTileEntityInteraction(args.Player, args.Data, tileEntityID, playerIndex)) + return true; + + return false; + } + private static bool HandleSyncTilePicking(GetDataHandlerArgs args) { byte playerIndex = args.Data.ReadInt8(); From c418dda42ce264f3287777aa3fa2cd83373358f3 Mon Sep 17 00:00:00 2001 From: Patrikkk Date: Thu, 18 Jun 2020 17:08:14 +0200 Subject: [PATCH 3/9] Add RequestTileEntityInteractionHandler. Check Mannequin modif perms. Users can no longer open a Mannequin if they do not have building permissions at the position of the Mannequin. (Mannequins work as a chest since 1.4) --- TShockAPI/Bouncer.cs | 6 ++- .../RequestTileEntityInteractionHandler.cs | 37 +++++++++++++++++++ TShockAPI/TShockAPI.csproj | 1 + 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 TShockAPI/Handlers/RequestTileEntityInteractionHandler.cs diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index 0ab38458..6f6959bc 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -39,6 +39,7 @@ namespace TShockAPI internal Handlers.SendTileSquareHandler STSHandler { get; set; } internal Handlers.NetModules.NetModulePacketHandler NetModuleHandler { get; set; } internal Handlers.EmojiHandler EmojiHandler { get; set; } + internal Handlers.RequestTileEntityInteractionHandler RequestTileEntityInteractionHandler { get; set; } internal Handlers.LandGolfBallInCupHandler LandGolfBallInCupHandler { get; set; } internal Handlers.SyncTilePickingHandler SyncTilePickingHandler { get; set; } @@ -54,7 +55,10 @@ namespace TShockAPI EmojiHandler = new Handlers.EmojiHandler(); GetDataHandlers.Emoji += EmojiHandler.OnReceive; - + + RequestTileEntityInteractionHandler = new Handlers.RequestTileEntityInteractionHandler(); + GetDataHandlers.RequestTileEntityInteraction += RequestTileEntityInteractionHandler.OnReceive; + LandGolfBallInCupHandler = new Handlers.LandGolfBallInCupHandler(); GetDataHandlers.LandGolfBallInCup += LandGolfBallInCupHandler.OnReceive; diff --git a/TShockAPI/Handlers/RequestTileEntityInteractionHandler.cs b/TShockAPI/Handlers/RequestTileEntityInteractionHandler.cs new file mode 100644 index 00000000..0cf77823 --- /dev/null +++ b/TShockAPI/Handlers/RequestTileEntityInteractionHandler.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Terraria.DataStructures; +using Terraria.GameContent.Tile_Entities; +using static TShockAPI.GetDataHandlers; + +namespace TShockAPI.Handlers +{ + /// + /// + /// + public class RequestTileEntityInteractionHandler : IPacketHandler + { + public void OnReceive(object sender, RequestTileEntityInteractionEventArgs args) + { + if (args.TileEntityID != -1) + { + TileEntity tileEntity; + if (TileEntity.ByID.TryGetValue(args.TileEntityID, out tileEntity)) + { + if (tileEntity is TEDisplayDoll) + { + if (!args.Player.HasBuildPermission(tileEntity.Position.X, tileEntity.Position.Y, false)) + { + args.Player.SendErrorMessage("You have no permission to modify a Mannequin in a protected area!"); + args.Handled = true; + return; + } + } + } + } + } + } +} diff --git a/TShockAPI/TShockAPI.csproj b/TShockAPI/TShockAPI.csproj index f9885393..07d9ae8b 100644 --- a/TShockAPI/TShockAPI.csproj +++ b/TShockAPI/TShockAPI.csproj @@ -99,6 +99,7 @@ + From fec5deedd94cac3b2217cf0ca59944c92a63c65f Mon Sep 17 00:00:00 2001 From: Patrikkk Date: Thu, 18 Jun 2020 17:11:42 +0200 Subject: [PATCH 4/9] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04bc68f4..fe45a0df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,9 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Cleaned up a check in Bouner OnTileEdit where it checks for using the respective item when placing a tile to make it clearer. This change also fixed the issue in a previous commit where valid replace action was caught. Moved the check for max tile/wall types to the beginning of the method. (@Patrikkk) * Improved clarity for insufficient permission related error messages. (@moisterrific) * Remove redundant Boulder placement check that prevented placing chests on them, as it is no longer possible to place a chest on a boulder, so nothing crashes the server. "1.2.3: Boulders with Chests on them no longer crash the game if the boulder is hit." (@kevzhao2, @Patrikkk) +* RequestTileEntity packet handling. (@Patrikkk) + * Implemented the OnRequestTileEntityInteraction even hook in GetDataHandler. (@Patrikkk) + * Created RequestTileEntityInteractionHandler which checks for building permissions when the player is attempting to open a display doll (Mannequin). This now prevents players to open a mannequin if they have no building permissions at the position of the Mannequin. ## TShock 4.4.0 (Pre-release 11) * New permission `tshock.tp.pylon` to enable teleporting via Teleportation Pylons (@QuiCM) From 2a0e1de285b9daff3959551cd1bcfb0aa5a89d4e Mon Sep 17 00:00:00 2001 From: Patrikkk Date: Thu, 18 Jun 2020 17:13:39 +0200 Subject: [PATCH 5/9] Changelog - Grammar fixes --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe45a0df..853b4573 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,7 +23,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Remove redundant Boulder placement check that prevented placing chests on them, as it is no longer possible to place a chest on a boulder, so nothing crashes the server. "1.2.3: Boulders with Chests on them no longer crash the game if the boulder is hit." (@kevzhao2, @Patrikkk) * RequestTileEntity packet handling. (@Patrikkk) * Implemented the OnRequestTileEntityInteraction even hook in GetDataHandler. (@Patrikkk) - * Created RequestTileEntityInteractionHandler which checks for building permissions when the player is attempting to open a display doll (Mannequin). This now prevents players to open a mannequin if they have no building permissions at the position of the Mannequin. + * Created RequestTileEntityInteractionHandler which checks for building permissions when the player is attempting to open a display doll (Mannequin). This now prevents players from opening a mannequin if they have no building permissions at the position of the mannequin. (@Patrikkk) ## TShock 4.4.0 (Pre-release 11) * New permission `tshock.tp.pylon` to enable teleporting via Teleportation Pylons (@QuiCM) From bb206262033c51f6415ef53b12b442e7e6912c07 Mon Sep 17 00:00:00 2001 From: Patrikkk Date: Thu, 18 Jun 2020 17:32:08 +0200 Subject: [PATCH 6/9] Add building permission checks for Hat Rack modification. Keeping this code format to have the code be friendly with us in future updates, as well as display proper rejection message to the players. --- .../RequestTileEntityInteractionHandler.cs | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/TShockAPI/Handlers/RequestTileEntityInteractionHandler.cs b/TShockAPI/Handlers/RequestTileEntityInteractionHandler.cs index 0cf77823..386a31e6 100644 --- a/TShockAPI/Handlers/RequestTileEntityInteractionHandler.cs +++ b/TShockAPI/Handlers/RequestTileEntityInteractionHandler.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Terraria; using Terraria.DataStructures; using Terraria.GameContent.Tile_Entities; using static TShockAPI.GetDataHandlers; @@ -21,14 +22,24 @@ namespace TShockAPI.Handlers TileEntity tileEntity; if (TileEntity.ByID.TryGetValue(args.TileEntityID, out tileEntity)) { - if (tileEntity is TEDisplayDoll) + if (tileEntity is TEHatRack && !args.Player.HasBuildPermission(tileEntity.Position.X, tileEntity.Position.Y, false)) { - if (!args.Player.HasBuildPermission(tileEntity.Position.X, tileEntity.Position.Y, false)) - { - args.Player.SendErrorMessage("You have no permission to modify a Mannequin in a protected area!"); - args.Handled = true; - return; - } + args.Player.SendErrorMessage("You do not have permission to modify a Hat Rack in a protected area!"); + args.Handled = true; + return; + } + else if (tileEntity is TEDisplayDoll && !args.Player.HasBuildPermission(tileEntity.Position.X, tileEntity.Position.Y, false)) + { + args.Player.SendErrorMessage("You do not have permission to modify a Mannequin in a protected area!"); + args.Handled = true; + return; + } + else if (!args.Player.HasBuildPermission(tileEntity.Position.X, tileEntity.Position.Y, false)) + { + args.Player.SendErrorMessage("You do not have permission to modify a TileEntity in a protected area!"); + TShock.Log.ConsoleDebug($"RequestTileEntityInteractionHandler: Rejected packet due to lack of building permissions! - From {args.Player.Name} | Position X:{tileEntity.Position.X} Y:{tileEntity.Position.Y}, TileEntity type: {tileEntity.type}, Tile type: {Main.tile[tileEntity.Position.X, tileEntity.Position.Y].type}"); + args.Handled = true; + return; } } } From 0874177edbe380d6a208ccd77d5122079e6b7f1b Mon Sep 17 00:00:00 2001 From: Patrikkk Date: Thu, 18 Jun 2020 17:37:29 +0200 Subject: [PATCH 7/9] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 853b4573..a75f0d39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,7 +23,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Remove redundant Boulder placement check that prevented placing chests on them, as it is no longer possible to place a chest on a boulder, so nothing crashes the server. "1.2.3: Boulders with Chests on them no longer crash the game if the boulder is hit." (@kevzhao2, @Patrikkk) * RequestTileEntity packet handling. (@Patrikkk) * Implemented the OnRequestTileEntityInteraction even hook in GetDataHandler. (@Patrikkk) - * Created RequestTileEntityInteractionHandler which checks for building permissions when the player is attempting to open a display doll (Mannequin). This now prevents players from opening a mannequin if they have no building permissions at the position of the mannequin. (@Patrikkk) + * Created RequestTileEntityInteractionHandler which checks for building permissions when the player is attempting to open a display doll (Mannequin) or a Hat Rack. This now prevents players from opening a Mannequin or a Hat Rack if they have no building permissions at the position of these tile entities. As of 1.4.0.5, these are the only two items that use this packet. (@Patrikkk) ## TShock 4.4.0 (Pre-release 11) * New permission `tshock.tp.pylon` to enable teleporting via Teleportation Pylons (@QuiCM) From 89ab7be0f8db22b1abe809bc1f2eeb23d4422c4e Mon Sep 17 00:00:00 2001 From: Patrikkk Date: Thu, 25 Jun 2020 01:51:30 +0200 Subject: [PATCH 8/9] TileEntityInteraction - Pass TileEntity object instead of ID in args. --- TShockAPI/GetDataHandlers.cs | 13 +++--- .../RequestTileEntityInteractionHandler.cs | 41 ++++++++----------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index 3276d86d..dcec0c18 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -1909,9 +1909,9 @@ namespace TShockAPI public class RequestTileEntityInteractionEventArgs : GetDataHandledEventArgs { /// - /// The ID of the TileEntity that the player is requesting interaction with. + /// The TileEntity object that the player is requesting interaction with. /// - public int TileEntityID { get; set; } + public TileEntity TileEntity { get; set; } /// /// The player index in the packet who requests interaction with the TileEntity. /// @@ -1921,7 +1921,7 @@ namespace TShockAPI /// Called when a player requests interaction with a TileEntity. /// public static HandlerList RequestTileEntityInteraction = new HandlerList(); - private static bool OnRequestTileEntityInteraction(TSPlayer player, MemoryStream data, int tileEntityID, byte playerIndex) + private static bool OnRequestTileEntityInteraction(TSPlayer player, MemoryStream data, TileEntity tileEntity, byte playerIndex) { if (RequestTileEntityInteraction == null) return false; @@ -1931,7 +1931,7 @@ namespace TShockAPI Player = player, Data = data, PlayerIndex = playerIndex, - TileEntityID = tileEntityID + TileEntity = tileEntity }; RequestTileEntityInteraction.Invoke(null, args); return args.Handled; @@ -3741,7 +3741,10 @@ namespace TShockAPI int tileEntityID = args.Data.ReadInt32(); byte playerIndex = args.Data.ReadInt8(); - if (OnRequestTileEntityInteraction(args.Player, args.Data, tileEntityID, playerIndex)) + if (!TileEntity.ByID.TryGetValue(tileEntityID, out TileEntity tileEntity)) + return false; + + if (OnRequestTileEntityInteraction(args.Player, args.Data, tileEntity, playerIndex)) return true; return false; diff --git a/TShockAPI/Handlers/RequestTileEntityInteractionHandler.cs b/TShockAPI/Handlers/RequestTileEntityInteractionHandler.cs index 386a31e6..cb4e80ea 100644 --- a/TShockAPI/Handlers/RequestTileEntityInteractionHandler.cs +++ b/TShockAPI/Handlers/RequestTileEntityInteractionHandler.cs @@ -17,31 +17,24 @@ namespace TShockAPI.Handlers { public void OnReceive(object sender, RequestTileEntityInteractionEventArgs args) { - if (args.TileEntityID != -1) + if (args.TileEntity is TEHatRack && !args.Player.HasBuildPermission(args.TileEntity.Position.X, args.TileEntity.Position.Y, false)) { - TileEntity tileEntity; - if (TileEntity.ByID.TryGetValue(args.TileEntityID, out tileEntity)) - { - if (tileEntity is TEHatRack && !args.Player.HasBuildPermission(tileEntity.Position.X, tileEntity.Position.Y, false)) - { - args.Player.SendErrorMessage("You do not have permission to modify a Hat Rack in a protected area!"); - args.Handled = true; - return; - } - else if (tileEntity is TEDisplayDoll && !args.Player.HasBuildPermission(tileEntity.Position.X, tileEntity.Position.Y, false)) - { - args.Player.SendErrorMessage("You do not have permission to modify a Mannequin in a protected area!"); - args.Handled = true; - return; - } - else if (!args.Player.HasBuildPermission(tileEntity.Position.X, tileEntity.Position.Y, false)) - { - args.Player.SendErrorMessage("You do not have permission to modify a TileEntity in a protected area!"); - TShock.Log.ConsoleDebug($"RequestTileEntityInteractionHandler: Rejected packet due to lack of building permissions! - From {args.Player.Name} | Position X:{tileEntity.Position.X} Y:{tileEntity.Position.Y}, TileEntity type: {tileEntity.type}, Tile type: {Main.tile[tileEntity.Position.X, tileEntity.Position.Y].type}"); - args.Handled = true; - return; - } - } + args.Player.SendErrorMessage("You do not have permission to modify a Hat Rack in a protected area!"); + args.Handled = true; + return; + } + else if (args.TileEntity is TEDisplayDoll && !args.Player.HasBuildPermission(args.TileEntity.Position.X, args.TileEntity.Position.Y, false)) + { + args.Player.SendErrorMessage("You do not have permission to modify a Mannequin in a protected area!"); + args.Handled = true; + return; + } + else if (!args.Player.HasBuildPermission(args.TileEntity.Position.X, args.TileEntity.Position.Y, false)) + { + args.Player.SendErrorMessage("You do not have permission to modify a TileEntity in a protected area!"); + TShock.Log.ConsoleDebug($"RequestTileEntityInteractionHandler: Rejected packet due to lack of building permissions! - From {args.Player.Name} | Position X:{args.TileEntity.Position.X} Y:{args.TileEntity.Position.Y}, TileEntity type: {args.TileEntity.type}, Tile type: {Main.tile[args.TileEntity.Position.X, args.TileEntity.Position.Y].type}"); + args.Handled = true; + return; } } } From 530bc951230544b7ff7ddd89602dab5c755a73e1 Mon Sep 17 00:00:00 2001 From: Patrikkk Date: Sat, 27 Jun 2020 10:22:03 +0200 Subject: [PATCH 9/9] RequestTileEntityInteractionHandler - Use object size for building perm check. This commit will modify the RequestTileEntityInteractionHandler to use HasBuildPermissionForTileObject when checking for building permissions for Hat Rack and Display Doll to give an accurate response wether or not any part of the object has an overlapping protected region. --- TShockAPI/Handlers/RequestTileEntityInteractionHandler.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TShockAPI/Handlers/RequestTileEntityInteractionHandler.cs b/TShockAPI/Handlers/RequestTileEntityInteractionHandler.cs index cb4e80ea..b2505307 100644 --- a/TShockAPI/Handlers/RequestTileEntityInteractionHandler.cs +++ b/TShockAPI/Handlers/RequestTileEntityInteractionHandler.cs @@ -17,13 +17,13 @@ namespace TShockAPI.Handlers { public void OnReceive(object sender, RequestTileEntityInteractionEventArgs args) { - if (args.TileEntity is TEHatRack && !args.Player.HasBuildPermission(args.TileEntity.Position.X, args.TileEntity.Position.Y, false)) + if (args.TileEntity is TEHatRack && !args.Player.HasBuildPermissionForTileObject(args.TileEntity.Position.X, args.TileEntity.Position.Y, TEHatRack.entityTileWidth, TEHatRack.entityTileHeight, false)) { args.Player.SendErrorMessage("You do not have permission to modify a Hat Rack in a protected area!"); args.Handled = true; return; } - else if (args.TileEntity is TEDisplayDoll && !args.Player.HasBuildPermission(args.TileEntity.Position.X, args.TileEntity.Position.Y, false)) + else if (args.TileEntity is TEDisplayDoll && !args.Player.HasBuildPermissionForTileObject(args.TileEntity.Position.X, args.TileEntity.Position.Y, TEDisplayDoll.entityTileWidth, TEDisplayDoll.entityTileHeight, false)) { args.Player.SendErrorMessage("You do not have permission to modify a Mannequin in a protected area!"); args.Handled = true;