Skip to content

Commit 686ebdc

Browse files
committed
New Join & Leave events, added a fix for duplicate ExPlayer instances
1 parent 2c9485f commit 686ebdc

3 files changed

Lines changed: 110 additions & 14 deletions

File tree

LabExtended/Events/ExPlayerEvents.cs

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using LabExtended.API;
2+
23
using LabExtended.Extensions;
34

45
using LabExtended.Events.Player;
@@ -20,16 +21,46 @@ public static class ExPlayerEvents
2021
/// </summary>
2122
public static event Action<ExPlayer>? Joined;
2223

24+
/// <summary>
25+
/// Gets called when a new NPC joins the server.
26+
/// </summary>
27+
public static event Action<ExPlayer>? NpcJoined;
28+
29+
/// <summary>
30+
/// Gets called when the local server player joins the server.
31+
/// </summary>
32+
public static event Action<ExPlayer>? HostJoined;
33+
2334
/// <summary>
2435
/// Gets called when a player finishes authentification.
2536
/// </summary>
26-
public static event Action<ExPlayer>? Verified;
37+
public static event Action<ExPlayer>? Verified;
38+
39+
/// <summary>
40+
/// Gets called when an NPC player finishes authentification.
41+
/// </summary>
42+
public static event Action<ExPlayer>? NpcVerified;
43+
44+
/// <summary>
45+
/// Gets called when the local server player finishes authentification.
46+
/// </summary>
47+
public static event Action<ExPlayer>? HostVerified;
2748

2849
/// <summary>
2950
/// Gets called after the player's object is destroyed.
3051
/// </summary>
31-
public static event Action<ExPlayer>? Left;
32-
52+
public static event Action<ExPlayer>? Left;
53+
54+
/// <summary>
55+
/// Gets called after an NPC player's object is destroyed.
56+
/// </summary>
57+
public static event Action<ExPlayer>? NpcLeft;
58+
59+
/// <summary>
60+
/// Gets called after the local server player's object is destroyed.
61+
/// </summary>
62+
public static event Action<ExPlayer>? HostLeft;
63+
3364
/// <inheritdoc cref="PlayerLeavingEventArgs"/>
3465
public static event Action<PlayerLeavingEventArgs>? Leaving;
3566
#endregion
@@ -139,21 +170,63 @@ public static class ExPlayerEvents
139170
/// <param name="player">Player who joined.</param>
140171
public static void OnJoined(ExPlayer player)
141172
=> Joined.InvokeSafe(player);
142-
173+
174+
/// <summary>
175+
/// Invokes the <see cref="NpcJoined"/> event.
176+
/// </summary>
177+
/// <param name="player">Player who joined.</param>
178+
public static void OnNpcJoined(ExPlayer player)
179+
=> NpcJoined.InvokeSafe(player);
180+
181+
/// <summary>
182+
/// Invokes the <see cref="HostJoined"/> event.
183+
/// </summary>
184+
/// <param name="player">Player who joined.</param>
185+
public static void OnHostJoined(ExPlayer player)
186+
=> HostJoined.InvokeSafe(player);
187+
143188
/// <summary>
144189
/// Invokes the <see cref="Verified"/> event.
145190
/// </summary>
146191
/// <param name="player">The player who just verified.</param>
147192
public static void OnVerified(ExPlayer player)
148193
=> Verified.InvokeSafe(player);
149-
194+
195+
/// <summary>
196+
/// Invokes the <see cref="NpcVerified"/> event.
197+
/// </summary>
198+
/// <param name="player">Player who joined.</param>
199+
public static void OnNpcVerified(ExPlayer player)
200+
=> NpcJoined.InvokeSafe(player);
201+
202+
/// <summary>
203+
/// Invokes the <see cref="HostVerified"/> event.
204+
/// </summary>
205+
/// <param name="player">Player who joined.</param>
206+
public static void OnHostVerified(ExPlayer player)
207+
=> HostJoined.InvokeSafe(player);
208+
150209
/// <summary>
151210
/// Invokes the <see cref="Left"/> event.
152211
/// </summary>
153212
/// <param name="player">Player who left.</param>
154213
public static void OnLeft(ExPlayer player)
155214
=> Left.InvokeSafe(player);
156215

