1
0

BuyBeforeCondition.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Content.Shared.Store;
  2. using Content.Shared.Store.Components;
  3. using Robust.Shared.Prototypes;
  4. namespace Content.Server.Store.Conditions;
  5. public sealed partial class BuyBeforeCondition : ListingCondition
  6. {
  7. /// <summary>
  8. /// Required listing(s) needed to purchase before this listing is available
  9. /// </summary>
  10. [DataField(required: true)]
  11. public HashSet<ProtoId<ListingPrototype>> Whitelist;
  12. /// <summary>
  13. /// Listing(s) that if bought, block this purchase, if any.
  14. /// </summary>
  15. public HashSet<ProtoId<ListingPrototype>>? Blacklist;
  16. public override bool Condition(ListingConditionArgs args)
  17. {
  18. if (!args.EntityManager.TryGetComponent<StoreComponent>(args.StoreEntity, out var storeComp))
  19. return false;
  20. var allListings = storeComp.FullListingsCatalog;
  21. var purchasesFound = false;
  22. if (Blacklist != null)
  23. {
  24. foreach (var blacklistListing in Blacklist)
  25. {
  26. foreach (var listing in allListings)
  27. {
  28. if (listing.ID == blacklistListing.Id && listing.PurchaseAmount > 0)
  29. return false;
  30. }
  31. }
  32. }
  33. foreach (var requiredListing in Whitelist)
  34. {
  35. foreach (var listing in allListings)
  36. {
  37. if (listing.ID == requiredListing.Id)
  38. {
  39. purchasesFound = listing.PurchaseAmount > 0;
  40. break;
  41. }
  42. }
  43. }
  44. return purchasesFound;
  45. }
  46. }