diff --git a/Content.Server/Mind/Mind.cs b/Content.Server/Mind/Mind.cs index 61fb22b986..9f51860411 100644 --- a/Content.Server/Mind/Mind.cs +++ b/Content.Server/Mind/Mind.cs @@ -279,7 +279,7 @@ namespace Content.Server.Mind if (entity != null) { - if (!entMan.TryGetComponent(entity.Value, out component)) + if (!entMan.TryGetComponent(entity.Value, out component)) { component = entMan.AddComponent(entity.Value); } @@ -309,10 +309,18 @@ namespace Content.Server.Mind if(OwnedComponent != null) mindSystem.InternalAssignMind(OwnedComponent.Owner, this, OwnedComponent); - if (VisitingEntity != null - && (ghostCheckOverride // to force mind transfer, for example from ControlMobVerb - || !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 + // Don't do the full deletion cleanup if we're transferring to our visitingentity + if (alreadyAttached) + { + // Set VisitingEntity null first so the removal of VisitingMind doesn't get through Unvisit() and delete what we're visiting. + // Yes this control flow sucks. + VisitingEntity = null; + IoCManager.Resolve().RemoveComponent(entity!.Value); + } + else if (VisitingEntity != null + && (ghostCheckOverride // to force mind transfer, for example from ControlMobVerb + || !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 { RemoveVisitingEntity(); } @@ -402,11 +410,7 @@ namespace Content.Server.Mind DebugTools.AssertNotNull(oldVisitingEnt); var entities = IoCManager.Resolve(); - if (entities.HasComponent(oldVisitingEnt)) - { - entities.RemoveComponent(oldVisitingEnt); - } - + entities.RemoveComponent(oldVisitingEnt); entities.EventBus.RaiseLocalEvent(oldVisitingEnt, new MindUnvisitedMessage()); }