1
0

PresetIdCardSystem.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using Content.Server.Access.Components;
  2. using Content.Server.GameTicking;
  3. using Content.Server.Station.Components;
  4. using Content.Server.Station.Systems;
  5. using Content.Shared.Access.Systems;
  6. using Content.Shared.Roles;
  7. using Content.Shared.StatusIcon;
  8. using Robust.Shared.Prototypes;
  9. namespace Content.Server.Access.Systems;
  10. public sealed class PresetIdCardSystem : EntitySystem
  11. {
  12. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  13. [Dependency] private readonly IdCardSystem _cardSystem = default!;
  14. [Dependency] private readonly SharedAccessSystem _accessSystem = default!;
  15. [Dependency] private readonly StationSystem _stationSystem = default!;
  16. public override void Initialize()
  17. {
  18. SubscribeLocalEvent<PresetIdCardComponent, MapInitEvent>(OnMapInit);
  19. SubscribeLocalEvent<RulePlayerJobsAssignedEvent>(PlayerJobsAssigned);
  20. }
  21. private void PlayerJobsAssigned(RulePlayerJobsAssignedEvent ev)
  22. {
  23. // Go over all ID cards and make sure they're correctly configured for extended access.
  24. var query = EntityQueryEnumerator<PresetIdCardComponent>();
  25. while (query.MoveNext(out var uid, out var card))
  26. {
  27. var station = _stationSystem.GetOwningStation(uid);
  28. // If we're not on an extended access station, the ID is already configured correctly from MapInit.
  29. if (station == null || !TryComp<StationJobsComponent>(station.Value, out var jobsComp) || !jobsComp.ExtendedAccess)
  30. continue;
  31. SetupIdAccess(uid, card, true);
  32. SetupIdName(uid, card);
  33. }
  34. }
  35. private void OnMapInit(EntityUid uid, PresetIdCardComponent id, MapInitEvent args)
  36. {
  37. // If a preset ID card is spawned on a station at setup time,
  38. // the station may not exist,
  39. // or may not yet know whether it is on extended access (players not spawned yet).
  40. // PlayerJobsAssigned makes sure extended access is configured correctly in that case.
  41. var station = _stationSystem.GetOwningStation(uid);
  42. var extended = false;
  43. // Station not guaranteed to have jobs (e.g. nukie outpost).
  44. if (TryComp(station, out StationJobsComponent? stationJobs))
  45. extended = stationJobs.ExtendedAccess;
  46. SetupIdAccess(uid, id, extended);
  47. SetupIdName(uid, id);
  48. }
  49. private void SetupIdName(EntityUid uid, PresetIdCardComponent id)
  50. {
  51. if (id.IdName == null)
  52. return;
  53. _cardSystem.TryChangeFullName(uid, id.IdName);
  54. }
  55. private void SetupIdAccess(EntityUid uid, PresetIdCardComponent id, bool extended)
  56. {
  57. if (id.JobName == null)
  58. return;
  59. if (!_prototypeManager.TryIndex(id.JobName, out JobPrototype? job))
  60. {
  61. Log.Error($"Invalid job id ({id.JobName}) for preset card");
  62. return;
  63. }
  64. _accessSystem.SetAccessToJob(uid, job, extended);
  65. _cardSystem.TryChangeJobTitle(uid, job.LocalizedName);
  66. _cardSystem.TryChangeJobDepartment(uid, job);
  67. if (_prototypeManager.TryIndex(job.Icon, out var jobIcon))
  68. _cardSystem.TryChangeJobIcon(uid, jobIcon);
  69. }
  70. }