105 lines
3.6 KiB
C#
105 lines
3.6 KiB
C#
using Content.Shared.Containers.ItemSlots;
|
|
using Content.Shared.Interaction;
|
|
using Content.Shared.Nutrition.Components;
|
|
using Content.Shared.Nutrition.EntitySystems;
|
|
using Robust.Shared.Containers;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
namespace Content.Shared.Cabinet;
|
|
|
|
/// <summary>
|
|
/// Controls ItemCabinet slot locking and visuals.
|
|
/// </summary>
|
|
public sealed class ItemCabinetSystem : EntitySystem
|
|
{
|
|
[Dependency] private readonly ItemSlotsSystem _slots = default!;
|
|
[Dependency] private readonly OpenableSystem _openable = default!;
|
|
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
|
|
|
|
private EntityQuery<ItemCabinetComponent> _cabinetQuery = default!;
|
|
private EntityQuery<ItemSlotsComponent> _slotsQuery = default!;
|
|
|
|
/// <inheritdoc/>
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
_cabinetQuery = GetEntityQuery<ItemCabinetComponent>();
|
|
_slotsQuery = GetEntityQuery<ItemSlotsComponent>();
|
|
|
|
SubscribeLocalEvent<ItemCabinetComponent, ComponentStartup>(OnStartup);
|
|
SubscribeLocalEvent<ItemCabinetComponent, MapInitEvent>(OnMapInit);
|
|
SubscribeLocalEvent<ItemCabinetComponent, EntInsertedIntoContainerMessage>(OnContainerModified);
|
|
SubscribeLocalEvent<ItemCabinetComponent, EntRemovedFromContainerMessage>(OnContainerModified);
|
|
SubscribeLocalEvent<ItemCabinetComponent, OpenableOpenedEvent>(OnOpened);
|
|
SubscribeLocalEvent<ItemCabinetComponent, OpenableClosedEvent>(OnClosed);
|
|
}
|
|
|
|
private void OnStartup(Entity<ItemCabinetComponent> ent, ref ComponentStartup args)
|
|
{
|
|
UpdateAppearance(ent);
|
|
}
|
|
|
|
private void OnMapInit(Entity<ItemCabinetComponent> ent, ref MapInitEvent args)
|
|
{
|
|
// update at mapinit to avoid copy pasting locked: true and locked: false for each closed/open prototype
|
|
SetSlotLock((ent, ent.Comp), !_openable.IsOpen(ent));
|
|
}
|
|
|
|
private void UpdateAppearance(Entity<ItemCabinetComponent> ent)
|
|
{
|
|
_appearance.SetData(ent, ItemCabinetVisuals.ContainsItem, HasItem((ent, ent.Comp)));
|
|
}
|
|
|
|
private void OnContainerModified(EntityUid uid, ItemCabinetComponent component, ContainerModifiedMessage args)
|
|
{
|
|
if (args.Container.ID == component.Slot)
|
|
UpdateAppearance((uid, component));
|
|
}
|
|
|
|
private void OnOpened(Entity<ItemCabinetComponent> ent, ref OpenableOpenedEvent args)
|
|
{
|
|
SetSlotLock((ent, ent.Comp), false);
|
|
}
|
|
|
|
private void OnClosed(Entity<ItemCabinetComponent> ent, ref OpenableClosedEvent args)
|
|
{
|
|
SetSlotLock((ent, ent.Comp), true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tries to get the cabinet's item slot.
|
|
/// </summary>
|
|
public bool TryGetSlot(Entity<ItemCabinetComponent?> ent, [NotNullWhen(true)] out ItemSlot? slot)
|
|
{
|
|
slot = null;
|
|
if (!_cabinetQuery.Resolve(ent, ref ent.Comp))
|
|
return false;
|
|
|
|
if (!_slotsQuery.TryComp(ent, out var slots))
|
|
return false;
|
|
|
|
return _slots.TryGetSlot(ent, ent.Comp.Slot, out slot, slots);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if the cabinet contains an item.
|
|
/// </summary>
|
|
public bool HasItem(Entity<ItemCabinetComponent?> ent)
|
|
{
|
|
return TryGetSlot(ent, out var slot) && slot.HasItem;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lock or unlock the underlying item slot.
|
|
/// </summary>
|
|
public void SetSlotLock(Entity<ItemCabinetComponent> ent, bool closed)
|
|
{
|
|
if (!_slotsQuery.TryComp(ent, out var slots))
|
|
return;
|
|
|
|
if (_slots.TryGetSlot(ent, ent.Comp.Slot, out var slot, slots))
|
|
_slots.SetLock(ent, slot, closed, slots);
|
|
}
|
|
}
|