ContainerHeldSystem.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using Robust.Shared.Containers;
  2. using Content.Shared.Item;
  3. using Content.Shared.Storage;
  4. using Content.Shared.Storage.EntitySystems;
  5. using Content.Shared.Toggleable;
  6. namespace Content.Shared.ContainerHeld;
  7. public sealed class ContainerHeldSystem : EntitySystem
  8. {
  9. [Dependency] private readonly SharedItemSystem _item = default!;
  10. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  11. [Dependency] private readonly SharedStorageSystem _storage = default!;
  12. public override void Initialize()
  13. {
  14. base.Initialize();
  15. SubscribeLocalEvent<ContainerHeldComponent, EntInsertedIntoContainerMessage>(OnContainerModified);
  16. SubscribeLocalEvent<ContainerHeldComponent, EntRemovedFromContainerMessage>(OnContainerModified);
  17. }
  18. private void OnContainerModified(EntityUid uid, ContainerHeldComponent comp, ContainerModifiedMessage args)
  19. {
  20. if (!(HasComp<StorageComponent>(uid)
  21. && TryComp<AppearanceComponent>(uid, out var appearance)
  22. && TryComp<ItemComponent>(uid, out var item)))
  23. {
  24. return;
  25. }
  26. if (_storage.GetCumulativeItemAreas(uid) >= comp.Threshold)
  27. {
  28. _item.SetHeldPrefix(uid, "full", component: item);
  29. _appearance.SetData(uid, ToggleVisuals.Toggled, true, appearance);
  30. }
  31. else
  32. {
  33. _item.SetHeldPrefix(uid, "empty", component: item);
  34. _appearance.SetData(uid, ToggleVisuals.Toggled, false, appearance);
  35. }
  36. }
  37. }