BuyerDepartmentCondition.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using Content.Shared.Mind;
  2. using Content.Shared.Roles;
  3. using Content.Shared.Roles.Jobs;
  4. using Content.Shared.Store;
  5. using Robust.Shared.Prototypes;
  6. using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Set;
  7. namespace Content.Server.Store.Conditions;
  8. /// <summary>
  9. /// Allows a store entry to be filtered out based on the user's job.
  10. /// Supports both blacklists and whitelists
  11. /// </summary>
  12. public sealed partial class BuyerDepartmentCondition : ListingCondition
  13. {
  14. /// <summary>
  15. /// A whitelist of department prototypes that can purchase this listing. Only one needs to be found.
  16. /// </summary>
  17. [DataField("whitelist", customTypeSerializer: typeof(PrototypeIdHashSetSerializer<DepartmentPrototype>))]
  18. public HashSet<string>? Whitelist;
  19. /// <summary>
  20. /// A blacklist of department prototypes that can purchase this listing. Only one needs to be found.
  21. /// </summary>
  22. [DataField("blacklist", customTypeSerializer: typeof(PrototypeIdHashSetSerializer<DepartmentPrototype>))]
  23. public HashSet<string>? Blacklist;
  24. public override bool Condition(ListingConditionArgs args)
  25. {
  26. var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
  27. var ent = args.EntityManager;
  28. if (!ent.TryGetComponent<MindComponent>(args.Buyer, out var _))
  29. return true; // inanimate objects don't have minds
  30. var jobs = ent.System<SharedJobSystem>();
  31. jobs.MindTryGetJob(args.Buyer, out var job);
  32. if (Blacklist != null && job != null)
  33. {
  34. foreach (var department in prototypeManager.EnumeratePrototypes<DepartmentPrototype>())
  35. {
  36. if (department.Roles.Contains(job.ID) && Blacklist.Contains(department.ID))
  37. return false;
  38. }
  39. }
  40. if (Whitelist != null)
  41. {
  42. var found = false;
  43. if (job != null)
  44. {
  45. foreach (var department in prototypeManager.EnumeratePrototypes<DepartmentPrototype>())
  46. {
  47. if (department.Roles.Contains(job.ID) && Whitelist.Contains(department.ID))
  48. {
  49. found = true;
  50. break;
  51. }
  52. }
  53. }
  54. if (!found)
  55. return false;
  56. }
  57. return true;
  58. }
  59. }