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
This commit is contained in:
@@ -28,17 +28,17 @@ namespace Content.Server.GameObjects.Components.ActionBlocking
|
|||||||
/// How many of this entity's hands are currently cuffed.
|
/// How many of this entity's hands are currently cuffed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
public int CuffedHandCount => _container.ContainedEntities.Count * 2;
|
public int CuffedHandCount => Container.ContainedEntities.Count * 2;
|
||||||
|
|
||||||
protected IEntity LastAddedCuffs => _container.ContainedEntities[_container.ContainedEntities.Count - 1];
|
protected IEntity LastAddedCuffs => Container.ContainedEntities[^1];
|
||||||
|
|
||||||
public IReadOnlyList<IEntity> StoredEntities => _container.ContainedEntities;
|
public IReadOnlyList<IEntity> StoredEntities => Container.ContainedEntities;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Container of various handcuffs currently applied to the entity.
|
/// Container of various handcuffs currently applied to the entity.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ViewVariables(VVAccess.ReadOnly)]
|
[ViewVariables(VVAccess.ReadOnly)]
|
||||||
private Container _container = default!;
|
public Container Container { get; set; } = default!;
|
||||||
|
|
||||||
// TODO: Make a component message
|
// TODO: Make a component message
|
||||||
public event Action? OnCuffedStateChanged;
|
public event Action? OnCuffedStateChanged;
|
||||||
@@ -49,10 +49,7 @@ namespace Content.Server.GameObjects.Components.ActionBlocking
|
|||||||
{
|
{
|
||||||
base.Initialize();
|
base.Initialize();
|
||||||
|
|
||||||
_container = ContainerManagerComponent.Ensure<Container>(Name, Owner);
|
Container = ContainerManagerComponent.Ensure<Container>(Name, Owner);
|
||||||
|
|
||||||
Owner.EntityManager.EventBus.SubscribeEvent<HandCountChangedEvent>(EventSource.Local, this, HandleHandCountChange);
|
|
||||||
|
|
||||||
Owner.EnsureComponentWarn<HandsComponent>();
|
Owner.EnsureComponentWarn<HandsComponent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,7 +107,7 @@ namespace Content.Server.GameObjects.Components.ActionBlocking
|
|||||||
handsComponent.Drop(handcuff);
|
handsComponent.Drop(handcuff);
|
||||||
}
|
}
|
||||||
|
|
||||||
_container.Insert(handcuff);
|
Container.Insert(handcuff);
|
||||||
CanStillInteract = Owner.TryGetComponent(out HandsComponent? ownerHands) && ownerHands.Hands.Count() > CuffedHandCount;
|
CanStillInteract = Owner.TryGetComponent(out HandsComponent? ownerHands) && ownerHands.Hands.Count() > CuffedHandCount;
|
||||||
|
|
||||||
OnCuffedStateChanged?.Invoke();
|
OnCuffedStateChanged?.Invoke();
|
||||||
@@ -120,37 +117,10 @@ namespace Content.Server.GameObjects.Components.ActionBlocking
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
public void CuffedStateChanged()
|
||||||
/// 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 UpdateHandCount()
|
|
||||||
{
|
{
|
||||||
var dirty = false;
|
UpdateAlert();
|
||||||
var handCount = Owner.TryGetComponent(out HandsComponent? handsComponent) ? handsComponent.Hands.Count() : 0;
|
OnCuffedStateChanged?.Invoke();
|
||||||
|
|
||||||
while (CuffedHandCount > handCount && CuffedHandCount > 0)
|
|
||||||
{
|
|
||||||
dirty = true;
|
|
||||||
|
|
||||||
var entity = _container.ContainedEntities[_container.ContainedEntities.Count - 1];
|
|
||||||
_container.Remove(entity);
|
|
||||||
entity.Transform.WorldPosition = Owner.Transform.Coordinates.Position;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dirty)
|
|
||||||
{
|
|
||||||
CanStillInteract = handCount > CuffedHandCount;
|
|
||||||
OnCuffedStateChanged?.Invoke();
|
|
||||||
Dirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void HandleHandCountChange(HandCountChangedEvent message)
|
|
||||||
{
|
|
||||||
if (message.Sender == Owner)
|
|
||||||
{
|
|
||||||
UpdateHandCount();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -212,11 +182,16 @@ namespace Content.Server.GameObjects.Components.ActionBlocking
|
|||||||
|
|
||||||
if (cuffsToRemove == null)
|
if (cuffsToRemove == null)
|
||||||
{
|
{
|
||||||
|
if (Container.ContainedEntities.Count == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
cuffsToRemove = LastAddedCuffs;
|
cuffsToRemove = LastAddedCuffs;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!_container.ContainedEntities.Contains(cuffsToRemove))
|
if (!Container.ContainedEntities.Contains(cuffsToRemove))
|
||||||
{
|
{
|
||||||
Logger.Warning("A user is trying to remove handcuffs that aren't in the owner's container. This should never happen!");
|
Logger.Warning("A user is trying to remove handcuffs that aren't in the owner's container. This should never happen!");
|
||||||
}
|
}
|
||||||
@@ -282,7 +257,7 @@ namespace Content.Server.GameObjects.Components.ActionBlocking
|
|||||||
if (cuff.EndUncuffSound != null)
|
if (cuff.EndUncuffSound != null)
|
||||||
audio.PlayFromEntity(cuff.EndUncuffSound, Owner);
|
audio.PlayFromEntity(cuff.EndUncuffSound, Owner);
|
||||||
|
|
||||||
_container.ForceRemove(cuffsToRemove);
|
Container.ForceRemove(cuffsToRemove);
|
||||||
cuffsToRemove.Transform.AttachToGridOrMap();
|
cuffsToRemove.Transform.AttachToGridOrMap();
|
||||||
cuffsToRemove.Transform.WorldPosition = Owner.Transform.WorldPosition;
|
cuffsToRemove.Transform.WorldPosition = Owner.Transform.WorldPosition;
|
||||||
|
|
||||||
|
|||||||
51
Content.Server/GameObjects/EntitySystems/CuffableSystem.cs
Normal file
51
Content.Server/GameObjects/EntitySystems/CuffableSystem.cs
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user