1
0

BuyerSpeciesCondition.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using Content.Shared.Humanoid;
  2. using Content.Shared.Store;
  3. using Content.Shared.Humanoid.Prototypes;
  4. using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Set;
  5. using Content.Shared.Mind;
  6. namespace Content.Server.Store.Conditions;
  7. /// <summary>
  8. /// Allows a store entry to be filtered out based on the user's species.
  9. /// Supports both blacklists and whitelists.
  10. /// </summary>
  11. public sealed partial class BuyerSpeciesCondition : ListingCondition
  12. {
  13. /// <summary>
  14. /// A whitelist of species that can purchase this listing.
  15. /// </summary>
  16. [DataField("whitelist", customTypeSerializer: typeof(PrototypeIdHashSetSerializer<SpeciesPrototype>))]
  17. public HashSet<string>? Whitelist;
  18. /// <summary>
  19. /// A blacklist of species that cannot purchase this listing.
  20. /// </summary>
  21. [DataField("blacklist", customTypeSerializer: typeof(PrototypeIdHashSetSerializer<SpeciesPrototype>))]
  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 mind))
  27. return true; // needed to obtain body entityuid to check for humanoid appearance
  28. if (!ent.TryGetComponent<HumanoidAppearanceComponent>(mind.OwnedEntity, out var appearance))
  29. return true; // inanimate or non-humanoid entities should be handled elsewhere, main example being surplus crates
  30. if (Blacklist != null)
  31. {
  32. if (Blacklist.Contains(appearance.Species))
  33. return false;
  34. }
  35. if (Whitelist != null)
  36. {
  37. if (!Whitelist.Contains(appearance.Species))
  38. return false;
  39. }
  40. return true;
  41. }
  42. }