From 2177d75066bd47c200547126d27b3c54fdb81c62 Mon Sep 17 00:00:00 2001 From: Patrikkk Date: Mon, 1 Jun 2020 18:31:30 +0200 Subject: [PATCH] Add SyncTilePicking event. --- CHANGELOG.md | 2 +- TShockAPI/GetDataHandlers.cs | 93 +++++++++++++++++++++++++++++------- 2 files changed, 77 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 121cb25a..8dc088af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when adding new entries; sign your name off when you add or change something. This should primarily be things like user changes, not necessarily codebase changes unless it's really relevant or large. ## Upcoming Changes -* Your change goes here! +* Add SyncTilePicking event. This is called when a player damages a tile. ## TShock 4.4.0 (Pre-release 10) * Fix all rope coils. (@Olink) diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index b5c9bbb0..64f57b9c 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -151,9 +151,10 @@ namespace TShockAPI { PacketTypes.CrystalInvasionStart, HandleOldOnesArmy }, { PacketTypes.PlayerHurtV2, HandlePlayerDamageV2 }, { PacketTypes.PlayerDeathV2, HandlePlayerKillMeV2 }, + { PacketTypes.SyncTilePicking, HandleSyncTilePicking }, + { PacketTypes.SyncRevengeMarker, HandleSyncRevengeMarker }, { PacketTypes.FishOutNPC, HandleFishOutNPC }, - { PacketTypes.FoodPlatterTryPlacing, HandleFoodPlatterTryPlacing }, - { PacketTypes.SyncRevengeMarker, HandleSyncRevengeMarker } + { PacketTypes.FoodPlatterTryPlacing, HandleFoodPlatterTryPlacing } }; } @@ -1866,6 +1867,51 @@ namespace TShockAPI return args.Handled; } + /// + /// 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 + { + Player = player, + Data = data, + PlayerIndex = playerIndex, + TileX = tileX, + TileY = tileY, + TileDamage = tileDamage + }; + SyncTilePicking.Invoke(null, args); + return args.Handled; + } + + /// /// For use in a FishOutNPC event. /// @@ -3671,6 +3717,34 @@ 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(); + Vector2 location = args.Data.ReadVector2(); + int netId = args.Data.ReadInt32(); + float npcHpPercent = args.Data.ReadSingle(); + int npcTypeAgainstDiscouragement = args.Data.ReadInt32(); //tfw the argument is Type Against Discouragement + int npcAiStyleAgainstDiscouragement = args.Data.ReadInt32(); //see ^ + int coinsValue = args.Data.ReadInt32(); + float baseValue = args.Data.ReadSingle(); + bool spawnedFromStatus = args.Data.ReadBoolean(); + + return false; + } + private static bool HandleFishOutNPC(GetDataHandlerArgs args) { ushort tileX = args.Data.ReadUInt16(); @@ -3697,21 +3771,6 @@ namespace TShockAPI return false; } - private static bool HandleSyncRevengeMarker(GetDataHandlerArgs args) - { - int uniqueID = args.Data.ReadInt32(); - Vector2 location = args.Data.ReadVector2(); - int netId = args.Data.ReadInt32(); - float npcHpPercent = args.Data.ReadSingle(); - int npcTypeAgainstDiscouragement = args.Data.ReadInt32(); //tfw the argument is Type Against Discouragement - int npcAiStyleAgainstDiscouragement = args.Data.ReadInt32(); //see ^ - int coinsValue = args.Data.ReadInt32(); - float baseValue = args.Data.ReadSingle(); - bool spawnedFromStatus = args.Data.ReadBoolean(); - - return false; - } - public enum EditAction { KillTile = 0,