Fix ReplacePlayerForConn isLocalPlayer (#2969)

- Moved `ChangeOwner` down below `OnChangeOwner`
- Added `isLocalPlayer` bool to `ChangeOwnerMessage`
- Added handling for `isLocalPlayer to `ChangeOwner`
- Added call to `SendChangeOwnerMessage` to `ReplacePlayerForConnection` for when `keepAuthority` is true
- Fixes #2968
This commit is contained in:
MrGadget 2021-10-20 22:43:12 -04:00 committed by GitHub
parent 282c843ad1
commit 9184d56eec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 8 deletions

View File

@ -69,6 +69,7 @@ public struct ChangeOwnerMessage : NetworkMessage
{ {
public uint netId; public uint netId;
public bool isOwner; public bool isOwner;
public bool isLocalPlayer;
} }
public struct ObjectSpawnStartedMessage : NetworkMessage {} public struct ObjectSpawnStartedMessage : NetworkMessage {}

View File

@ -1020,12 +1020,6 @@ internal static void ApplySpawnPayload(NetworkIdentity identity, SpawnMessage me
} }
} }
internal static void ChangeOwner(NetworkIdentity identity, ChangeOwnerMessage message)
{
identity.hasAuthority = message.isOwner;
identity.NotifyAuthority();
}
// Finds Existing Object with NetId or spawns a new one using AssetId or sceneId // Finds Existing Object with NetId or spawns a new one using AssetId or sceneId
internal static bool FindOrSpawnObject(SpawnMessage message, out NetworkIdentity identity) internal static bool FindOrSpawnObject(SpawnMessage message, out NetworkIdentity identity)
{ {
@ -1286,6 +1280,24 @@ internal static void OnChangeOwner(ChangeOwnerMessage message)
Debug.LogError($"OnChangeOwner: Could not find object with netId {message.netId}"); Debug.LogError($"OnChangeOwner: Could not find object with netId {message.netId}");
} }
internal static void ChangeOwner(NetworkIdentity identity, ChangeOwnerMessage message)
{
identity.hasAuthority = message.isOwner;
identity.NotifyAuthority();
identity.isLocalPlayer = message.isLocalPlayer;
if (identity.isLocalPlayer)
localPlayer = identity;
else if (localPlayer == identity)
{
// localPlayer may already be assigned to something else
// so only make it null if it's this identity.
localPlayer = null;
}
CheckForLocalPlayer(identity);
}
internal static void CheckForLocalPlayer(NetworkIdentity identity) internal static void CheckForLocalPlayer(NetworkIdentity identity)
{ {
if (identity == localPlayer) if (identity == localPlayer)

View File

@ -762,7 +762,12 @@ public static bool ReplacePlayerForConnection(NetworkConnection conn, GameObject
Respawn(identity); Respawn(identity);
if (!keepAuthority) if (keepAuthority)
// This needs to be sent to clear isLocalPlayer on
// client while keeping hasAuthority true
SendChangeOwnerMessage(previousPlayer, conn);
else
// This clears both isLocalPlayer and hasAuthority on client
previousPlayer.RemoveClientAuthority(); previousPlayer.RemoveClientAuthority();
return true; return true;
@ -955,7 +960,12 @@ internal static void SendChangeOwnerMessage(NetworkIdentity identity, NetworkCon
//Debug.Log($"Server SendChangeOwnerMessage: name={identity.name} netid={identity.netId}"); //Debug.Log($"Server SendChangeOwnerMessage: name={identity.name} netid={identity.netId}");
conn.Send(new ChangeOwnerMessage { netId = identity.netId, isOwner = identity.connectionToClient == conn }); conn.Send(new ChangeOwnerMessage
{
netId = identity.netId,
isOwner = identity.connectionToClient == conn,
isLocalPlayer = conn.identity == identity
});
} }
static void SpawnObject(GameObject obj, NetworkConnection ownerConnection) static void SpawnObject(GameObject obj, NetworkConnection ownerConnection)