Skip to content

Commit c2823c4

Browse files
Merge pull request #221 from UchuServer/enhancement/player-leave-event
Add Player Leave Event
2 parents a6780fd + afa38cf commit c2823c4

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

Uchu.Char/Handlers/CharacterHandler.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using RakDotNet;
77
using Uchu.Core;
88
using Uchu.Core.Client;
9+
using Uchu.World;
910

1011
namespace Uchu.Char.Handlers
1112
{
@@ -69,7 +70,17 @@ private static async Task SendCharacterList(IRakConnection connection, long user
6970
[PacketHandler]
7071
public async Task CharacterList(CharacterListRequest packet, IRakConnection connection)
7172
{
73+
// Remove the player from the zone.
7274
var session = UchuServer.SessionCache.GetSession(connection.EndPoint);
75+
if (UchuServer is WorldUchuServer worldUchuServer) {
76+
var player = worldUchuServer.FindPlayer(connection);
77+
if (player != null)
78+
{
79+
await player.DestroyAsync();
80+
}
81+
}
82+
83+
// Send the character list.
7384
await SendCharacterList(connection, session.UserId);
7485
}
7586

Uchu.Char/Uchu.Char.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
<ItemGroup>
99
<ProjectReference Include="..\Uchu.Core\Uchu.Core.csproj" />
10+
<ProjectReference Include="..\Uchu.World\Uchu.World.csproj" />
1011
</ItemGroup>
1112

1213
</Project>

Uchu.World/Objects/GameObjects/Player.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ internal Player()
5353
{
5454
Connection.Disconnected += async reason =>
5555
{
56-
Logger.Information($"{this} left: {reason}.");
57-
await GetComponent<SaveComponent>().SaveAsync(false);
58-
Connection = default;
59-
Destroy(this);
56+
await DestroyAsync();
6057
};
6158

6259
Listen(OnPositionUpdate, UpdatePhysics);
@@ -117,6 +114,17 @@ internal Player()
117114
});
118115
}
119116

117+
/// <summary>
118+
/// Destroys the player.
119+
/// </summary>
120+
public async Task DestroyAsync(CloseReason? reason = CloseReason.ClientDisconnect)
121+
{
122+
Logger.Information($"{this} left: {reason}.");
123+
await GetComponent<SaveComponent>().SaveAsync(false);
124+
Connection = default;
125+
Destroy(this);
126+
}
127+
120128
/// <summary>
121129
/// Constructs the player, settings spawn parameters and masks
122130
/// </summary>

Uchu.World/Objects/Zone.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public class Zone : Object
8383

8484
// Events
8585
public Event<Player> OnPlayerLoad { get; }
86+
public Event<Player> OnPlayerLeave { get; }
8687
public Event<Object> OnObject { get; }
8788
public Event OnTick { get; }
8889
public Event<Player, string> OnChatMessage { get; }
@@ -98,6 +99,7 @@ public Zone(ZoneInfo zoneInfo, UchuServer uchuServer, ushort instanceId = defaul
9899
EarlyPhysics = new Event();
99100
LatePhysics = new Event();
100101
OnPlayerLoad = new Event<Player>();
102+
OnPlayerLeave = new Event<Player>();
101103
OnObject = new Event<Object>();
102104
OnTick = new Event();
103105
OnChatMessage = new Event<Player, string>();
@@ -401,6 +403,12 @@ internal void UnregisterObject(Object obj)
401403

402404
ManagedObjects.Remove(obj);
403405

406+
// Invoke the player left event if the object is an event.
407+
if (obj is Player player)
408+
{
409+
OnPlayerLeave.Invoke(player);
410+
}
411+
404412
if (obj is GameObject gameObject)
405413
{
406414
if ((gameObject.Id.Flags & ObjectIdFlags.Spawned) != 0)

0 commit comments

Comments
 (0)