From 75589c25aba978fc258fa78e6e01df6e1a7ad6a4 Mon Sep 17 00:00:00 2001 From: kremnev8 Date: Fri, 18 Mar 2022 11:21:44 +0300 Subject: [PATCH] Add OnPlayerJoin and OnPlayerLeft events to API --- NebulaAPI/NebulaModAPI.cs | 13 +++++++++++++ .../Session/LobbyRequestProcessor.cs | 1 + .../Session/StartGameMessageProcessor.cs | 1 + NebulaNetwork/PlayerManager.cs | 1 + 4 files changed, 16 insertions(+) diff --git a/NebulaAPI/NebulaModAPI.cs b/NebulaAPI/NebulaModAPI.cs index c025f7360..2dbc2b8e6 100644 --- a/NebulaAPI/NebulaModAPI.cs +++ b/NebulaAPI/NebulaModAPI.cs @@ -86,6 +86,19 @@ public static IMultiplayerSession MultiplayerSession /// public static Action OnPlanetLoadFinished; + /// + /// Subscribe to receive even when a new player joins the game. + /// The event fires right after the player starts to load in the save data + /// > player - info about the player + /// + public static Action OnPlayerJoinedGame; + + /// + /// Subscribe to receive even when a new player leaves the game + /// > player - info about the player + /// + public static Action OnPlayerLeftGame; + private void Awake() { nebulaIsInstalled = false; diff --git a/NebulaNetwork/PacketProcessors/Session/LobbyRequestProcessor.cs b/NebulaNetwork/PacketProcessors/Session/LobbyRequestProcessor.cs index a5f9fc024..bf5fa1016 100644 --- a/NebulaNetwork/PacketProcessors/Session/LobbyRequestProcessor.cs +++ b/NebulaNetwork/PacketProcessors/Session/LobbyRequestProcessor.cs @@ -164,6 +164,7 @@ public override void ProcessPacket(LobbyRequest packet, NebulaConnection conn) } Multiplayer.Session.World.OnPlayerJoining(player.Data.Username); + NebulaModAPI.OnPlayerJoinedGame?.Invoke(player.Data); // Make sure that each player that is currently in the game receives that a new player as join so they can create its RemotePlayerCharacter PlayerJoining pdata = new PlayerJoining((PlayerData)player.Data.CreateCopyWithoutMechaData()); // Remove inventory from mecha data diff --git a/NebulaNetwork/PacketProcessors/Session/StartGameMessageProcessor.cs b/NebulaNetwork/PacketProcessors/Session/StartGameMessageProcessor.cs index e227184aa..7db5be354 100644 --- a/NebulaNetwork/PacketProcessors/Session/StartGameMessageProcessor.cs +++ b/NebulaNetwork/PacketProcessors/Session/StartGameMessageProcessor.cs @@ -46,6 +46,7 @@ public override void ProcessPacket(StartGameMessage packet, NebulaConnection con } Multiplayer.Session.World.OnPlayerJoining(player.Data.Username); + NebulaModAPI.OnPlayerJoinedGame?.Invoke(player.Data); // Make sure that each player that is currently in the game receives that a new player as join so they can create its RemotePlayerCharacter PlayerJoining pdata = new PlayerJoining((PlayerData)player.Data.CreateCopyWithoutMechaData()); // Remove inventory from mecha data diff --git a/NebulaNetwork/PlayerManager.cs b/NebulaNetwork/PlayerManager.cs index 7e2107772..f2d35d30c 100644 --- a/NebulaNetwork/PlayerManager.cs +++ b/NebulaNetwork/PlayerManager.cs @@ -288,6 +288,7 @@ public void PlayerDisconnected(INebulaConnection conn) if (player != null) { SendPacketToOtherPlayers(new PlayerDisconnected(player.Id), player); + NebulaModAPI.OnPlayerLeftGame?.Invoke(player.Data); Multiplayer.Session.World.DestroyRemotePlayerModel(player.Id); using (threadSafe.availablePlayerIds.GetLocked(out Queue availablePlayerIds)) {