From 965361c4066203e2e59c9b49a92f7d728864afaf Mon Sep 17 00:00:00 2001 From: Olink Date: Sun, 24 May 2020 03:48:08 -0400 Subject: [PATCH 1/9] Add a projectile tracker, so that we can allow fluid bombs. --- TShockAPI/Bouncer.cs | 38 +++++++++++++++++++++++++++++------- TShockAPI/GetDataHandlers.cs | 18 +++++++++++++++++ TShockAPI/TSPlayer.cs | 6 ++++++ TShockAPI/TShock.cs | 13 ++++++++++++ 4 files changed, 68 insertions(+), 7 deletions(-) diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index dd55f6ef..f3a85f40 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -962,6 +962,18 @@ namespace TShockAPI // Denotes that the player has recently set a fuse - used for cheat detection. args.Player.RecentFuse = 10; } + + if (projectileCreatesLiquid.ContainsKey(type)) + { + lock (args.Player.RecentlyCreatedProjectiles) + { + args.Player.RecentlyCreatedProjectiles.Add(new ProjectileStruct() + { + Index = ident, + CreatedAt = DateTime.Now + }); + } + } } /// Handles the NPC Strike event for Bouncer. @@ -1290,6 +1302,18 @@ namespace TShockAPI args.Player.TileLiquidThreshold++; } + bool wasThereABombNearby = false; + + lock (args.Player.RecentlyCreatedProjectiles) + { + var keys = projectileCreatesLiquid.Where(k => k.Value == type).Select(k => k.Key); + var recentBombs = args.Player.RecentlyCreatedProjectiles.Where(keys.Contains(Main.projectile[p.Index].type)); + wasThereABombNearby = recentBombs.Any(r => (args.TileX > (Main.projectile[r.Index].position.X / 16.0f) - 32 + && args.TileX < (Main.projectile[r.Index].position.X / 16.0f) + 32) + && (args.TileY > (Main.projectile[r.Index].position.Y / 16.0f) - 32 + && args.TileY < (Main.projectile[r.Index].position.Y / 16.0f) + 32)); + } + // Liquid anti-cheat // Arguably the banned buckets bit should be in the item bans system if (amount != 0) @@ -1326,7 +1350,7 @@ namespace TShockAPI bucket = 6; } - if (type == LiquidType.Lava && !(bucket == 2 || bucket == 0 || bucket == 5 || bucket == 6)) + if (!wasThereABombNearby && type == LiquidType.Lava && !(bucket == 2 || bucket == 0 || bucket == 5 || bucket == 6)) { TShock.Log.ConsoleDebug("Bouncer / OnLiquidSet rejected bucket check 1 from {0}", args.Player.Name); args.Player.SendErrorMessage("You do not have permission to perform this action."); @@ -1336,7 +1360,7 @@ namespace TShockAPI return; } - if (type == LiquidType.Lava && TShock.Itembans.ItemIsBanned("Lava Bucket", args.Player)) + if (!wasThereABombNearby && type == LiquidType.Lava && TShock.Itembans.ItemIsBanned("Lava Bucket", args.Player)) { TShock.Log.ConsoleDebug("Bouncer / OnLiquidSet rejected lava bucket from {0}", args.Player.Name); args.Player.SendErrorMessage("You do not have permission to perform this action."); @@ -1346,7 +1370,7 @@ namespace TShockAPI return; } - if (type == LiquidType.Water && !(bucket == 1 || bucket == 0 || bucket == 4)) + if (!wasThereABombNearby && type == LiquidType.Water && !(bucket == 1 || bucket == 0 || bucket == 4)) { TShock.Log.ConsoleDebug("Bouncer / OnLiquidSet rejected bucket check 2 from {0}", args.Player.Name); args.Player.SendErrorMessage("You do not have permission to perform this action."); @@ -1356,7 +1380,7 @@ namespace TShockAPI return; } - if (type == LiquidType.Water && TShock.Itembans.ItemIsBanned("Water Bucket", args.Player)) + if (!wasThereABombNearby && type == LiquidType.Water && TShock.Itembans.ItemIsBanned("Water Bucket", args.Player)) { TShock.Log.ConsoleDebug("Bouncer / OnLiquidSet rejected bucket check 3 from {0}", args.Player.Name); args.Player.SendErrorMessage("You do not have permission to perform this action."); @@ -1366,7 +1390,7 @@ namespace TShockAPI return; } - if (type == LiquidType.Honey && !(bucket == 3 || bucket == 0)) + if (!wasThereABombNearby && type == LiquidType.Honey && !(bucket == 3 || bucket == 0)) { TShock.Log.ConsoleDebug("Bouncer / OnLiquidSet rejected bucket check 4 from {0}", args.Player.Name); args.Player.SendErrorMessage("You do not have permission to perform this action."); @@ -1376,7 +1400,7 @@ namespace TShockAPI return; } - if (type == LiquidType.Honey && TShock.Itembans.ItemIsBanned("Honey Bucket", args.Player)) + if (!wasThereABombNearby && type == LiquidType.Honey && TShock.Itembans.ItemIsBanned("Honey Bucket", args.Player)) { TShock.Log.ConsoleDebug("Bouncer / OnLiquidSet rejected bucket check 5 from {0}", args.Player.Name); args.Player.SendErrorMessage("You do not have permission to perform this action."); @@ -1395,7 +1419,7 @@ namespace TShockAPI return; } - if (!args.Player.IsInRange(tileX, tileY, 16)) + if (!wasThereABombNearby && !args.Player.IsInRange(tileX, tileY, 16)) { TShock.Log.ConsoleDebug("Bouncer / OnLiquidSet rejected range checks from {0}", args.Player.Name); args.Player.SendTileSquare(tileX, tileY, 1); diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index 19c6f4b0..57c8985f 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -2386,6 +2386,10 @@ namespace TShockAPI } args.Player.LastKilledProjectile = type; + lock (args.Player.RecentlyCreatedProjectiles) + { + args.Player.RecentlyCreatedProjectiles.ForEach(s => { if (s.Index == index) { s.Killed = true; } }); + } return false; } @@ -3446,6 +3450,13 @@ namespace TShockAPI { ProjectileID.MysticSnakeCoil, TileID.MysticSnakeRope } }; + internal static Dictionary projectileCreatesLiquid = new Dictionary + { + {ProjectileID.LavaBomb, LiquidType.Lava}, + {ProjectileID.WetBomb, LiquidType.Water}, + {ProjectileID.HoneyBomb, LiquidType.Honey} + }; + internal static Dictionary ropeCoilPlacements = new Dictionary { {ItemID.RopeCoil, TileID.Rope}, @@ -3461,5 +3472,12 @@ namespace TShockAPI { {TileID.MinecartTrack, 3} }; + + internal struct ProjectileStruct + { + public int Index { get; set; } + public DateTime CreatedAt { get; set; } + public bool Killed { get; internal set; } + } } } diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index 86642e69..277e9b00 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -764,6 +764,12 @@ namespace TShockAPI /// public int LastKilledProjectile = 0; + /// + /// Keeps track of recently created projectiles by this player. TShock.cs OnSecondUpdate() removes from this in an async task. + /// Projectiles older than 5 seconds are purged from this collection as they are no longer "recent". + /// + internal List RecentlyCreatedProjectiles = new List(); + /// /// The current region this player is in, or null if none. /// diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index d0b174b4..656a1314 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -44,6 +44,7 @@ using Microsoft.Xna.Framework; using TShockAPI.Sockets; using TShockAPI.CLI; using TShockAPI.Localization; +using System.Threading.Tasks; namespace TShockAPI { @@ -1064,6 +1065,18 @@ namespace TShockAPI } } } + + Task.Run(() => + { + if (player != null && player.TPlayer.whoAmI >= 0) + { + var threshold = DateTime.Now.AddSeconds(-5); + lock (player.RecentlyCreatedProjectiles) + { + player.RecentlyCreatedProjectiles = player.RecentlyCreatedProjectiles.Where(s => s.CreatedAt > threshold).ToList(); + } + } + }); } Utils.SetConsoleTitle(false); } From fecdb755ff94c8812c091e1153cd1a79e8401921 Mon Sep 17 00:00:00 2001 From: Olink Date: Sun, 24 May 2020 03:50:01 -0400 Subject: [PATCH 2/9] Add entry to changelog. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23493249..c0074b8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Update for OTAPI 2.0.0.36 and Terraria 1.4.0.4. (@hakusaro, @Patrikkk, @DeathCradle) * Fixed /wind command. (@AxeelAnder) * Fixed NPC buff bouncer. (@AxeelAnder) +* Fix fluid bombs. (@Olink) ## TShock 4.4.0 (Pre-release 7 (Entangled)) * Fixed bed spawn issues when trying to remove spawn point in SSC. (@Olink) From 3b0c5f777551caf54be2ba6b3b19dae06489d1fd Mon Sep 17 00:00:00 2001 From: Olink Date: Sun, 24 May 2020 03:54:27 -0400 Subject: [PATCH 3/9] Make bouncer compile. Reduce range checks. --- TShockAPI/Bouncer.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index f3a85f40..afa5d6a3 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -1307,11 +1307,11 @@ namespace TShockAPI lock (args.Player.RecentlyCreatedProjectiles) { var keys = projectileCreatesLiquid.Where(k => k.Value == type).Select(k => k.Key); - var recentBombs = args.Player.RecentlyCreatedProjectiles.Where(keys.Contains(Main.projectile[p.Index].type)); - wasThereABombNearby = recentBombs.Any(r => (args.TileX > (Main.projectile[r.Index].position.X / 16.0f) - 32 - && args.TileX < (Main.projectile[r.Index].position.X / 16.0f) + 32) - && (args.TileY > (Main.projectile[r.Index].position.Y / 16.0f) - 32 - && args.TileY < (Main.projectile[r.Index].position.Y / 16.0f) + 32)); + var recentBombs = args.Player.RecentlyCreatedProjectiles.Where(p => keys.Contains(Main.projectile[p.Index].type)); + wasThereABombNearby = recentBombs.Any(r => (args.TileX > (Main.projectile[r.Index].position.X / 16.0f) - 16 + && args.TileX < (Main.projectile[r.Index].position.X / 16.0f) + 16) + && (args.TileY > (Main.projectile[r.Index].position.Y / 16.0f) - 16 + && args.TileY < (Main.projectile[r.Index].position.Y / 16.0f) + 16)); } // Liquid anti-cheat From 9209ac0b73c90d8d64aaf8d4e6e7fcfe2eb9243c Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 24 May 2020 04:19:00 -0400 Subject: [PATCH 4/9] Apply suggestions from code review Co-authored-by: Lucas Nicodemus --- CHANGELOG.md | 2 +- TShockAPI/Bouncer.cs | 2 +- TShockAPI/TSPlayer.cs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0074b8c..1505337c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Update for OTAPI 2.0.0.36 and Terraria 1.4.0.4. (@hakusaro, @Patrikkk, @DeathCradle) * Fixed /wind command. (@AxeelAnder) * Fixed NPC buff bouncer. (@AxeelAnder) -* Fix fluid bombs. (@Olink) +* Fixed lava, wet, and honey bombs. (@Olink) ## TShock 4.4.0 (Pre-release 7 (Entangled)) * Fixed bed spawn issues when trying to remove spawn point in SSC. (@Olink) diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index afa5d6a3..a506d079 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -1303,7 +1303,7 @@ namespace TShockAPI } bool wasThereABombNearby = false; - +// The +/- 16 is compensation for maximum detonation distance, but it might be possible to drop to 5 and be conservative lock (args.Player.RecentlyCreatedProjectiles) { var keys = projectileCreatesLiquid.Where(k => k.Value == type).Select(k => k.Key); diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index 277e9b00..3d7deaae 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -765,8 +765,8 @@ namespace TShockAPI public int LastKilledProjectile = 0; /// - /// Keeps track of recently created projectiles by this player. TShock.cs OnSecondUpdate() removes from this in an async task. - /// Projectiles older than 5 seconds are purged from this collection as they are no longer "recent". + /// Keeps track of recently created projectiles by this player. TShock.cs OnSecondUpdate() removes from this in an async task. + /// Projectiles older than 5 seconds are purged from this collection as they are no longer "recent." /// internal List RecentlyCreatedProjectiles = new List(); From b73088306d7aad990d2c9c088f8da4c71376f7d9 Mon Sep 17 00:00:00 2001 From: Olink Date: Sun, 24 May 2020 04:27:12 -0400 Subject: [PATCH 5/9] Move the projectile refresh to Bouncer. Bouncer now has an OnSecondUpdate(). Move projectile created tracking to GetDataHandler from Bouncer. --- TShockAPI/Bouncer.cs | 33 +++++++++++++++++++-------------- TShockAPI/GetDataHandlers.cs | 12 ++++++++++++ TShockAPI/TShock.cs | 15 ++------------- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index a506d079..a5e14115 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -19,18 +19,17 @@ using System; using System.Collections.Generic; using System.Linq; using Terraria.ID; -using TShockAPI.DB; using TShockAPI.Net; using Terraria; using Microsoft.Xna.Framework; using OTAPI.Tile; using TShockAPI.Localization; using static TShockAPI.GetDataHandlers; -using TerrariaApi.Server; using Terraria.ObjectData; using Terraria.DataStructures; using Terraria.Localization; using TShockAPI.Models.PlayerUpdate; +using System.Threading.Tasks; namespace TShockAPI { @@ -962,18 +961,6 @@ namespace TShockAPI // Denotes that the player has recently set a fuse - used for cheat detection. args.Player.RecentFuse = 10; } - - if (projectileCreatesLiquid.ContainsKey(type)) - { - lock (args.Player.RecentlyCreatedProjectiles) - { - args.Player.RecentlyCreatedProjectiles.Add(new ProjectileStruct() - { - Index = ident, - CreatedAt = DateTime.Now - }); - } - } } /// Handles the NPC Strike event for Bouncer. @@ -2069,6 +2056,24 @@ namespace TShockAPI } } + internal void OnSecondUpdate() + { + Task.Run(() => + { + foreach (var player in TShock.Players) + { + if (player != null && player.TPlayer.whoAmI >= 0) + { + var threshold = DateTime.Now.AddSeconds(-5); + lock (player.RecentlyCreatedProjectiles) + { + player.RecentlyCreatedProjectiles = player.RecentlyCreatedProjectiles.Where(s => s.CreatedAt > threshold).ToList(); + } + } + } + }); + } + // These time values are references from Projectile.cs, at npc.AddBuff() calls. private static Dictionary NPCAddBuffTimeMax = new Dictionary() { diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index 57c8985f..606f56a6 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -2322,6 +2322,18 @@ namespace TShockAPI if (OnNewProjectile(args.Data, ident, pos, vel, knockback, dmg, owner, type, index, args.Player)) return true; + if (projectileCreatesLiquid.ContainsKey(type)) + { + lock (args.Player.RecentlyCreatedProjectiles) + { + args.Player.RecentlyCreatedProjectiles.Add(new ProjectileStruct() + { + Index = ident, + CreatedAt = DateTime.Now + }); + } + } + return false; } diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 656a1314..6f685654 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -44,7 +44,6 @@ using Microsoft.Xna.Framework; using TShockAPI.Sockets; using TShockAPI.CLI; using TShockAPI.Localization; -using System.Threading.Tasks; namespace TShockAPI { @@ -1065,19 +1064,9 @@ namespace TShockAPI } } } - - Task.Run(() => - { - if (player != null && player.TPlayer.whoAmI >= 0) - { - var threshold = DateTime.Now.AddSeconds(-5); - lock (player.RecentlyCreatedProjectiles) - { - player.RecentlyCreatedProjectiles = player.RecentlyCreatedProjectiles.Where(s => s.CreatedAt > threshold).ToList(); - } - } - }); } + + Bouncer.OnSecondUpdate(); Utils.SetConsoleTitle(false); } From 8edca72919d0c705e2965a9d0379c4a6932da1ba Mon Sep 17 00:00:00 2001 From: Olink Date: Sun, 24 May 2020 04:33:03 -0400 Subject: [PATCH 6/9] Add config option for the bomb radius. --- TShockAPI/Bouncer.cs | 8 ++++---- TShockAPI/ConfigFile.cs | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index a5e14115..7dde6776 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -1295,10 +1295,10 @@ namespace TShockAPI { var keys = projectileCreatesLiquid.Where(k => k.Value == type).Select(k => k.Key); var recentBombs = args.Player.RecentlyCreatedProjectiles.Where(p => keys.Contains(Main.projectile[p.Index].type)); - wasThereABombNearby = recentBombs.Any(r => (args.TileX > (Main.projectile[r.Index].position.X / 16.0f) - 16 - && args.TileX < (Main.projectile[r.Index].position.X / 16.0f) + 16) - && (args.TileY > (Main.projectile[r.Index].position.Y / 16.0f) - 16 - && args.TileY < (Main.projectile[r.Index].position.Y / 16.0f) + 16)); + wasThereABombNearby = recentBombs.Any(r => (args.TileX > (Main.projectile[r.Index].position.X / 16.0f) - TShock.Config.BombExplosionRadius + && args.TileX < (Main.projectile[r.Index].position.X / 16.0f) + TShock.Config.BombExplosionRadius) + && (args.TileY > (Main.projectile[r.Index].position.Y / 16.0f) - TShock.Config.BombExplosionRadius + && args.TileY < (Main.projectile[r.Index].position.Y / 16.0f) + TShock.Config.BombExplosionRadius)); } // Liquid anti-cheat diff --git a/TShockAPI/ConfigFile.cs b/TShockAPI/ConfigFile.cs index fb920732..6fd5794c 100644 --- a/TShockAPI/ConfigFile.cs +++ b/TShockAPI/ConfigFile.cs @@ -527,6 +527,9 @@ namespace TShockAPI [Description("Whether or not the server should output debug level messages related to system operation.")] public bool DebugLogs = false; + [Description("Determines the range in tiles that a bomb can affect tiles.")] + public int BombExplosionRadius = 5; + /// /// Reads a configuration file from a given path /// From 3105a977a406d890411724a9c1c79694d4a4f449 Mon Sep 17 00:00:00 2001 From: Olink Date: Sun, 24 May 2020 04:37:15 -0400 Subject: [PATCH 7/9] Add fluid rockets. --- TShockAPI/GetDataHandlers.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index 606f56a6..7d76a0d4 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -3465,8 +3465,11 @@ namespace TShockAPI internal static Dictionary projectileCreatesLiquid = new Dictionary { {ProjectileID.LavaBomb, LiquidType.Lava}, + {ProjectileID.LavaRocket, LiquidType.Lava }, {ProjectileID.WetBomb, LiquidType.Water}, - {ProjectileID.HoneyBomb, LiquidType.Honey} + {ProjectileID.WetRocket, LiquidType.Water }, + {ProjectileID.HoneyBomb, LiquidType.Honey}, + {ProjectileID.HoneyRocket, LiquidType.Honey } }; internal static Dictionary ropeCoilPlacements = new Dictionary From a0fd1d04a9f076c69869ef6fc0a3cf20a837c251 Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 24 May 2020 04:38:53 -0400 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 1505337c..05ce14d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Update for OTAPI 2.0.0.36 and Terraria 1.4.0.4. (@hakusaro, @Patrikkk, @DeathCradle) * Fixed /wind command. (@AxeelAnder) * Fixed NPC buff bouncer. (@AxeelAnder) -* Fixed lava, wet, and honey bombs. (@Olink) +* Fixed lava, wet, honey bombs; and lava, wet, and honey rockets. (@Olink) ## TShock 4.4.0 (Pre-release 7 (Entangled)) * Fixed bed spawn issues when trying to remove spawn point in SSC. (@Olink) From f1b35e47cc440d2131fd23b09b8044e68e6a4033 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Sun, 24 May 2020 13:23:18 -0700 Subject: [PATCH 9/9] Update config description for bomb range --- TShockAPI/ConfigFile.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TShockAPI/ConfigFile.cs b/TShockAPI/ConfigFile.cs index 6fd5794c..4806cfc5 100644 --- a/TShockAPI/ConfigFile.cs +++ b/TShockAPI/ConfigFile.cs @@ -527,7 +527,7 @@ namespace TShockAPI [Description("Whether or not the server should output debug level messages related to system operation.")] public bool DebugLogs = false; - [Description("Determines the range in tiles that a bomb can affect tiles.")] + [Description("Determines the range in tiles that a bomb can affect tiles from detonation point.")] public int BombExplosionRadius = 5; ///