Make storage UI close upon being locked (#27810)

* make storage close on lock

* formatting and comments

* Update Content.Shared/Storage/EntitySystems/SharedStorageSystem.cs

Co-authored-by: ShadowCommander <shadowjjt@gmail.com>

* Apply suggestions from code review

Co-authored-by: ShadowCommander <shadowjjt@gmail.com>

* Swap to foreach instead of for

Co-authored-by: Kara <lunarautomaton6@gmail.com>

---------

Co-authored-by: ShadowCommander <shadowjjt@gmail.com>
Co-authored-by: Kara <lunarautomaton6@gmail.com>
This commit is contained in:
Sphiral
2024-05-09 01:50:50 -05:00
committed by GitHub
parent c866c2f524
commit de729f9037

View File

@@ -109,6 +109,7 @@ public abstract class SharedStorageSystem : EntitySystem
SubscribeLocalEvent<StorageComponent, AfterInteractEvent>(AfterInteract);
SubscribeLocalEvent<StorageComponent, DestructionEventArgs>(OnDestroy);
SubscribeLocalEvent<StorageComponent, BoundUIOpenedEvent>(OnBoundUIOpen);
SubscribeLocalEvent<StorageComponent, LockToggledEvent>(OnLockToggled);
SubscribeLocalEvent<MetaDataComponent, StackCountChangedEvent>(OnStackCountChanged);
SubscribeLocalEvent<StorageComponent, EntInsertedIntoContainerMessage>(OnEntInserted);
@@ -1401,6 +1402,25 @@ public abstract class SharedStorageSystem : EntitySystem
return _nextSmallest[item.Size];
}
/// <summary>
/// Checks if a storage's UI is open by anyone when locked, and closes it unless they're an admin.
/// </summary>
private void OnLockToggled(EntityUid uid, StorageComponent component, ref LockToggledEvent args)
{
if (!args.Locked)
return;
// Gets everyone looking at the UI
foreach (var actor in _ui.GetActors(uid, StorageComponent.StorageUiKey.Key))
{
if (_admin.HasAdminFlag(actor, AdminFlags.Admin))
continue;
// And closes it unless they're an admin
_ui.CloseUi(uid, StorageComponent.StorageUiKey.Key, actor);
}
}
private void OnStackCountChanged(EntityUid uid, MetaDataComponent component, StackCountChangedEvent args)
{
if (_containerSystem.TryGetContainingContainer(uid, out var container, component) &&