From 9780cf906229ab8ae812cee860259b3c9d3ae204 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Sat, 24 Nov 2018 19:12:22 +0100 Subject: [PATCH] Allow changing the owner of a mind. --- Content.Server/Mobs/Mind.cs | 47 ++++++++++++++++++++++++++-- Content.Server/Players/PlayerData.cs | 7 +++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/Content.Server/Mobs/Mind.cs b/Content.Server/Mobs/Mind.cs index b7ab5aaf8b..c134f13d12 100644 --- a/Content.Server/Mobs/Mind.cs +++ b/Content.Server/Mobs/Mind.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using Content.Server.GameObjects.Components.Mobs; +using Content.Server.Players; using SS14.Server.Interfaces.Player; using SS14.Shared.Interfaces.GameObjects; using SS14.Shared.IoC; @@ -37,7 +38,7 @@ namespace Content.Server.Mobs /// The session ID of the player owning this mind. /// [ViewVariables] - public NetSessionId SessionId { get; } + public NetSessionId? SessionId { get; private set; } [ViewVariables] public bool IsVisitingEntity => VisitingEntity != null; @@ -76,8 +77,12 @@ namespace Content.Server.Mobs { get { + if (!SessionId.HasValue) + { + return null; + } var playerMgr = IoCManager.Resolve(); - playerMgr.TryGetSessionById(SessionId, out var ret); + playerMgr.TryGetSessionById(SessionId.Value, out var ret); return ret; } } @@ -212,6 +217,44 @@ namespace Content.Server.Mobs VisitingEntity = null; } + public void ChangeOwningPlayer(NetSessionId? newOwner) + { + var playerMgr = IoCManager.Resolve(); + PlayerData newOwnerData = null; + if (newOwner.HasValue) + { + if (!playerMgr.TryGetPlayerData(newOwner.Value, out var uncast)) + { + // This restriction is because I'm too lazy to initialize the player data + // for a client that hasn't logged in yet. + // Go ahead and remove it if you need. + throw new ArgumentException("new owner must have previously logged into the server."); + } + + newOwnerData = uncast.ContentData(); + } + + // Make sure to remove control from our old owner if they're logged in. + var oldSession = Session; + oldSession?.AttachToEntity(null); + + if (SessionId.HasValue) + { + playerMgr.GetPlayerData(SessionId.Value).ContentData().Mind = null; + } + + SessionId = newOwner; + if (!newOwner.HasValue) + { + return; + } + + // Yank new owner out of their old mind too. + // Can I mention how much I love the word yank? + newOwnerData.Mind?.ChangeOwningPlayer(null); + newOwnerData.Mind = this; + } + public void Visit(IEntity entity) { Session?.AttachToEntity(entity); diff --git a/Content.Server/Players/PlayerData.cs b/Content.Server/Players/PlayerData.cs index 0188331674..743f4a3e3f 100644 --- a/Content.Server/Players/PlayerData.cs +++ b/Content.Server/Players/PlayerData.cs @@ -1,5 +1,6 @@ using Content.Server.Mobs; using SS14.Server.Interfaces.Player; +using SS14.Shared.IoC; using SS14.Shared.Network; using SS14.Shared.ViewVariables; @@ -19,10 +20,16 @@ namespace Content.Server.Players /// /// The currently occupied mind of the player owning this data. + /// DO NOT DIRECTLY SET THIS UNLESS YOU KNOW WHAT YOU'RE DOING. /// [ViewVariables] public Mind Mind { get; set; } + public void WipeMind() + { + Mind.ChangeOwningPlayer(null); + } + public PlayerData(NetSessionId sessionId) { SessionId = sessionId;