Allow changing the owner of a mind.

This commit is contained in:
Pieter-Jan Briers
2018-11-24 19:12:22 +01:00
parent 10120bb0b6
commit 9780cf9062
2 changed files with 52 additions and 2 deletions

View File

@@ -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.
/// </summary>
[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<IPlayerManager>();
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<IPlayerManager>();
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);

View File

@@ -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
/// <summary>
/// The currently occupied mind of the player owning this data.
/// DO NOT DIRECTLY SET THIS UNLESS YOU KNOW WHAT YOU'RE DOING.
/// </summary>
[ViewVariables]
public Mind Mind { get; set; }
public void WipeMind()
{
Mind.ChangeOwningPlayer(null);
}
public PlayerData(NetSessionId sessionId)
{
SessionId = sessionId;