Skip to content

Commit ece8376

Browse files
committed
Delay sending MOTD until the player spawns with a character.
1 parent 111442e commit ece8376

2 files changed

Lines changed: 35 additions & 28 deletions

File tree

‎Essentials/Commands/PlayerModule.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public void Unban(string nameOrSteamId)
159159
[Permission(MyPromoteLevel.None)]
160160
public void Motd()
161161
{
162-
Plugin.SendMotd(Context.Player.IdentityId);
162+
Plugin.SendMotd((MyPlayer)Context.Player);
163163
}
164164
}
165165
}

‎Essentials/EssentialsPlugin.cs‎

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ private void SessionChanged(ITorchSession session, TorchSessionState state)
7474
switch (state)
7575
{
7676
case TorchSessionState.Loaded:
77+
mpMan.PlayerJoined += MotdOnce;
7778
mpMan.PlayerLeft += ResetMotdOnce;
78-
MyEntities.OnEntityAdd += MotdOnce;
7979
if(Config.StopShipsOnStart)
8080
StopShips();
8181
_control.Dispatcher.Invoke(() =>
@@ -88,7 +88,7 @@ private void SessionChanged(ITorchSession session, TorchSessionState state)
8888
break;
8989
case TorchSessionState.Unloading:
9090
mpMan.PlayerLeft -= ResetMotdOnce;
91-
MyEntities.OnEntityAdd -= MotdOnce;
91+
mpMan.PlayerJoined += MotdOnce;
9292
break;
9393
}
9494
}
@@ -106,31 +106,39 @@ private void ResetMotdOnce(IPlayer player)
106106
_motdOnce.Remove(player.SteamId);
107107
}
108108

109-
private void MotdOnce(MyEntity obj)
109+
private void MotdOnce(IPlayer player)
110110
{
111-
if (obj is MyCharacter character)
112-
{
113-
Task.Run(() =>
114-
{
115-
Thread.Sleep(1000);
116-
Torch.Invoke(() =>
117-
{
118-
if (_motdOnce.Contains(character.ControlSteamId))
119-
return;
120-
121-
var id = character.ControllerInfo?.ControllingIdentityId;
122-
if (!id.HasValue)
123-
return;
124-
125-
SendMotd(id.Value);
126-
_motdOnce.Add(character.ControlSteamId);
127-
});
128-
});
129-
}
111+
//TODO: REMOVE ALL THIS TRASH!
112+
//implement a PlayerSpawned event in Torch. This will work for now.
113+
Task.Run(() =>
114+
{
115+
var start = DateTime.Now;
116+
var timeout = TimeSpan.FromMinutes(5);
117+
var pid = new MyPlayer.PlayerId(player.SteamId, 0);
118+
while (DateTime.Now - start <= timeout)
119+
{
120+
if (!MySession.Static.Players.TryGetPlayerById(pid, out MyPlayer p) || p.Character == null)
121+
{
122+
Thread.Sleep(1000);
123+
continue;
124+
}
125+
126+
Torch.Invoke(() =>
127+
{
128+
if (_motdOnce.Contains(player.SteamId))
129+
return;
130+
131+
SendMotd(p);
132+
_motdOnce.Add(player.SteamId);
133+
});
134+
break;
135+
}
136+
});
130137
}
131138

132-
public void SendMotd(long playerId = 0)
139+
public void SendMotd(MyPlayer player)
133140
{
141+
long playerId = player.Identity.IdentityId;
134142
if (!string.IsNullOrEmpty(Config.MotdUrl))
135143
{
136144
if (MyGuiSandbox.IsUrlWhitelisted(Config.MotdUrl))
@@ -139,12 +147,11 @@ public void SendMotd(long playerId = 0)
139147
MyVisualScriptLogicProvider.OpenSteamOverlay($"https://steamcommunity.com/linkfilter/?url={Config.MotdUrl}", playerId);
140148
}
141149

142-
var id = MySession.Static.Players.TryGetSteamId(playerId);
150+
var id = player.Client.SteamUserId;
143151
if (id <= 0) //can't remember if this returns 0 or -1 on error.
144152
return;
145-
146-
var player = MySession.Static.Players.TryGetIdentity(playerId);
147-
string name = player?.DisplayName ?? "player";
153+
154+
string name = player.Identity?.DisplayName ?? "player";
148155

149156
bool newUser = !Config.KnownSteamIds.Contains(id);
150157
if (newUser)

0 commit comments

Comments
 (0)