BuyerJobCondition.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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.Serialization.TypeSerializers.Implementations.Custom.Prototype.Set;
  6. namespace Content.Server.Store.Conditions;
  7. /// <summary>
  8. /// Allows a store entry to be filtered out based on the user's job.
  9. /// Supports both blacklists and whitelists
  10. /// </summary>
  11. public sealed partial class BuyerJobCondition : ListingCondition
  12. {
  13. /// <summary>
  14. /// A whitelist of jobs prototypes that can purchase this listing. Only one needs to be found.
  15. /// </summary>
  16. [DataField("whitelist", customTypeSerializer: typeof(PrototypeIdHashSetSerializer<JobPrototype>))]
  17. public HashSet<string>? Whitelist;
  18. /// <summary>
  19. /// A blacklist of job prototypes that can purchase this listing. Only one needs to be found.
  20. /// </summary>
  21. [DataField("blacklist", customTypeSerializer: typeof(PrototypeIdHashSetSerializer<JobPrototype>))]
  22. public HashSet<string>? Blacklist;
  23. public override bool Condition(ListingConditionArgs args)
  24. {
  25. var ent = args.EntityManager;
  26. if (!ent.TryGetComponent<MindComponent>(args.Buyer, out var _))
  27. return true; // inanimate objects don't have minds
  28. var jobs = ent.System<SharedJobSystem>();
  29. jobs.MindTryGetJob(args.Buyer, out var job);
  30. if (Blacklist != null)
  31. {
  32. if (job is not null && Blacklist.Contains(job.ID))
  33. return false;
  34. }
  35. if (Whitelist != null)
  36. {
  37. if (job == null || !Whitelist.Contains(job.ID))
  38. return false;
  39. }
  40. return true;
  41. }
  42. }