From bd7b1c1460609d471b5dd22b42f2a7e7ea0301db Mon Sep 17 00:00:00 2001 From: tru321 <84877585+tru321@users.noreply.github.com> Date: Wed, 8 Dec 2021 11:05:33 +0800 Subject: [PATCH 1/8] Update GetDataHandlers.cs --- TShockAPI/GetDataHandlers.cs | 74 ++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index bf0fb54e..4e1ae400 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -135,6 +135,7 @@ namespace TShockAPI { PacketTypes.Teleport, HandleTeleport }, { PacketTypes.PlayerHealOther, HandleHealOther }, { PacketTypes.CatchNPC, HandleCatchNpc }, + { PacketTypes.ReleaseNPC, HandleReleaseNpc }, { PacketTypes.TeleportationPotion, HandleTeleportationPotion }, { PacketTypes.CompleteAnglerQuest, HandleCompleteAnglerQuest }, { PacketTypes.NumberOfAnglerQuestsCompleted, HandleNumberOfAnglerQuestsCompleted }, @@ -1638,6 +1639,56 @@ namespace TShockAPI return args.Handled; } + /// + /// The ReleaseNPC event arguments + /// + public class ReleaseNpcEventArgs : GetDataHandledEventArgs + { + /// + /// The X value of where NPC released + /// + public int X { get; set; } + + /// + /// The Y value of where NPC released + /// + public int Y { get; set; } + + /// + /// The NPC Type that player release + /// + public short Type { get; set; } + + /// + /// The NPC release style + /// + public byte Style { get; set; } + } + + /// + /// Called when player release a NPC, for checking critter released from item. + /// + public static HandlerList ReleaseNPC = new HandlerList(); + private static bool OnReleaseNpc(TSPlayer player, MemoryStream data, int _x, int _y, short _type, byte _style) + { + if (ReleaseNPC == null) + { + return false; + } + + var args = new ReleaseNpcEventArgs + { + Player = player, + Data = data, + X = _x, + Y = _y, + Type = _type, + Style = _style + }; + ReleaseNPC.Invoke(null, args); + return args.Handled; + } + /// The arguments to the PlaceObject hook. public class PlaceObjectEventArgs : GetDataHandledEventArgs { @@ -3658,10 +3709,33 @@ namespace TShockAPI NetMessage.SendData((int)PacketTypes.NpcUpdate, -1, -1, NetworkText.Empty, npcID); return true; } + + if(args.Player.IsBeingDisabled()) + { + TShock.Log.ConsoleDebug("GetDataHandlers / HandleCatchNpc rejected catch npc {0}", args.Player.Name); + Main.npc[npcID].active = true; + NetMessage.SendData((int)PacketTypes.NpcUpdate, -1, -1, NetworkText.Empty, npcID); + return true; + } return false; } + private static bool HandleReleaseNpc(GetDataHandlerArgs args) + { + var x = args.Data.ReadInt32(); + var y = args.Data.ReadInt32(); + var type = args.Data.ReadInt16(); + var style = args.Data.ReadInt8(); + + if (OnReleaseNpc(args.Player, args.Data, x, y, type, style)) + { + return true; + } + + return false; + } + private static bool HandleTeleportationPotion(GetDataHandlerArgs args) { var type = args.Data.ReadByte(); From d4befdeff01ca4905afb18624e771a0209c7f8af Mon Sep 17 00:00:00 2001 From: tru321 <84877585+tru321@users.noreply.github.com> Date: Wed, 8 Dec 2021 11:10:12 +0800 Subject: [PATCH 2/8] Update Bouncer.cs --- TShockAPI/Bouncer.cs | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index 2145b373..d7797fed 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -105,6 +105,7 @@ namespace TShockAPI GetDataHandlers.NPCAddBuff += OnNPCAddBuff; GetDataHandlers.NPCHome += OnUpdateNPCHome; GetDataHandlers.HealOtherPlayer += OnHealOtherPlayer; + GetDataHandlers.ReleaseNPC += OnReleaseNPC; GetDataHandlers.PlaceObject += OnPlaceObject; GetDataHandlers.PlaceTileEntity += OnPlaceTileEntity; GetDataHandlers.PlaceItemFrame += OnPlaceItemFrame; @@ -1829,6 +1830,52 @@ namespace TShockAPI return; } + /// + /// A bouncer for checking NPC released by player + /// + /// The object that triggered the event. + /// The packet arguments that the event has. + internal void OnReleaseNPC(object sender, GetDataHandlers.ReleaseNpcEventArgs args) + { + int x = args.X; + int y = args.Y; + short type = args.Type; + byte style = args.Style; + + // if npc released outside allowed tile + if ( x >= Main.maxTilesX * 16 - 16 || x < 0 || y >= Main.maxTilesY * 16 - 16 || y < 0) + { + TShock.Log.ConsoleDebug("Bouncer / OnReleaseNPC rejected out of bounds from {0}", args.Player.Name); + args.Handled = true; + return; + } + + // if player disabled + if (args.Player.IsBeingDisabled()) + { + TShock.Log.ConsoleDebug("Bouncer / OnReleaseNPC rejected npc release from {0}", args.Player.Name); + args.Handled = true; + return; + } + + // if released npc not from its item (from crafted packet) + // e.g. using bunny item to release golden bunny + if(args.Player.TPlayer.lastVisualizedSelectedItem.makeNPC != type) + { + TShock.Log.ConsoleDebug("Bouncer / OnReleaseNPC rejected npc release from {0}", args.Player.Name); + args.Player.Kick("Trying to release different critter exploit!", true); + args.Handled = true; + return; + } + + if (args.Player.IsBouncerThrottled()) + { + TShock.Log.ConsoleDebug("Bouncer / OnReleaseNPC rejected throttle from {0}", args.Player.Name); + args.Handled = true; + return; + } + } + /// Bouncer's PlaceObject hook reverts malicious tile placement. /// The object that triggered the event. /// The packet arguments that the event has. From 26a46ef40c005d19ee7e1ddde708125e0fb895f1 Mon Sep 17 00:00:00 2001 From: tru321 <84877585+tru321@users.noreply.github.com> Date: Wed, 8 Dec 2021 11:48:55 +0800 Subject: [PATCH 3/8] Update Bouncer.cs --- TShockAPI/Bouncer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index d7797fed..e8a2b43c 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -1860,7 +1860,7 @@ namespace TShockAPI // if released npc not from its item (from crafted packet) // e.g. using bunny item to release golden bunny - if(args.Player.TPlayer.lastVisualizedSelectedItem.makeNPC != type) + if(args.Player.TPlayer.lastVisualizedSelectedItem.makeNPC != type && args.Player.TPlayer.lastVisualizedSelectedItem.placeStyle != style) { TShock.Log.ConsoleDebug("Bouncer / OnReleaseNPC rejected npc release from {0}", args.Player.Name); args.Player.Kick("Trying to release different critter exploit!", true); From 8d54033404c97b48ddac39e6e7a20c9fc935ca78 Mon Sep 17 00:00:00 2001 From: tru321 <84877585+tru321@users.noreply.github.com> Date: Wed, 8 Dec 2021 12:04:47 +0800 Subject: [PATCH 4/8] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4412bdb8..719c0258 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Do not insert tabs into this file, under any circumstances, ever. * Do not forget to sign every line you change with your name. (@hakusaro) * If there is no section called "Upcoming changes" below this line, please add one with `## Upcoming changes` as the first line, and then a bulleted item directly after with the first change. +## Upcoming changes +* Added hook `GetDataHandlers.OnReleaseNpc` to handling ReleaseNPC packet and a bouncer to stops unregistered and logged out players from releasing critters NPC. The bouncer has additional filter to stops players who tried to release different critter using crafted packet, e.g. using bunny item to release golden bunny. (@tru321) +* Added filter in `GetDataHandlers.HandleCatchNpc` that stops unregistered and logged out players to catch critters. (@tru321) ## TShock 4.5.12 * Fixed the ability to spawn Zenith projectile with non-original items. (@AgaSpace) From 16f7c876e135a4e816438f05be363280cba702ec Mon Sep 17 00:00:00 2001 From: tru321 <84877585+tru321@users.noreply.github.com> Date: Wed, 8 Dec 2021 14:44:03 +0800 Subject: [PATCH 5/8] Apply suggestions from code review Co-authored-by: Lucas Nicodemus --- TShockAPI/Bouncer.cs | 4 ++-- TShockAPI/GetDataHandlers.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index e8a2b43c..cdb4e3c6 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -1843,7 +1843,7 @@ namespace TShockAPI byte style = args.Style; // if npc released outside allowed tile - if ( x >= Main.maxTilesX * 16 - 16 || x < 0 || y >= Main.maxTilesY * 16 - 16 || y < 0) + if (x >= Main.maxTilesX * 16 - 16 || x < 0 || y >= Main.maxTilesY * 16 - 16 || y < 0) { TShock.Log.ConsoleDebug("Bouncer / OnReleaseNPC rejected out of bounds from {0}", args.Player.Name); args.Handled = true; @@ -1860,7 +1860,7 @@ namespace TShockAPI // if released npc not from its item (from crafted packet) // e.g. using bunny item to release golden bunny - if(args.Player.TPlayer.lastVisualizedSelectedItem.makeNPC != type && args.Player.TPlayer.lastVisualizedSelectedItem.placeStyle != style) + if (args.Player.TPlayer.lastVisualizedSelectedItem.makeNPC != type && args.Player.TPlayer.lastVisualizedSelectedItem.placeStyle != style) { TShock.Log.ConsoleDebug("Bouncer / OnReleaseNPC rejected npc release from {0}", args.Player.Name); args.Player.Kick("Trying to release different critter exploit!", true); diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index 4e1ae400..a3da07f8 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -1666,7 +1666,7 @@ namespace TShockAPI } /// - /// Called when player release a NPC, for checking critter released from item. + /// Called when player release a NPC, for checking critter released from item. /// public static HandlerList ReleaseNPC = new HandlerList(); private static bool OnReleaseNpc(TSPlayer player, MemoryStream data, int _x, int _y, short _type, byte _style) From e0d6252693daf6ffcfc56fb1ee4845322c422ba3 Mon Sep 17 00:00:00 2001 From: tru321 <84877585+tru321@users.noreply.github.com> Date: Fri, 10 Dec 2021 10:53:58 +0800 Subject: [PATCH 6/8] Update GetDataHandlers.cs --- TShockAPI/GetDataHandlers.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index a3da07f8..d67a4d53 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -3713,8 +3713,6 @@ namespace TShockAPI if(args.Player.IsBeingDisabled()) { TShock.Log.ConsoleDebug("GetDataHandlers / HandleCatchNpc rejected catch npc {0}", args.Player.Name); - Main.npc[npcID].active = true; - NetMessage.SendData((int)PacketTypes.NpcUpdate, -1, -1, NetworkText.Empty, npcID); return true; } From f20ce4aaa92d5faa50188e800544a04ffabd8977 Mon Sep 17 00:00:00 2001 From: tru321 <84877585+tru321@users.noreply.github.com> Date: Fri, 10 Dec 2021 10:57:56 +0800 Subject: [PATCH 7/8] Update Bouncer.cs --- TShockAPI/Bouncer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index cdb4e3c6..5269d0d2 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -1862,8 +1862,8 @@ namespace TShockAPI // e.g. using bunny item to release golden bunny if (args.Player.TPlayer.lastVisualizedSelectedItem.makeNPC != type && args.Player.TPlayer.lastVisualizedSelectedItem.placeStyle != style) { - TShock.Log.ConsoleDebug("Bouncer / OnReleaseNPC rejected npc release from {0}", args.Player.Name); - args.Player.Kick("Trying to release different critter exploit!", true); + TShock.Log.ConsoleDebug("Bouncer / OnReleaseNPC released different critter from {0}", args.Player.Name); + args.Player.Kick("Released critter was not from its item.", true); args.Handled = true; return; } From 3ab630e731f970f74c40d9d92f28c5224655f063 Mon Sep 17 00:00:00 2001 From: tru321 <84877585+tru321@users.noreply.github.com> Date: Sun, 12 Dec 2021 19:13:29 +0800 Subject: [PATCH 8/8] Update CHANGELOG.md --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 719c0258..0aba9c42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,8 +12,8 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Do not forget to sign every line you change with your name. (@hakusaro) * If there is no section called "Upcoming changes" below this line, please add one with `## Upcoming changes` as the first line, and then a bulleted item directly after with the first change. ## Upcoming changes -* Added hook `GetDataHandlers.OnReleaseNpc` to handling ReleaseNPC packet and a bouncer to stops unregistered and logged out players from releasing critters NPC. The bouncer has additional filter to stops players who tried to release different critter using crafted packet, e.g. using bunny item to release golden bunny. (@tru321) -* Added filter in `GetDataHandlers.HandleCatchNpc` that stops unregistered and logged out players to catch critters. (@tru321) +* Added hook `GetDataHandlers.OnReleaseNpc` to handling ReleaseNPC packet and a bouncer to stops unregistered and logged out players on SSC servers from releasing critters NPC. The bouncer has additional filter to stops players who tried to release different critter using crafted packet, e.g. using bunny item to release golden bunny. (@tru321) +* Added filter in `GetDataHandlers.HandleCatchNpc` that stops unregistered and logged out players on SSC servers to catch critters. (@tru321) ## TShock 4.5.12 * Fixed the ability to spawn Zenith projectile with non-original items. (@AgaSpace)