diff --git a/Content.Server/Mind/Mind.cs b/Content.Server/Mind/Mind.cs index 32b5ad88e9..c689c9ddc1 100644 --- a/Content.Server/Mind/Mind.cs +++ b/Content.Server/Mind/Mind.cs @@ -264,6 +264,13 @@ namespace Content.Server.Mind /// public void TransferTo(EntityUid? entity, bool ghostCheckOverride = false) { + // Looks like caller just wants us to go back to normal. + if (entity == OwnedEntity) + { + UnVisit(); + return; + } + var entMan = IoCManager.Resolve(); MindComponent? component = null; @@ -306,11 +313,11 @@ namespace Content.Server.Mind || !entMan.TryGetComponent(VisitingEntity!, out GhostComponent? ghostComponent) // visiting entity is not a Ghost || !ghostComponent.CanReturnToBody)) // it is a ghost, but cannot return to body anyway, so it's okay { - VisitingEntity = default; + RemoveVisitingEntity(); } // Player is CURRENTLY connected. - if (Session != null && !alreadyAttached && VisitingEntity == default) + if (Session != null && !alreadyAttached && VisitingEntity == null) { Session.AttachToEntity(entity); Logger.Info($"Session {Session.Name} transferred to entity {entity}."); @@ -370,17 +377,26 @@ namespace Content.Server.Mind Logger.Info($"Session {Session?.Name} visiting entity {entity}."); } + /// + /// Returns the mind to its original entity. + /// public void UnVisit() { - if (VisitingEntity == null) - { - return; - } - Session?.AttachToEntity(OwnedEntity); + RemoveVisitingEntity(); + } + + /// + /// Cleans up the VisitingEntity. + /// + private void RemoveVisitingEntity() + { + if (VisitingEntity == null) + return; + var oldVisitingEnt = VisitingEntity.Value; // Null this before removing the component to avoid any infinite loops. - VisitingEntity = default; + VisitingEntity = null; DebugTools.AssertNotNull(oldVisitingEnt);