Skip to content

Commit a79a6d1

Browse files
committed
better
1 parent 6a5ad1d commit a79a6d1

1 file changed

Lines changed: 42 additions & 8 deletions

File tree

EXILED/Exiled.API/Extensions/MirrorExtensions.cs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -658,14 +658,14 @@ public static void EditNetworkObject(this Player player, NetworkIdentity identit
658658
}
659659

660660
/// <summary>
661-
/// Respawns the specified <see cref="NetworkIdentity"/> by respawning its underlying <see cref="GameObject"/> on the server.
662-
/// This forces Mirror to reinitialize the network state for the object.
661+
/// Sends a spawn message for the specified <see cref="NetworkIdentity"/> to a targeted collection of players.
663662
/// </summary>
664-
/// <param name="identity">The <see cref="NetworkIdentity"/> to respawn.</param>
665-
public static void RespawnNetworkIdentity(this NetworkIdentity identity)
663+
/// <param name="identity">The <see cref="NetworkIdentity"/> to serialize and spawn.</param>
664+
/// <param name="players">The collection of <see cref="Player"/> who will receive the spawn message.</param>
665+
public static void SendSpawnMessageForPlayers(this NetworkIdentity identity, IEnumerable<Player> players)
666666
{
667-
ObjectDestroyMessage destroyMessage = new() { netId = identity.netId };
668-
NetworkServer.SendToReady(destroyMessage);
667+
if (identity == null || identity.netId == 0 || !players.Any())
668+
return;
669669

670670
using NetworkWriterPooled ownerWriter = NetworkWriterPool.Get();
671671
using NetworkWriterPooled observersWriter = NetworkWriterPool.Get();
@@ -688,9 +688,9 @@ public static void RespawnNetworkIdentity(this NetworkIdentity identity)
688688
payload = observerPayload,
689689
};
690690

691-
foreach (Player player in Player.List)
691+
foreach (Player player in players)
692692
{
693-
if (player.Connection == null || !player.IsConnected)
693+
if (!player.IsConnected)
694694
continue;
695695

696696
bool isOwner = identity.connectionToClient == player.Connection;
@@ -714,6 +714,40 @@ public static void RespawnNetworkIdentity(this NetworkIdentity identity)
714714
}
715715
}
716716

717+
/// <summary>
718+
/// Sends a destroy message for the specified <see cref="NetworkIdentity"/> to a targeted collection of players.
719+
/// </summary>
720+
/// <param name="identity">The <see cref="NetworkIdentity"/> to destroy on the clients.</param>
721+
/// <param name="players">The collection of <see cref="Player"/> who will receive the destroy message.</param>
722+
public static void DestroyNetworkIdentityForPlayers(this NetworkIdentity identity, IEnumerable<Player> players)
723+
{
724+
if (identity == null || identity.netId == 0 || !players.Any())
725+
return;
726+
727+
ObjectDestroyMessage destroyMessage = new ObjectDestroyMessage() { netId = identity.netId };
728+
729+
foreach (Player player in players)
730+
{
731+
if (!player.IsConnected)
732+
continue;
733+
734+
player.Connection.Send(destroyMessage);
735+
}
736+
}
737+
738+
/// <summary>
739+
/// Respawns the specified <see cref="NetworkIdentity"/> by respawning its underlying <see cref="GameObject"/> on the server.
740+
/// </summary>
741+
/// <param name="identity">The <see cref="NetworkIdentity"/> to respawn.</param>
742+
public static void RespawnNetworkIdentity(this NetworkIdentity identity)
743+
{
744+
if (identity == null || identity.netId == 0)
745+
return;
746+
747+
NetworkServer.SendToReady(new ObjectDestroyMessage() { netId = identity.netId });
748+
identity.SendSpawnMessageForPlayers(Player.List);
749+
}
750+
717751
/// <summary>
718752
/// Respawns a networked <see cref="GameObject"/> on the server by unspawning and respawning it.
719753
/// This forces Mirror to reinitialize the network state for the object.

0 commit comments

Comments
 (0)