SeedExtractorSystem.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Content.Server.Botany.Components;
  2. using Content.Server.Popups;
  3. using Content.Server.Power.EntitySystems;
  4. using Content.Shared.Interaction;
  5. using Content.Shared.Popups;
  6. using Robust.Shared.Random;
  7. namespace Content.Server.Botany.Systems;
  8. public sealed class SeedExtractorSystem : EntitySystem
  9. {
  10. [Dependency] private readonly IRobustRandom _random = default!;
  11. [Dependency] private readonly PopupSystem _popupSystem = default!;
  12. [Dependency] private readonly BotanySystem _botanySystem = default!;
  13. public override void Initialize()
  14. {
  15. base.Initialize();
  16. SubscribeLocalEvent<SeedExtractorComponent, InteractUsingEvent>(OnInteractUsing);
  17. }
  18. private void OnInteractUsing(EntityUid uid, SeedExtractorComponent seedExtractor, InteractUsingEvent args)
  19. {
  20. if (!this.IsPowered(uid, EntityManager))
  21. return;
  22. if (!TryComp(args.Used, out ProduceComponent? produce))
  23. return;
  24. if (!_botanySystem.TryGetSeed(produce, out var seed) || seed.Seedless)
  25. {
  26. _popupSystem.PopupCursor(Loc.GetString("seed-extractor-component-no-seeds", ("name", args.Used)),
  27. args.User, PopupType.MediumCaution);
  28. return;
  29. }
  30. _popupSystem.PopupCursor(Loc.GetString("seed-extractor-component-interact-message", ("name", args.Used)),
  31. args.User, PopupType.Medium);
  32. QueueDel(args.Used);
  33. args.Handled = true;
  34. var amount = _random.Next(seedExtractor.BaseMinSeeds, seedExtractor.BaseMaxSeeds + 1);
  35. var coords = Transform(uid).Coordinates;
  36. var packetSeed = seed;
  37. if (amount > 1)
  38. packetSeed.Unique = false;
  39. for (var i = 0; i < amount; i++)
  40. {
  41. _botanySystem.SpawnSeedPacket(packetSeed, coords, args.User);
  42. }
  43. }
  44. }