From 845fc3b7c80d7df870dd241681abfce2ea9a41c9 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Fri, 15 Dec 2017 12:12:21 -0700 Subject: [PATCH] Move OnChestItemChange to Bouncer Fix issue where TShock didn't properly do anti-cheat on chest item changes. --- CHANGELOG.md | 2 ++ TShockAPI/Bouncer.cs | 35 +++++++++++++++++++++++++++++++++++ TShockAPI/GetDataHandlers.cs | 30 ++++++------------------------ 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4673edc0..b627f6c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -70,6 +70,8 @@ Putting this stuff down here so things don't conflict as often. * Fixed invasions started by TShock not reporting size correctly and probably not working at all. (@hakusaro) * Removed `GetDataHandlers.TileKill` and replaced it with `GetDataHandlers.PlaceChest` as the packet originally designated as tile kill is now only used for chests. (@hakusaro) * Added `TSPlayer` to `GetDataHandlers.NPCHome`. (@hakusaro) +* Added a `TSPlayer` to `GetDataHandlers.ChestItemChanged`. (@hakusaro) +* Fixed chest item changes not triggering any range checks, tile checks, or correct chest checks. (@hakusaro) ## TShock 4.3.25 * Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6. diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index 8bb255a0..1aa6b22e 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -42,6 +42,7 @@ namespace TShockAPI { // Setup hooks + GetDataHandlers.ChestItemChange.Register(OnChestItemChange); GetDataHandlers.NPCHome.Register(OnUpdateNPCHome); GetDataHandlers.ChestOpen.Register(OnChestOpen); GetDataHandlers.PlaceChest.Register(OnPlaceChest); @@ -56,6 +57,40 @@ namespace TShockAPI GetDataHandlers.TileEdit.Register(OnTileEdit); } + internal void OnChestItemChange(object sender, GetDataHandlers.ChestItemEventArgs args) + { + short id = args.ID; + byte slot = args.Slot; + short stacks = args.Stacks; + byte prefix = args.Prefix; + short type = args.Type; + + if (args.Player.TPlayer.chest != id) + { + args.Handled = true; + return; + } + + if (TShock.CheckIgnores(args.Player)) + { + args.Player.SendData(PacketTypes.ChestItem, "", id, slot); + args.Handled = true; + return; + } + + if (TShock.CheckTilePermission(args.Player, Main.chest[id].x, Main.chest[id].y) && TShock.Config.RegionProtectChests) + { + args.Handled = true; + return; + } + + if (TShock.CheckRangePermission(args.Player, Main.chest[id].x, Main.chest[id].y)) + { + args.Handled = true; + return; + } + } + /// The Bouncer handler for when an NPC is rehomed. /// The object that triggered the event. /// The packet arguments that the event has. diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index dead02ec..8431a27d 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -922,6 +922,8 @@ namespace TShockAPI /// public class ChestItemEventArgs : HandledEventArgs { + /// The TSPlayer that triggered the event. + public TSPlayer Player { get; set; } /// /// ChestID /// @@ -948,13 +950,14 @@ namespace TShockAPI /// public static HandlerList ChestItemChange; - private static bool OnChestItemChange(short id, byte slot, short stacks, byte prefix, short type) + private static bool OnChestItemChange(TSPlayer player, short id, byte slot, short stacks, byte prefix, short type) { if (ChestItemChange == null) return false; var args = new ChestItemEventArgs { + Player = player, ID = id, Slot = slot, Stacks = stacks, @@ -2452,35 +2455,14 @@ namespace TShockAPI var prefix = args.Data.ReadInt8(); var type = args.Data.ReadInt16(); - if (OnChestItemChange(id, slot, stacks, prefix, type)) + if (OnChestItemChange(args.Player, id, slot, stacks, prefix, type)) return true; - if (args.TPlayer.chest != id) - { - return false; - } - - if (TShock.CheckIgnores(args.Player)) - { - args.Player.SendData(PacketTypes.ChestItem, "", id, slot); - return true; - } - Item item = new Item(); item.netDefaults(type); if (stacks > item.maxStack || TShock.Itembans.ItemIsBanned(EnglishLanguage.GetItemNameById(item.type), args.Player)) { - return false; - } - - if (TShock.CheckTilePermission(args.Player, Main.chest[id].x, Main.chest[id].y) && TShock.Config.RegionProtectChests) - { - return false; - } - - if (TShock.CheckRangePermission(args.Player, Main.chest[id].x, Main.chest[id].y)) - { - return false; + return true; } return false;