Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Essentials/Conditions/ConditionsImplementations.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -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<MyCubeGrid>().Where(y => !y.EntityId.Equals(grid.EntityId) && y.IsPowered)
)).Cast<MyCubeGrid>().Where(y => !y.EntityId.Equals(grid.EntityId) && y.IsPowerSwitchOn)
.Count() == 0;
}

Expand Down
85 changes: 59 additions & 26 deletions Essentials/EssentialsPlugin.cs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
Expand DownExpand Up@@ -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;
Expand DownExpand Up@@ -268,34 +270,65 @@ private void ResetMotdOnce(IPlayer player)
_motdOnce.Remove(player.SteamId);
}

private readonly ConcurrentDictionary<ulong, CancellationTokenSource> _motdTasks = new ConcurrentDictionary<ulong, CancellationTokenSource>();
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.TryRemove(player.SteamId, out var cts))
{
cts.Cancel();
}
}

public void SendMotd(MyPlayer player, bool onSessionChanged)
Expand Down