LimitedItemGiverSystem.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Content.Server.Hands.Systems;
  2. using Content.Server.Popups;
  3. using Content.Shared.Interaction;
  4. using Content.Shared.Storage;
  5. using Robust.Shared.Player;
  6. namespace Content.Server.Holiday.Christmas;
  7. /// <summary>
  8. /// This handles handing out items from item givers.
  9. /// </summary>
  10. public sealed class LimitedItemGiverSystem : EntitySystem
  11. {
  12. [Dependency] private readonly HandsSystem _hands = default!;
  13. [Dependency] private readonly HolidaySystem _holiday = default!;
  14. [Dependency] private readonly PopupSystem _popup = default!;
  15. /// <inheritdoc/>
  16. public override void Initialize()
  17. {
  18. SubscribeLocalEvent<LimitedItemGiverComponent, InteractHandEvent>(OnInteractHand);
  19. }
  20. private void OnInteractHand(EntityUid uid, LimitedItemGiverComponent component, InteractHandEvent args)
  21. {
  22. if (!TryComp<ActorComponent>(args.User, out var actor))
  23. return;
  24. if (component.GrantedPlayers.Contains(actor.PlayerSession.UserId) || (component.RequiredHoliday is not null && !_holiday.IsCurrentlyHoliday(component.RequiredHoliday)))
  25. {
  26. _popup.PopupEntity(Loc.GetString(component.DeniedPopup), uid, args.User);
  27. return;
  28. }
  29. var toGive = EntitySpawnCollection.GetSpawns(component.SpawnEntries);
  30. var coords = Transform(args.User).Coordinates;
  31. foreach (var item in toGive)
  32. {
  33. if (item is null)
  34. continue;
  35. var spawned = Spawn(item, coords);
  36. _hands.PickupOrDrop(args.User, spawned);
  37. }
  38. component.GrantedPlayers.Add(actor.PlayerSession.UserId);
  39. _popup.PopupEntity(Loc.GetString(component.ReceivedPopup), uid, args.User);
  40. }
  41. }