216+
/// <summary>
217+
/// Invokes the <see cref="NpcLeft"/> event.
218+
/// </summary>
219+
/// <param name="player">Player who left.</param>
220+
public static void OnNpcLeft(ExPlayer player)
221+
=> NpcLeft.InvokeSafe(player);
222+
223+
/// <summary>
224+
/// Invokes the <see cref="HostLeft"/> event.
225+
/// </summary>
226+
/// <param name="player">Player who left.</param>
227+
public static void OnHostLeft(ExPlayer player)
228+
=> HostLeft.InvokeSafe(player);
229+
157230
/// <summary>
158231
/// Invokes the <see cref="Leaving"/> event.
159232
/// </summary>

LabExtended/Events/InternalEvents.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
using LabApi.Events;
22
using LabApi.Events.Handlers;
3+
34
using LabApi.Events.Arguments.PlayerEvents;
45
using LabApi.Events.Arguments.ServerEvents;
56

67
using LabExtended.API;
78
using LabExtended.API.Enums;
89

910
using LabExtended.Core;
10-
11-
using LabExtended.Attributes;
1211
using LabExtended.Extensions;
1312

14-
using NetworkManagerUtils;
1513
using NetworkManagerUtils.Dummies;
1614

1715
#pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
@@ -40,12 +38,18 @@ internal static class InternalEvents
4038
internal static void HandlePlayerVerified(ExPlayer player)
4139
{
4240
if (player.IsServer)
41+
{
42+
ExPlayerEvents.OnHostVerified(player);
4343
return;
44+
}
4445

4546
OnPlayerVerified.InvokeSafe(player);
4647

47-
if (player.IsNpc)
48+
if (player.IsNpc)
49+
{
50+
ExPlayerEvents.OnNpcVerified(player);
4851
return;
52+
}
4953

5054
if (!string.IsNullOrWhiteSpace(player.UserId))
5155
{
@@ -80,12 +84,24 @@ internal static void HandlePlayerVerified(ExPlayer player)
8084
internal static void HandlePlayerJoin(ExPlayer player)
8185
{
8286
if (!player.IsServer)
87+
{
8388
OnPlayerJoined.InvokeSafe(player);
89+
}
8490
else
91+
{
8592
OnHostJoined.InvokeSafe(player);
8693

87-
if (!player.IsNpc)
94+
ExPlayerEvents.OnHostJoined(player);
95+
}
96+
97+
if (!player.IsNpc)
98+
{
8899
ExPlayerEvents.OnJoined(player);
100+
}
101+
else
102+
{
103+
ExPlayerEvents.OnNpcJoined(player);
104+
}
89105
}
90106

91107
internal static void HandlePlayerLeave(ExPlayer? player)
@@ -119,6 +135,12 @@ internal static void HandlePlayerLeave(ExPlayer? player)
119135
else if (player.IsServer)
120136
{
121137
OnHostLeft.InvokeSafe(player);
138+
139+
ExPlayerEvents.OnHostLeft(player);
140+
}
141+
else if (player.IsNpc)
142+
{
143+
ExPlayerEvents.OnNpcLeft(player);
122144
}
123145
}
124146

LabExtended/Patches/Functions/Players/PlayerConstructorPatch.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ private static bool JoinPrefix(ReferenceHub referenceHub)
2323
{
2424
if (referenceHub != null)
2525
{
26-
var player = new ExPlayer(referenceHub,
27-
referenceHub.isLocalPlayer || referenceHub.connectionToClient.GetType() != typeof(NetworkConnectionToClient)
28-
? GetNewNpcToggles()
29-
: GetNewPlayerToggles());
26+
if (!ExPlayer.TryGet(referenceHub, out var player))
27+
player = new ExPlayer(referenceHub,
28+
referenceHub.isLocalPlayer || referenceHub.connectionToClient.GetType() != typeof(NetworkConnectionToClient)
29+
? GetNewNpcToggles()
30+
: GetNewPlayerToggles());
3031

3132
if (referenceHub.isLocalPlayer)
3233
{

0 commit comments

Comments
 (0)