diff --git a/CHANGELOG.md b/CHANGELOG.md index c24fd45d..823bb7de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Adding EmojiHandler to handle an exploit. Adding `tshock.sendemoji` permission and checks. Added this permission to guest group by default. * Handling SyncCavernMonsterType packet to prevent an exploit where players could modify the server's cavern monster types and make the server spawn any NPCs - including bosses - onto other players. * Added LandGolfBallInCup event which is accessible for developers to work with, as well as LandGolfBallInCup handler to handle exploits where players could send direct packets to trigger and imitate golf ball cup landing anywhere in the game world. Added two public lists in Handlers.LandGolfBallInCupHandler: GolfBallProjectileIDs and GolfClubItemIDs. (@Patrikkk) +* Add SyncTilePicking event. This is called when a player damages a tile. Implementing SyncTilePickingHandler and patching tile damaging related exploits. (Preventing player sending invalid world position data which disconnects other players.) ## TShock 4.4.0 (Pre-release 10) * Fix all rope coils. (@Olink) diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index 8029031b..6f2a2ae4 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -40,6 +40,7 @@ namespace TShockAPI internal Handlers.NetModules.NetModulePacketHandler NetModuleHandler { get; set; } internal Handlers.EmojiHandler EmojiHandler { get; set; } internal Handlers.LandGolfBallInCupHandler LandGolfBallInCupHandler { get; set; } + internal Handlers.SyncTilePickingHandler SyncTilePickingHandler { get; set; } /// Constructor call initializes Bouncer and related functionality. /// A new Bouncer. @@ -57,6 +58,9 @@ namespace TShockAPI LandGolfBallInCupHandler = new Handlers.LandGolfBallInCupHandler(); GetDataHandlers.LandGolfBallInCup += LandGolfBallInCupHandler.OnReceive; + SyncTilePickingHandler = new Handlers.SyncTilePickingHandler(); + GetDataHandlers.SyncTilePicking += SyncTilePickingHandler.OnReceive; + // Setup hooks GetDataHandlers.GetSection += OnGetSection; GetDataHandlers.PlayerUpdate += OnPlayerUpdate; diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index cf13e396..496a7053 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.SyncTilePicking, HandleSyncTilePicking }, { PacketTypes.SyncRevengeMarker, HandleSyncRevengeMarker }, { PacketTypes.LandGolfBallInCup, HandleLandGolfBallInCup }, { PacketTypes.FishOutNPC, HandleFishOutNPC }, @@ -1870,6 +1871,48 @@ namespace TShockAPI } /// + /// For use in a SyncTilePicking event. + /// + public class SyncTilePickingEventArgs : GetDataHandledEventArgs + { + /// + /// The player index in the packet, who sends the tile picking data. + /// + public byte PlayerIndex { get; set; } + /// + /// The X world position of the tile that is being picked. + /// + public short TileX { get; set; } + /// + /// The Y world position of the tile that is being picked. + /// + public short TileY { get; set; } + /// + /// The damage that is being dealt on the tile. + /// + public byte TileDamage { get; set; } + } + /// + /// Called when a player hits and damages a tile. + /// + public static HandlerList SyncTilePicking = new HandlerList(); + private static bool OnSyncTilePicking(TSPlayer player, MemoryStream data, byte playerIndex, short tileX, short tileY, byte tileDamage) + { + if (SyncTilePicking == null) + return false; + + var args = new SyncTilePickingEventArgs + { + PlayerIndex = playerIndex, + TileX = tileX, + TileY = tileY, + TileDamage = tileDamage + }; + SyncTilePicking.Invoke(null, args); + return args.Handled; + } + + /// For use in an Emoji event. /// public class EmojiEventArgs : GetDataHandledEventArgs @@ -3653,7 +3696,20 @@ namespace TShockAPI return false; } - + + private static bool HandleSyncTilePicking(GetDataHandlerArgs args) + { + byte playerIndex = args.Data.ReadInt8(); + short tileX = args.Data.ReadInt16(); + short tileY = args.Data.ReadInt16(); + byte damage = args.Data.ReadInt8(); + + if (OnSyncTilePicking(args.Player, args.Data, playerIndex, tileX, tileY, damage)) + return true; + + return false; + } + private static bool HandleSyncRevengeMarker(GetDataHandlerArgs args) { int uniqueID = args.Data.ReadInt32(); diff --git a/TShockAPI/Handlers/SyncTilePickingHandler.cs b/TShockAPI/Handlers/SyncTilePickingHandler.cs new file mode 100644 index 00000000..d7ad1e13 --- /dev/null +++ b/TShockAPI/Handlers/SyncTilePickingHandler.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Terraria; +using static TShockAPI.GetDataHandlers; + +namespace TShockAPI.Handlers +{ + class SyncTilePickingHandler : IPacketHandler + { + /// + /// Invoked when player damages a tile. Rejects the packet if its out of world bounds. + /// + /// + /// + public void OnReceive(object sender, SyncTilePickingEventArgs args) + { + if (args.TileX > Main.maxTilesX || args.TileX < 0 + || args.TileY > Main.maxTilesY || args.TileY < 0) + { + TShock.Log.ConsoleDebug($"SyncTilePickingHandler: X and Y position is out of world bounds! - From {args.Player.Name}"); + args.Handled = true; + return; + } + } + } +} diff --git a/TShockAPI/TShockAPI.csproj b/TShockAPI/TShockAPI.csproj index 495127c8..f9885393 100644 --- a/TShockAPI/TShockAPI.csproj +++ b/TShockAPI/TShockAPI.csproj @@ -100,6 +100,7 @@ +