Skip to content

Commit

Permalink
fix: ReplacePlayer now calls OnStartLocalPlayer (#1280)
Browse files Browse the repository at this point in the history
* fix: ReplacePlayer now calls OnStartLocalPlayer

fixes #962

* fix: replace player cannot steal another player
  • Loading branch information
paulpach committed Dec 4, 2019
1 parent b23cfc7 commit 0e1bc81
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions Assets/Mirror/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ public static bool AddPlayerForConnection(NetworkConnection conn, GameObject pla
conn.identity = identity;

// Set the connection on the NetworkIdentity on the server, NetworkIdentity.SetLocalPlayer is not called on the server (it is on clients)
identity.connectionToClient = (NetworkConnectionToClient)conn;
identity.SetClientOwner(conn);

// special case, we are in host mode, set hasAuthority to true so that all overrides see it
if (conn is ULocalConnectionToClient)
Expand All @@ -831,18 +831,21 @@ public static bool AddPlayerForConnection(NetworkConnection conn, GameObject pla

if (LogFilter.Debug) Debug.Log("Adding new playerGameObject object netId: " + identity.netId + " asset ID " + identity.assetId);

FinishPlayerForConnection(identity, player);
identity.SetClientOwner(conn);
Respawn(identity);
return true;
}

static void FinishPlayerForConnection(NetworkIdentity identity, GameObject playerGameObject)
static void Respawn(NetworkIdentity identity)
{
if (identity.netId == 0)
{
// it is allowed to provide an already spawned object as the new player object.
// so dont spawn it again.
Spawn(playerGameObject, identity.connectionToClient);
// If the object has not been spawned, then do a full spawn and update observers
Spawn(identity.gameObject, identity.connectionToClient);
}
else
{
// otherwise just replace his data
SendSpawnMessage(identity, identity.connectionToClient);
}
}

Expand All @@ -855,6 +858,12 @@ internal static bool InternalReplacePlayerForConnection(NetworkConnection conn,
return false;
}

if (identity.connectionToClient != null && identity.connectionToClient != conn)
{
Debug.LogError("Cannot replace player for connection. New player is already owned by a different connection" + player);
return false;
}

//NOTE: there can be an existing player
if (LogFilter.Debug) Debug.Log("NetworkServer ReplacePlayer");

Expand All @@ -863,7 +872,7 @@ internal static bool InternalReplacePlayerForConnection(NetworkConnection conn,
conn.identity = identity;

// Set the connection on the NetworkIdentity on the server, NetworkIdentity.SetLocalPlayer is not called on the server (it is on clients)
identity.connectionToClient = (NetworkConnectionToClient)conn;
identity.SetClientOwner(conn);

//NOTE: DONT set connection ready.

Expand All @@ -883,8 +892,7 @@ internal static bool InternalReplacePlayerForConnection(NetworkConnection conn,

if (LogFilter.Debug) Debug.Log("Replacing playerGameObject object netId: " + player.GetComponent<NetworkIdentity>().netId + " asset ID " + player.GetComponent<NetworkIdentity>().assetId);

FinishPlayerForConnection(identity, player);
identity.SetClientOwner(conn);
Respawn(identity);

if (!keepAuthority)
previousPlayer.RemoveClientAuthority();
Expand Down

0 comments on commit 0e1bc81

Please sign in to comment.