Files
tbd-station-14/Content.Shared/Buckle/SharedBuckleSystem.Strap.cs
Tayrtahn ef79373904 Fix chairs deleting players (#37261)
Unbuckle entities when a strap is about to be deleted
2025-05-07 19:25:17 -04:00

100 lines
3.5 KiB
C#

using System.Linq;
using Content.Shared.Buckle.Components;
using Content.Shared.Construction;
using Content.Shared.Destructible;
using Content.Shared.Foldable;
using Content.Shared.Storage;
using Robust.Shared.Containers;
namespace Content.Shared.Buckle;
public abstract partial class SharedBuckleSystem
{
private void InitializeStrap()
{
SubscribeLocalEvent<StrapComponent, ComponentStartup>(OnStrapStartup);
SubscribeLocalEvent<StrapComponent, ComponentShutdown>(OnStrapShutdown);
SubscribeLocalEvent<StrapComponent, EntityTerminatingEvent>(OnStrapTerminating);
SubscribeLocalEvent<StrapComponent, ComponentRemove>((e, c, _) => StrapRemoveAll(e, c));
SubscribeLocalEvent<StrapComponent, ContainerGettingInsertedAttemptEvent>(OnStrapContainerGettingInsertedAttempt);
SubscribeLocalEvent<StrapComponent, DestructionEventArgs>((e, c, _) => StrapRemoveAll(e, c));
SubscribeLocalEvent<StrapComponent, BreakageEventArgs>((e, c, _) => StrapRemoveAll(e, c));
SubscribeLocalEvent<StrapComponent, FoldAttemptEvent>(OnAttemptFold);
SubscribeLocalEvent<StrapComponent, MachineDeconstructedEvent>((e, c, _) => StrapRemoveAll(e, c));
}
private void OnStrapStartup(EntityUid uid, StrapComponent component, ComponentStartup args)
{
Appearance.SetData(uid, StrapVisuals.State, component.BuckledEntities.Count != 0);
}
private void OnStrapShutdown(EntityUid uid, StrapComponent component, ComponentShutdown args)
{
if (!TerminatingOrDeleted(uid))
StrapRemoveAll(uid, component);
}
private void OnStrapTerminating(Entity<StrapComponent> entity, ref EntityTerminatingEvent args)
{
StrapRemoveAll(entity, entity.Comp);
}
private void OnStrapContainerGettingInsertedAttempt(EntityUid uid, StrapComponent component, ContainerGettingInsertedAttemptEvent args)
{
// If someone is attempting to put this item inside of a backpack, ensure that it has no entities strapped to it.
if (args.Container.ID == StorageComponent.ContainerId && component.BuckledEntities.Count != 0)
args.Cancel();
}
private void OnAttemptFold(EntityUid uid, StrapComponent component, ref FoldAttemptEvent args)
{
if (args.Cancelled)
return;
args.Cancelled = component.BuckledEntities.Count != 0;
}
/// <summary>
/// Remove everything attached to the strap
/// </summary>
private void StrapRemoveAll(EntityUid uid, StrapComponent strapComp)
{
foreach (var entity in strapComp.BuckledEntities.ToArray())
{
Unbuckle(entity, entity);
}
}
private bool StrapHasSpace(EntityUid strapUid, BuckleComponent buckleComp, StrapComponent? strapComp = null)
{
if (!Resolve(strapUid, ref strapComp, false))
return false;
var avail = strapComp.Size;
foreach (var buckle in strapComp.BuckledEntities)
{
avail -= CompOrNull<BuckleComponent>(buckle)?.Size ?? 0;
}
return avail >= buckleComp.Size;
}
/// <summary>
/// Sets the enabled field in the strap component to a value
/// </summary>
public void StrapSetEnabled(EntityUid strapUid, bool enabled, StrapComponent? strapComp = null)
{
if (!Resolve(strapUid, ref strapComp, false) ||
strapComp.Enabled == enabled)
return;
strapComp.Enabled = enabled;
Dirty(strapUid, strapComp);
if (!enabled)
StrapRemoveAll(strapUid, strapComp);
}
}