From 5d75539c7ed2d051bc628b2786e4d244f25bfc6b Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Fri, 3 Jun 2022 07:17:58 +1000 Subject: [PATCH] Fix aghost crashes (#8556) There's still some oddity around MobObserver / AdminObserver where if you move after suicide then aghost you return to your original body, but at least you won't spawn a billion AdminObservers anymore or crash if you controlmob one. --- Content.Server/Mind/Mind.cs | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) 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);