1
0

ThrowInsertContainerSystem.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Content.Server.Administration.Logs;
  2. using Content.Shared.Database;
  3. using Content.Shared.Popups;
  4. using Content.Shared.Throwing;
  5. using Robust.Shared.Audio.Systems;
  6. using Robust.Shared.Containers;
  7. using Robust.Shared.Random;
  8. namespace Content.Server.Containers;
  9. public sealed class ThrowInsertContainerSystem : EntitySystem
  10. {
  11. [Dependency] private readonly IAdminLogManager _adminLogger = default!;
  12. [Dependency] private readonly SharedAudioSystem _audio = default!;
  13. [Dependency] private readonly SharedContainerSystem _containerSystem = default!;
  14. [Dependency] private readonly SharedPopupSystem _popup = default!;
  15. [Dependency] private readonly IRobustRandom _random = default!;
  16. public override void Initialize()
  17. {
  18. base.Initialize();
  19. SubscribeLocalEvent<ThrowInsertContainerComponent, ThrowHitByEvent>(OnThrowCollide);
  20. }
  21. private void OnThrowCollide(Entity<ThrowInsertContainerComponent> ent, ref ThrowHitByEvent args)
  22. {
  23. var container = _containerSystem.GetContainer(ent, ent.Comp.ContainerId);
  24. if (!_containerSystem.CanInsert(args.Thrown, container))
  25. return;
  26. var beforeThrowArgs = new BeforeThrowInsertEvent(args.Thrown);
  27. RaiseLocalEvent(ent, ref beforeThrowArgs);
  28. if (beforeThrowArgs.Cancelled)
  29. return;
  30. if (_random.Prob(ent.Comp.Probability))
  31. {
  32. _audio.PlayPvs(ent.Comp.MissSound, ent);
  33. _popup.PopupEntity(Loc.GetString(ent.Comp.MissLocString), ent);
  34. return;
  35. }
  36. if (!_containerSystem.Insert(args.Thrown, container))
  37. throw new InvalidOperationException("Container insertion failed but CanInsert returned true");
  38. _audio.PlayPvs(ent.Comp.InsertSound, ent);
  39. if (args.Component.Thrower != null)
  40. _adminLogger.Add(LogType.Landed, LogImpact.Low, $"{ToPrettyString(args.Thrown)} thrown by {ToPrettyString(args.Component.Thrower.Value):player} landed in {ToPrettyString(ent)}");
  41. }
  42. }
  43. /// <summary>
  44. /// Sent before the insertion is made.
  45. /// Allows preventing the insertion if any system on the entity should need to.
  46. /// </summary>
  47. [ByRefEvent]
  48. public record struct BeforeThrowInsertEvent(EntityUid ThrownEntity, bool Cancelled = false);