StoreSystem.Refund.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using Content.Server.Store.Components;
  2. using Content.Shared.Actions.Events;
  3. using Content.Shared.Interaction.Events;
  4. using Content.Shared.Store.Components;
  5. using Content.Shared.Weapons.Ranged.Systems;
  6. using Robust.Shared.Containers;
  7. namespace Content.Server.Store.Systems;
  8. public sealed partial class StoreSystem
  9. {
  10. private void InitializeRefund()
  11. {
  12. SubscribeLocalEvent<StoreComponent, EntityTerminatingEvent>(OnStoreTerminating);
  13. SubscribeLocalEvent<StoreRefundComponent, EntityTerminatingEvent>(OnRefundTerminating);
  14. SubscribeLocalEvent<StoreRefundComponent, EntRemovedFromContainerMessage>(OnEntityRemoved);
  15. SubscribeLocalEvent<StoreRefundComponent, EntInsertedIntoContainerMessage>(OnEntityInserted);
  16. SubscribeLocalEvent<StoreRefundComponent, ActionPerformedEvent>(OnActionPerformed);
  17. SubscribeLocalEvent<StoreRefundComponent, UseInHandEvent>(OnUseInHand);
  18. SubscribeLocalEvent<StoreRefundComponent, AttemptShootEvent>(OnShootAttempt);
  19. // TODO: Handle guardian refund disabling when guardians support refunds.
  20. }
  21. private void OnEntityRemoved(Entity<StoreRefundComponent> ent, ref EntRemovedFromContainerMessage args)
  22. {
  23. CheckDisableRefund(ent);
  24. }
  25. private void OnEntityInserted(Entity<StoreRefundComponent> ent, ref EntInsertedIntoContainerMessage args)
  26. {
  27. CheckDisableRefund(ent);
  28. }
  29. private void OnActionPerformed(Entity<StoreRefundComponent> ent, ref ActionPerformedEvent args)
  30. {
  31. CheckDisableRefund(ent);
  32. }
  33. private void OnUseInHand(Entity<StoreRefundComponent> ent, ref UseInHandEvent args)
  34. {
  35. CheckDisableRefund(ent);
  36. }
  37. private void OnShootAttempt(Entity<StoreRefundComponent> ent, ref AttemptShootEvent args)
  38. {
  39. if (args.Cancelled)
  40. return;
  41. CheckDisableRefund(ent);
  42. }
  43. private void OnStoreTerminating(Entity<StoreComponent> ent, ref EntityTerminatingEvent args)
  44. {
  45. if (ent.Comp.BoughtEntities.Count <= 0)
  46. return;
  47. foreach (var boughtEnt in ent.Comp.BoughtEntities)
  48. {
  49. if (!TryComp<StoreRefundComponent>(boughtEnt, out var refundComp))
  50. continue;
  51. refundComp.StoreEntity = null;
  52. }
  53. }
  54. private void OnRefundTerminating(Entity<StoreRefundComponent> ent, ref EntityTerminatingEvent args)
  55. {
  56. if (ent.Comp.StoreEntity == null)
  57. return;
  58. var ev = new RefundEntityDeletedEvent(ent);
  59. RaiseLocalEvent(ent.Comp.StoreEntity.Value, ref ev);
  60. }
  61. private void CheckDisableRefund(Entity<StoreRefundComponent> ent)
  62. {
  63. var component = ent.Comp;
  64. if (component.StoreEntity == null || !TryComp<StoreComponent>(component.StoreEntity.Value, out var storeComp) || !storeComp.RefundAllowed)
  65. return;
  66. var endTime = component.BoughtTime + component.DisableTime;
  67. if (IsOnStartingMap(component.StoreEntity.Value, storeComp) && _timing.CurTime < endTime)
  68. return;
  69. DisableRefund(component.StoreEntity.Value, storeComp);
  70. }
  71. }