From 9910725258a8fd874bbded58ed7b06ea9eb7eaaf Mon Sep 17 00:00:00 2001 From: AkjaHAsLk1IALk0MasH <46046453+AgaSpace@users.noreply.github.com> Date: Tue, 30 Nov 2021 20:45:56 +0700 Subject: [PATCH 1/9] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62a73f8c..ef058577 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin ## TShock 4.5.12 * Fixed the ability to spawn Zenith projectile with non-original items. (@AgaSpace) +* Fixed the ability to create custom messages with your death (or the death of another player) (@AgaSpace) ## TShock 4.5.11 * Add the new allowed buff TentacleSpike to NPC buff cheat detection bouncer. (@sgkoishi) From 8f69c6e767c85caa2a7803eb7a01481a8d364468 Mon Sep 17 00:00:00 2001 From: AkjaHAsLk1IALk0MasH <46046453+AgaSpace@users.noreply.github.com> Date: Tue, 30 Nov 2021 20:50:51 +0700 Subject: [PATCH 2/9] Created a new field in config. --- TShockAPI/Configuration/TShockConfig.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/TShockAPI/Configuration/TShockConfig.cs b/TShockAPI/Configuration/TShockConfig.cs index 2c643fab..d87dfa22 100644 --- a/TShockAPI/Configuration/TShockConfig.cs +++ b/TShockAPI/Configuration/TShockConfig.cs @@ -460,6 +460,10 @@ namespace TShockAPI.Configuration /// Prohibit the use of Zenith projectile with different objects instead of weapons. [Description("Prohibit the use of Zenith projectile with different objects instead of weapons.")] public bool DisableModifiedZenith = false; + + /// Allows you to disable or enable protection against creating custom messages with death. Created for developers who came up with a more original solution to this problem. + [Description("Allows you to disable or enable protection against creating custom messages with death. Created for developers who came up with a more original solution to this problem.")] + public bool DisableCustomDeathMessages = true; #endregion From cdeec8aa040079c6e9890bc1ed22d90979a6978e Mon Sep 17 00:00:00 2001 From: AkjaHAsLk1IALk0MasH <46046453+AgaSpace@users.noreply.github.com> Date: Tue, 30 Nov 2021 20:53:19 +0700 Subject: [PATCH 3/9] Added protection in OnKillMe --- TShockAPI/Bouncer.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index 2145b373..3216d769 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -2279,6 +2279,12 @@ namespace TShockAPI args.Handled = true; return; } + if (TShock.Config.Settings.DisableCustomDeathMessages && playerDeathReason._sourceCustomReason != null) + { + TShock.Log.ConsoleDebug("Bouncer / OnKillMe rejected custom death message from {0}", args.Player.Name); + args.Handled = true; + return; + } } } From a00512fcf45d32750930371637694e6da71bdcd9 Mon Sep 17 00:00:00 2001 From: AkjaHAsLk1IALk0MasH <46046453+AgaSpace@users.noreply.github.com> Date: Tue, 30 Nov 2021 21:14:53 +0700 Subject: [PATCH 4/9] Added protection to OnPlayerDamage --- TShockAPI/Bouncer.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index 3216d769..deca614e 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -2178,6 +2178,7 @@ namespace TShockAPI bool pvp = args.PVP; bool crit = args.Critical; byte direction = args.Direction; + PlayerDeathReason reason = args.PlayerDeathReason; if (id >= Main.maxPlayers || TShock.Players[id] == null) { @@ -2242,6 +2243,17 @@ namespace TShockAPI return; } + /* + * A player must take damage from another player in any case + * I didn't add sourceProjectile because the player can't always die from a projectile. + */ + if (TShock.Config.Settings.DisableCustomDeathMessages && + (reason._sourcePlayerIndex == -1 || reason._sourceNPCIndex != -1 || reason._sourceOtherIndex != -1 || reason._sourceCustomReason != null)) + { + TShock.Log.ConsoleDebug("Bouncer / OnPlayerDamage rejected custom death message from {0}", args.Player.Name); + args.Handled = true; + return; + } } /// Bouncer's KillMe hook stops crash exploits from out of bounds values. From a2a13ce4949a6c89b628884ec0553da0dfdd4d27 Mon Sep 17 00:00:00 2001 From: tru321 <84877585+tru321@users.noreply.github.com> Date: Wed, 1 Dec 2021 13:44:41 +0800 Subject: [PATCH 5/9] Added DataHandler for NpcTalk This should stopping unregistered and logged out players to interact with NPC. Which prevent them smuggling or duplicating items via NPC items slot. --- TShockAPI/GetDataHandlers.cs | 57 ++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index c4366961..a668cdef 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -118,6 +118,7 @@ namespace TShockAPI { PacketTypes.PlaceChest, HandlePlaceChest }, { PacketTypes.Zones, HandlePlayerZone }, { PacketTypes.PasswordSend, HandlePassword }, + { PacketTypes.NpcTalk, HandleNpcTalk }, { PacketTypes.PlayerAnimation, HandlePlayerAnimation }, { PacketTypes.PlayerMana, HandlePlayerMana }, { PacketTypes.PlayerTeam, HandlePlayerTeam }, @@ -1066,6 +1067,39 @@ namespace TShockAPI return args.Handled; } + /// + /// Using when player trying to talk to a NPC + /// + public class NpcTalkEventArgs : GetDataHandledEventArgs + { + /// + /// Player Id + /// + public byte PlayerId { get; set; } + /// + /// NPC Id player's talk to + /// + public short NPCTalkTarget { get; set; } + } + public static HandlerList NpcTalk = new HandlerList(); + private static bool OnNpcTalk(TSPlayer player, MemoryStream data, byte _plr, short _npctarget) + { + if (NpcTalk == null) + { + return false; + } + + var args = new NpcTalkEventArgs + { + Player = player, + Data = data, + PlayerId = _plr, + NPCTalkTarget = _npctarget, + }; + NpcTalk.Invoke(null, args); + return args.Handled; + } + /// /// For use with a PlayerAnimation event /// @@ -3104,6 +3138,29 @@ namespace TShockAPI return true; } + private static bool HandleNpcTalk(GetDataHandlerArgs args) + { + var plr = args.Data.ReadInt8(); + var npc = args.Data.ReadInt16(); + + if (OnNpcTalk(args.Player, args.Data, plr, npc)) + return true; + //Rejecting player who trying to talk to a npc if player were disabled, mainly for unregistered and logged out players. Preventing smuggling or duplicating their items if player put it in a npc's item slot + if (args.Player.IsBeingDisabled()) + { + TShock.Log.ConsoleDebug("GetDataHandlers / HandleNpcTalk rejected npc talk {0}", args.Player.Name); + args.Player.SendData(PacketTypes.NpcTalk, "", plr, -1); + return true; + } + + if (args.Player.IsBouncerThrottled()) + { + TShock.Log.ConsoleDebug("Bouncer / HandleNpcTalk rejected from bouncer throttle from {0}", args.Player.Name); + return true; + } + return false; + } + private static bool HandlePlayerAnimation(GetDataHandlerArgs args) { if (OnPlayerAnimation(args.Player, args.Data)) From 0b1da29cf848b631b7993d35f333af7ddad57be1 Mon Sep 17 00:00:00 2001 From: tru321 <84877585+tru321@users.noreply.github.com> Date: Wed, 1 Dec 2021 14:35:02 +0800 Subject: [PATCH 6/9] Update GetDataHandlers.cs --- TShockAPI/GetDataHandlers.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index a668cdef..bf0fb54e 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -1073,11 +1073,12 @@ namespace TShockAPI public class NpcTalkEventArgs : GetDataHandledEventArgs { /// - /// Player Id + /// The Terraria ID of the player talking to the NPC /// public byte PlayerId { get; set; } + /// - /// NPC Id player's talk to + /// The NPC ID of the NPC the player is talking to /// public short NPCTalkTarget { get; set; } } @@ -3145,6 +3146,7 @@ namespace TShockAPI if (OnNpcTalk(args.Player, args.Data, plr, npc)) return true; + //Rejecting player who trying to talk to a npc if player were disabled, mainly for unregistered and logged out players. Preventing smuggling or duplicating their items if player put it in a npc's item slot if (args.Player.IsBeingDisabled()) { From 0d60d1654d4a253c25bfc8af7900fc1d4c7c86d7 Mon Sep 17 00:00:00 2001 From: tru321 <84877585+tru321@users.noreply.github.com> Date: Wed, 1 Dec 2021 14:51:53 +0800 Subject: [PATCH 7/9] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62a73f8c..15b0bf04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin ## TShock 4.5.12 * Fixed the ability to spawn Zenith projectile with non-original items. (@AgaSpace) +* Added hook `GetDataHandlers.OnNpcTalk` for NpcTalk and a handler for it that stopping unregistered and logged out players to interact with NPC, preventing them to smuggling or duplicating items via NPC item slots. (@tru321) ## TShock 4.5.11 * Add the new allowed buff TentacleSpike to NPC buff cheat detection bouncer. (@sgkoishi) From c6b870a480788e30780ac1210a8289730308d577 Mon Sep 17 00:00:00 2001 From: tru321 <84877585+tru321@users.noreply.github.com> Date: Wed, 1 Dec 2021 15:49:59 +0800 Subject: [PATCH 8/9] Update CHANGELOG.md Co-authored-by: Lucas Nicodemus --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15b0bf04..4412bdb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin ## TShock 4.5.12 * Fixed the ability to spawn Zenith projectile with non-original items. (@AgaSpace) -* Added hook `GetDataHandlers.OnNpcTalk` for NpcTalk and a handler for it that stopping unregistered and logged out players to interact with NPC, preventing them to smuggling or duplicating items via NPC item slots. (@tru321) +* Added hook `GetDataHandlers.OnNpcTalk` for NpcTalk and a handler for it that stops unregistered and logged out players from interacting with NPCs, preventing them from smuggling or duplicating items via NPC item slots. (@tru321) ## TShock 4.5.11 * Add the new allowed buff TentacleSpike to NPC buff cheat detection bouncer. (@sgkoishi) From 823293ed4399e42fe27c4f22c41f449a00ec1a0d Mon Sep 17 00:00:00 2001 From: AkjaHAsLk1IALk0MasH <46046453+AgaSpace@users.noreply.github.com> Date: Thu, 2 Dec 2021 10:03:36 +0700 Subject: [PATCH 9/9] Updated code comments --- TShockAPI/Bouncer.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index deca614e..1d1ccf26 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -2244,8 +2244,11 @@ namespace TShockAPI } /* - * A player must take damage from another player in any case - * I didn't add sourceProjectile because the player can't always die from a projectile. + * PlayerDeathReason does not initially contain any information, so all fields have values -1 or null. + * We can use this to determine the real cause of death. + * + * If the player was not specified, that is, the player index is -1, then it is definitely a custom cause, as you can only deal damage with a projectile or another player. + * This is how everything else works. If an NPC is specified, its value is not -1, which is a custom cause. */ if (TShock.Config.Settings.DisableCustomDeathMessages && (reason._sourcePlayerIndex == -1 || reason._sourceNPCIndex != -1 || reason._sourceOtherIndex != -1 || reason._sourceCustomReason != null))