SharedRestrictSystem.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // SPDX-FileCopyrightText: 2025 Aiden <28298836+Aidenkrz@users.noreply.github.com>
  2. // SPDX-FileCopyrightText: 2025 GoobBot <uristmchands@proton.me>
  3. // SPDX-FileCopyrightText: 2025 Piras314 <p1r4s@proton.me>
  4. // SPDX-FileCopyrightText: 2025 gluesniffler <159397573+gluesniffler@users.noreply.github.com>
  5. // SPDX-FileCopyrightText: 2025 pheenty <fedorlukin2006@gmail.com>
  6. //
  7. // SPDX-License-Identifier: AGPL-3.0-or-later
  8. using Content.Shared.Interaction;
  9. using Content.Shared.Interaction.Events;
  10. using Content.Shared.Popups;
  11. using Content.Shared.Tag;
  12. using Content.Shared.Weapons.Melee.Events;
  13. using Content.Shared.Weapons.Ranged.Events;
  14. using Robust.Shared.Random;
  15. using Robust.Shared.Timing;
  16. namespace Content.Shared._Shitmed.Restrict;
  17. public sealed partial class SharedRestrictSystem : EntitySystem
  18. {
  19. [Dependency] private readonly TagSystem _tagSystem = default!;
  20. [Dependency] private readonly IRobustRandom _random = default!;
  21. [Dependency] private readonly SharedPopupSystem _popup = default!;
  22. [Dependency] private readonly IGameTiming _timing = default!;
  23. public override void Initialize()
  24. {
  25. SubscribeLocalEvent<RestrictInteractionByUserTagComponent, BeforeRangedInteractEvent>(OnAttemptInteract);
  26. SubscribeLocalEvent<RestrictMeleeByUserTagComponent, AttemptMeleeEvent>(OnAttemptMelee);
  27. SubscribeLocalEvent<RestrictGunshotsByUserTagComponent, ShotAttemptedEvent>(OnAttemptGunshot);
  28. }
  29. private void OnAttemptInteract(Entity<RestrictInteractionByUserTagComponent> ent, ref BeforeRangedInteractEvent args)
  30. {
  31. if (!_tagSystem.HasAllTags(args.User, ent.Comp.Contains) || _tagSystem.HasAnyTag(args.User, ent.Comp.DoesntContain))
  32. {
  33. if (ent.Comp.Messages.Count != 0)
  34. _popup.PopupClient(Loc.GetString(_random.Pick(ent.Comp.Messages)), args.User);
  35. args.Handled = true;
  36. }
  37. }
  38. private void OnAttemptMelee(Entity<RestrictMeleeByUserTagComponent> ent, ref AttemptMeleeEvent args)
  39. {
  40. if(!_tagSystem.HasAllTags(args.User, ent.Comp.Contains) || _tagSystem.HasAnyTag(args.User, ent.Comp.DoesntContain))
  41. {
  42. if(ent.Comp.Messages.Count != 0)
  43. args.Message = Loc.GetString(_random.Pick(ent.Comp.Messages));
  44. args.Cancelled = true;
  45. }
  46. }
  47. private void OnAttemptGunshot(Entity<RestrictGunshotsByUserTagComponent> ent, ref ShotAttemptedEvent args)
  48. {
  49. if(!_tagSystem.HasAllTags(args.User, ent.Comp.Contains) || _tagSystem.HasAnyTag(args.User, ent.Comp.DoesntContain))
  50. {
  51. var time = _timing.CurTime;
  52. if(ent.Comp.Messages.Count != 0 && time > ent.Comp.LastPopup + TimeSpan.FromSeconds(1))
  53. {
  54. ent.Comp.LastPopup = time;
  55. _popup.PopupClient(Loc.GetString(_random.Pick(ent.Comp.Messages)), args.User);
  56. }
  57. args.Cancel();
  58. }
  59. }
  60. }