From 7fe15feef6ef85f2d016fb970f401e61995c57b3 Mon Sep 17 00:00:00 2001 From: TheCrunchy Date: Mon, 10 Aug 2026 12:24:28 +0100 Subject: [PATCH 1/3] Update is powered checks --- Essentials/Conditions/ConditionsImplementations.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Essentials/Conditions/ConditionsImplementations.cs b/Essentials/Conditions/ConditionsImplementations.cs index 40d7703..e82f428 100644 --- a/Essentials/Conditions/ConditionsImplementations.cs +++ b/Essentials/Conditions/ConditionsImplementations.cs @@ -183,7 +183,7 @@ public static bool PoweredGridDistanceGreaterThan(MyCubeGrid grid, double dist) x => (VRageMath.Vector3.DistanceSquared(x.PositionComp.GetPosition(), grid.PositionComp.GetPosition()) < dist && (x.GetType() == typeof(MyCubeGrid)) - )).Cast().Where(y => !y.EntityId.Equals(grid.EntityId) && y.IsPowered) + )).Cast().Where(y => !y.EntityId.Equals(grid.EntityId) && y.IsPowerSwitchOn) .Count() == 0; } From 4e711e5701bbf70a7334909cf04aa01cbf7d2294 Mon Sep 17 00:00:00 2001 From: TheCrunchy Date: Mon, 10 Aug 2026 13:07:54 +0100 Subject: [PATCH 2/3] Clean up the MOTD code to not lock up threads when nexus redirects people or they disconnect before it completes --- Essentials/EssentialsPlugin.cs | 85 +++++++++++++++++++++++----------- 1 file changed, 59 insertions(+), 26 deletions(-) diff --git a/Essentials/EssentialsPlugin.cs b/Essentials/EssentialsPlugin.cs index 86860e1..c103f0d 100644 --- a/Essentials/EssentialsPlugin.cs +++ b/Essentials/EssentialsPlugin.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Reflection; @@ -141,6 +142,7 @@ private void SessionChanged(ITorchSession session, TorchSessionState state) mpMan.PlayerJoined += AccModule.GenerateAccount; mpMan.PlayerJoined += AccModule.CheckIp; mpMan.PlayerJoined += MotdOnce; + mpMan.PlayerLeft += KillMotdTasks; if (Config.EnableRanks) { RanksAndPermissions.GenerateRank(Config.DefaultRank); mpMan.PlayerJoined += RanksAndPermissions.RegisterInheritedRanks; @@ -268,34 +270,65 @@ private void ResetMotdOnce(IPlayer player) _motdOnce.Remove(player.SteamId); } + private readonly ConcurrentDictionary _motdTasks = new ConcurrentDictionary(); private void MotdOnce(IPlayer player) { - //TODO: REMOVE ALL THIS TRASH! - //implement a PlayerSpawned event in Torch. This will work for now. - Task.Run(() => - { - var start = DateTime.Now; - var timeout = TimeSpan.FromMinutes(5); - var pid = new MyPlayer.PlayerId(player.SteamId, 0); - while (DateTime.Now - start <= timeout) - { - if (!MySession.Static.Players.TryGetPlayerById(pid, out MyPlayer p) || p.Character == null) - { - Thread.Sleep(1000); - continue; - } - - Torch.Invoke(() => - { - if (_motdOnce.Contains(player.SteamId)) - return; - - SendMotd(p, true); - _motdOnce.Add(player.SteamId); - }); - break; - } - }); + var cts = new CancellationTokenSource(); + + if (!_motdTasks.TryAdd(player.SteamId, cts)) + { + cts.Dispose(); + return; + } + + Task.Run(async () => + { + var start = DateTime.UtcNow; + var timeout = TimeSpan.FromMinutes(5); + var pid = new MyPlayer.PlayerId(player.SteamId, 0); + + try + { + while (DateTime.UtcNow - start <= timeout) + { + cts.Token.ThrowIfCancellationRequested(); + if (!MySession.Static.Players.TryGetPlayerById(pid, out MyPlayer p) || + p.Character == null) + { + await Task.Delay(1000, cts.Token); + continue; + } + + await Torch.InvokeAsync(() => + { + if (_motdOnce.Contains(player.SteamId)) + return; + + SendMotd(p, true); + _motdOnce.Add(player.SteamId); + }); + + break; + } + } + catch (OperationCanceledException) + { + // Player disconnected. + } + finally + { + _motdTasks.TryRemove(player.SteamId, out _); + cts.Dispose(); + } + }, cts.Token); + } + + private void KillMotdTasks(IPlayer player) + { + if (_motdTasks.TryGetValue(player.SteamId, out var cts)) + { + cts.Cancel(); + } } public void SendMotd(MyPlayer player, bool onSessionChanged) From 6e059e846bbcc1d8b7b075ef3488fff3ec1fca1c Mon Sep 17 00:00:00 2001 From: TheCrunchy Date: Mon, 10 Aug 2026 13:26:09 +0100 Subject: [PATCH 3/3] swap to try remove not try get --- Essentials/EssentialsPlugin.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Essentials/EssentialsPlugin.cs b/Essentials/EssentialsPlugin.cs index c103f0d..e072df4 100644 --- a/Essentials/EssentialsPlugin.cs +++ b/Essentials/EssentialsPlugin.cs @@ -325,7 +325,7 @@ await Torch.InvokeAsync(() => private void KillMotdTasks(IPlayer player) { - if (_motdTasks.TryGetValue(player.SteamId, out var cts)) + if (_motdTasks.TryRemove(player.SteamId, out var cts)) { cts.Cancel(); }