Files
tbd-station-14/Content.Server/GameObjects/EntitySystems/CuffableSystem.cs
DrSmugleaf 0dd75e1821 Move event bus listener from CuffableComponent to CuffableSystem (#3321)
* Move event bus listener from CuffableComponent to CuffableSystem

* Fix uncuffing when inside a container

* Fix not updating the status
2021-02-21 13:39:54 +01:00

52 lines
1.6 KiB
C#

#nullable enable
using Content.Server.GameObjects.Components.ActionBlocking;
using Content.Server.GameObjects.Components.GUI;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
namespace Content.Server.GameObjects.EntitySystems
{
[UsedImplicitly]
public class CuffableSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
EntityManager.EventBus.SubscribeEvent<HandCountChangedEvent>(EventSource.Local, this, OnHandCountChanged);
}
/// <summary>
/// Check the current amount of hands the owner has, and if there's less hands than active cuffs we remove some cuffs.
/// </summary>
private void OnHandCountChanged(HandCountChangedEvent message)
{
var owner = message.Sender;
if (!owner.TryGetComponent(out CuffableComponent? cuffable) ||
!cuffable.Initialized) return;
var dirty = false;
var handCount = owner.GetComponentOrNull<HandsComponent>()?.Count ?? 0;
while (cuffable.CuffedHandCount > handCount && cuffable.CuffedHandCount > 0)
{
dirty = true;
var container = cuffable.Container;
var entity = container.ContainedEntities[^1];
container.Remove(entity);
entity.Transform.WorldPosition = owner.Transform.WorldPosition;
}
if (dirty)
{
cuffable.CanStillInteract = handCount > cuffable.CuffedHandCount;
cuffable.CuffedStateChanged();
cuffable.Dirty();
}
}
}
}