ProduceMaterialExtractorSystem.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.Linq;
  2. using Content.Server.Botany.Components;
  3. using Content.Server.Materials.Components;
  4. using Content.Server.Power.EntitySystems;
  5. using Content.Shared.Chemistry.EntitySystems;
  6. using Content.Shared.Interaction;
  7. using Robust.Server.Audio;
  8. namespace Content.Server.Materials;
  9. public sealed class ProduceMaterialExtractorSystem : EntitySystem
  10. {
  11. [Dependency] private readonly AudioSystem _audio = default!;
  12. [Dependency] private readonly MaterialStorageSystem _materialStorage = default!;
  13. [Dependency] private readonly SharedSolutionContainerSystem _solutionContainer = default!;
  14. /// <inheritdoc/>
  15. public override void Initialize()
  16. {
  17. SubscribeLocalEvent<ProduceMaterialExtractorComponent, AfterInteractUsingEvent>(OnInteractUsing);
  18. }
  19. private void OnInteractUsing(Entity<ProduceMaterialExtractorComponent> ent, ref AfterInteractUsingEvent args)
  20. {
  21. if (args.Handled)
  22. return;
  23. if (!this.IsPowered(ent, EntityManager))
  24. return;
  25. if (!TryComp<ProduceComponent>(args.Used, out var produce))
  26. return;
  27. if (!_solutionContainer.TryGetSolution(args.Used, produce.SolutionName, out var solution))
  28. return;
  29. // Can produce even have fractional amounts? Does it matter if they do?
  30. // Questions man was never meant to answer.
  31. var matAmount = solution.Value.Comp.Solution.Contents
  32. .Where(r => ent.Comp.ExtractionReagents.Contains(r.Reagent.Prototype))
  33. .Sum(r => r.Quantity.Float());
  34. _materialStorage.TryChangeMaterialAmount(ent, ent.Comp.ExtractedMaterial, (int) matAmount);
  35. _audio.PlayPvs(ent.Comp.ExtractSound, ent);
  36. QueueDel(args.Used);
  37. args.Handled = true;
  38. }
  39. }