ContrabandSystem.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Linq;
  2. using Content.Shared.Access.Systems;
  3. using Content.Shared.CCVar;
  4. using Content.Shared.Examine;
  5. using Content.Shared.Localizations;
  6. using Content.Shared.Roles;
  7. using Content.Shared.Verbs;
  8. using Robust.Shared.Configuration;
  9. using Robust.Shared.Prototypes;
  10. using Robust.Shared.Utility;
  11. namespace Content.Shared.Contraband;
  12. /// <summary>
  13. /// This handles showing examine messages for contraband-marked items.
  14. /// </summary>
  15. public sealed class ContrabandSystem : EntitySystem
  16. {
  17. [Dependency] private readonly IConfigurationManager _configuration = default!;
  18. [Dependency] private readonly IPrototypeManager _proto = default!;
  19. [Dependency] private readonly SharedIdCardSystem _id = default!;
  20. [Dependency] private readonly ExamineSystemShared _examine = default!;
  21. private bool _contrabandExamineEnabled;
  22. /// <inheritdoc/>
  23. public override void Initialize()
  24. {
  25. SubscribeLocalEvent<ContrabandComponent, GetVerbsEvent<ExamineVerb>>(OnDetailedExamine);
  26. Subs.CVar(_configuration, CCVars.ContrabandExamine, SetContrabandExamine, true);
  27. }
  28. public void CopyDetails(EntityUid uid, ContrabandComponent other, ContrabandComponent? contraband = null)
  29. {
  30. if (!Resolve(uid, ref contraband))
  31. return;
  32. contraband.Severity = other.Severity;
  33. contraband.AllowedDepartments = other.AllowedDepartments;
  34. contraband.AllowedJobs = other.AllowedJobs;
  35. Dirty(uid, contraband);
  36. }
  37. private void OnDetailedExamine(EntityUid ent,ContrabandComponent component, ref GetVerbsEvent<ExamineVerb> args)
  38. {
  39. if (!_contrabandExamineEnabled)
  40. return;
  41. // CanAccess is not used here, because we want people to be able to examine legality in strip menu.
  42. if (!args.CanInteract)
  43. return;
  44. // two strings:
  45. // one, the actual informative 'this is restricted'
  46. // then, the 'you can/shouldn't carry this around' based on the ID the user is wearing
  47. var localizedDepartments = component.AllowedDepartments.Select(p => Loc.GetString("contraband-department-plural", ("department", Loc.GetString(_proto.Index(p).Name))));
  48. var jobs = component.AllowedJobs.Select(p => _proto.Index(p).LocalizedName).ToArray();
  49. var localizedJobs = jobs.Select(p => Loc.GetString("contraband-job-plural", ("job", p)));
  50. var severity = _proto.Index(component.Severity);
  51. String departmentExamineMessage;
  52. if (severity.ShowDepartmentsAndJobs)
  53. {
  54. //creating a combined list of jobs and departments for the restricted text
  55. var list = ContentLocalizationManager.FormatList(localizedDepartments.Concat(localizedJobs).ToList());
  56. // department restricted text
  57. departmentExamineMessage = Loc.GetString("contraband-examine-text-Restricted-department", ("departments", list));
  58. }
  59. else
  60. {
  61. departmentExamineMessage = Loc.GetString(severity.ExamineText);
  62. }
  63. // text based on ID card
  64. List<ProtoId<DepartmentPrototype>> departments = new();
  65. var jobId = "";
  66. if (_id.TryFindIdCard(args.User, out var id))
  67. {
  68. departments = id.Comp.JobDepartments;
  69. if (id.Comp.LocalizedJobTitle is not null)
  70. {
  71. jobId = id.Comp.LocalizedJobTitle;
  72. }
  73. }
  74. String carryingMessage;
  75. // either its fully restricted, you have no departments, or your departments dont intersect with the restricted departments
  76. if (departments.Intersect(component.AllowedDepartments).Any()
  77. || jobs.Contains(jobId))
  78. {
  79. carryingMessage = Loc.GetString("contraband-examine-text-in-the-clear");
  80. }
  81. else
  82. {
  83. // otherwise fine to use :tm:
  84. carryingMessage = Loc.GetString("contraband-examine-text-avoid-carrying-around");
  85. }
  86. var examineMarkup = GetContrabandExamine(departmentExamineMessage, carryingMessage);
  87. _examine.AddDetailedExamineVerb(args,
  88. component,
  89. examineMarkup,
  90. Loc.GetString("contraband-examinable-verb-text"),
  91. "/Textures/Interface/VerbIcons/lock.svg.192dpi.png",
  92. Loc.GetString("contraband-examinable-verb-message"));
  93. }
  94. private FormattedMessage GetContrabandExamine(String deptMessage, String carryMessage)
  95. {
  96. var msg = new FormattedMessage();
  97. msg.AddMarkupOrThrow(deptMessage);
  98. msg.PushNewline();
  99. msg.AddMarkupOrThrow(carryMessage);
  100. return msg;
  101. }
  102. private void SetContrabandExamine(bool val)
  103. {
  104. _contrabandExamineEnabled = val;
  105. }
  106. }