RandomGiftSystem.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Content.Server.Administration.Logs;
  2. using Content.Server.Hands.Systems;
  3. using Content.Shared.Database;
  4. using Content.Shared.Examine;
  5. using Content.Shared.Interaction.Events;
  6. using Content.Shared.Item;
  7. using Content.Shared.Whitelist;
  8. using Robust.Server.Audio;
  9. using Robust.Shared.Map.Components;
  10. using Robust.Shared.Physics.Components;
  11. using Robust.Shared.Prototypes;
  12. using Robust.Shared.Random;
  13. namespace Content.Server.Holiday.Christmas;
  14. /// <summary>
  15. /// This handles granting players their gift.
  16. /// </summary>
  17. public sealed class RandomGiftSystem : EntitySystem
  18. {
  19. [Dependency] private readonly AudioSystem _audio = default!;
  20. [Dependency] private readonly HandsSystem _hands = default!;
  21. [Dependency] private readonly IComponentFactory _componentFactory = default!;
  22. [Dependency] private readonly IPrototypeManager _prototype = default!;
  23. [Dependency] private readonly IRobustRandom _random = default!;
  24. [Dependency] private readonly IAdminLogManager _adminLogger = default!;
  25. [Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
  26. [Dependency] private readonly SharedTransformSystem _transform = default!;
  27. private readonly List<string> _possibleGiftsSafe = new();
  28. private readonly List<string> _possibleGiftsUnsafe = new();
  29. /// <inheritdoc/>
  30. public override void Initialize()
  31. {
  32. SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnPrototypesReloaded);
  33. SubscribeLocalEvent<RandomGiftComponent, MapInitEvent>(OnGiftMapInit);
  34. SubscribeLocalEvent<RandomGiftComponent, UseInHandEvent>(OnUseInHand);
  35. SubscribeLocalEvent<RandomGiftComponent, ExaminedEvent>(OnExamined);
  36. BuildIndex();
  37. }
  38. private void OnExamined(EntityUid uid, RandomGiftComponent component, ExaminedEvent args)
  39. {
  40. if (_whitelistSystem.IsWhitelistFail(component.ContentsViewers, args.Examiner) || component.SelectedEntity is null)
  41. return;
  42. var name = _prototype.Index<EntityPrototype>(component.SelectedEntity).Name;
  43. args.PushText(Loc.GetString("gift-packin-contains", ("name", name)));
  44. }
  45. private void OnUseInHand(EntityUid uid, RandomGiftComponent component, UseInHandEvent args)
  46. {
  47. if (args.Handled)
  48. return;
  49. if (component.SelectedEntity is null)
  50. return;
  51. var coords = Transform(args.User).Coordinates;
  52. var handsEnt = Spawn(component.SelectedEntity, coords);
  53. _adminLogger.Add(LogType.EntitySpawn, LogImpact.Low, $"{ToPrettyString(args.User)} used {ToPrettyString(uid)} which spawned {ToPrettyString(handsEnt)}");
  54. if (component.Wrapper is not null)
  55. Spawn(component.Wrapper, coords);
  56. _audio.PlayPvs(component.Sound, args.User);
  57. // Don't delete the entity in the event bus, so we queue it for deletion.
  58. // We need the free hand for the new item, so we send it to nullspace.
  59. _transform.DetachEntity(uid, Transform(uid));
  60. QueueDel(uid);
  61. _hands.PickupOrDrop(args.User, handsEnt);
  62. args.Handled = true;
  63. }
  64. private void OnGiftMapInit(EntityUid uid, RandomGiftComponent component, MapInitEvent args)
  65. {
  66. if (component.InsaneMode)
  67. component.SelectedEntity = _random.Pick(_possibleGiftsUnsafe);
  68. else
  69. component.SelectedEntity = _random.Pick(_possibleGiftsSafe);
  70. }
  71. private void OnPrototypesReloaded(PrototypesReloadedEventArgs obj)
  72. {
  73. if (obj.WasModified<EntityPrototype>())
  74. BuildIndex();
  75. }
  76. private void BuildIndex()
  77. {
  78. _possibleGiftsSafe.Clear();
  79. _possibleGiftsUnsafe.Clear();
  80. var itemCompName = _componentFactory.GetComponentName(typeof(ItemComponent));
  81. var mapGridCompName = _componentFactory.GetComponentName(typeof(MapGridComponent));
  82. var physicsCompName = _componentFactory.GetComponentName(typeof(PhysicsComponent));
  83. foreach (var proto in _prototype.EnumeratePrototypes<EntityPrototype>())
  84. {
  85. if (proto.Abstract || proto.HideSpawnMenu || proto.Components.ContainsKey(mapGridCompName) || !proto.Components.ContainsKey(physicsCompName))
  86. continue;
  87. _possibleGiftsUnsafe.Add(proto.ID);
  88. if (!proto.Components.ContainsKey(itemCompName))
  89. continue;
  90. _possibleGiftsSafe.Add(proto.ID);
  91. }
  92. }
  93. }