StationRecordKeyStorageSystem.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Robust.Shared.GameStates;
  2. namespace Content.Shared.StationRecords;
  3. public sealed class StationRecordKeyStorageSystem : EntitySystem
  4. {
  5. [Dependency] private readonly SharedStationRecordsSystem _records = default!;
  6. public override void Initialize()
  7. {
  8. base.Initialize();
  9. SubscribeLocalEvent<StationRecordKeyStorageComponent, ComponentGetState>(OnGetState);
  10. SubscribeLocalEvent<StationRecordKeyStorageComponent, ComponentHandleState>(OnHandleState);
  11. }
  12. private void OnGetState(EntityUid uid, StationRecordKeyStorageComponent component, ref ComponentGetState args)
  13. {
  14. args.State = new StationRecordKeyStorageComponentState(_records.Convert(component.Key));
  15. }
  16. private void OnHandleState(EntityUid uid, StationRecordKeyStorageComponent component, ref ComponentHandleState args)
  17. {
  18. if (args.Current is not StationRecordKeyStorageComponentState state)
  19. return;
  20. component.Key = _records.Convert(state.Key);
  21. }
  22. /// <summary>
  23. /// Assigns a station record key to an entity.
  24. /// </summary>
  25. /// <param name="uid"></param>
  26. /// <param name="key"></param>
  27. /// <param name="keyStorage"></param>
  28. public void AssignKey(EntityUid uid, StationRecordKey key, StationRecordKeyStorageComponent? keyStorage = null)
  29. {
  30. if (!Resolve(uid, ref keyStorage))
  31. {
  32. return;
  33. }
  34. keyStorage.Key = key;
  35. Dirty(uid, keyStorage);
  36. }
  37. /// <summary>
  38. /// Removes a station record key from an entity.
  39. /// </summary>
  40. /// <param name="uid"></param>
  41. /// <param name="keyStorage"></param>
  42. /// <returns></returns>
  43. public StationRecordKey? RemoveKey(EntityUid uid, StationRecordKeyStorageComponent? keyStorage = null)
  44. {
  45. if (!Resolve(uid, ref keyStorage) || keyStorage.Key == null)
  46. {
  47. return null;
  48. }
  49. var key = keyStorage.Key;
  50. keyStorage.Key = null;
  51. Dirty(uid, keyStorage);
  52. return key;
  53. }
  54. /// <summary>
  55. /// Checks if an entity currently contains a station record key.
  56. /// </summary>
  57. /// <param name="uid"></param>
  58. /// <param name="keyStorage"></param>
  59. /// <returns></returns>
  60. public bool CheckKey(EntityUid uid, StationRecordKeyStorageComponent? keyStorage = null)
  61. {
  62. if (!Resolve(uid, ref keyStorage))
  63. {
  64. return false;
  65. }
  66. return keyStorage.Key != null;
  67. }
  68. }