SolutionSpikerSystem.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Content.Shared.Popups;
  2. using Content.Shared.Chemistry.Components;
  3. using Content.Shared.Chemistry.Components.SolutionManager;
  4. using Content.Shared.Interaction;
  5. namespace Content.Shared.Chemistry.EntitySystems;
  6. /// <summary>
  7. /// Entity system used to handle when solution containers are 'spiked'
  8. /// with another entity. Triggers the source entity afterwards.
  9. /// Uses refillable solution as the target solution, as that indicates
  10. /// 'easy' refills.
  11. ///
  12. /// Examples of spikable entity interactions include pills being dropped into glasses,
  13. /// eggs being cracked into bowls, and so on.
  14. /// </summary>
  15. public sealed class SolutionSpikerSystem : EntitySystem
  16. {
  17. [Dependency] private readonly SharedPopupSystem _popup = default!;
  18. [Dependency] private readonly SharedSolutionContainerSystem _solution = default!;
  19. public override void Initialize()
  20. {
  21. SubscribeLocalEvent<RefillableSolutionComponent, InteractUsingEvent>(OnInteractUsing);
  22. }
  23. private void OnInteractUsing(Entity<RefillableSolutionComponent> entity, ref InteractUsingEvent args)
  24. {
  25. if (TrySpike(args.Used, args.Target, args.User, entity.Comp))
  26. args.Handled = true;
  27. }
  28. /// <summary>
  29. /// Immediately transfer all reagents from this entity, to the other entity.
  30. /// The source entity will then be acted on by TriggerSystem.
  31. /// </summary>
  32. /// <param name="source">Source of the solution.</param>
  33. /// <param name="target">Target to spike with the solution from source.</param>
  34. /// <param name="user">User spiking the target solution.</param>
  35. private bool TrySpike(EntityUid source, EntityUid target, EntityUid user, RefillableSolutionComponent? spikableTarget = null,
  36. SolutionSpikerComponent? spikableSource = null,
  37. SolutionContainerManagerComponent? managerSource = null,
  38. SolutionContainerManagerComponent? managerTarget = null)
  39. {
  40. if (!Resolve(source, ref spikableSource, ref managerSource, false)
  41. || !Resolve(target, ref spikableTarget, ref managerTarget, false)
  42. || !_solution.TryGetRefillableSolution((target, spikableTarget, managerTarget), out var targetSoln, out var targetSolution)
  43. || !_solution.TryGetSolution((source, managerSource), spikableSource.SourceSolution, out _, out var sourceSolution))
  44. {
  45. return false;
  46. }
  47. if (targetSolution.Volume == 0 && !spikableSource.IgnoreEmpty)
  48. {
  49. _popup.PopupClient(Loc.GetString(spikableSource.PopupEmpty, ("spiked-entity", target), ("spike-entity", source)), user, user);
  50. return false;
  51. }
  52. if (!_solution.ForceAddSolution(targetSoln.Value, sourceSolution))
  53. return false;
  54. _popup.PopupClient(Loc.GetString(spikableSource.Popup, ("spiked-entity", target), ("spike-entity", source)), user, user);
  55. sourceSolution.RemoveAllSolution();
  56. if (spikableSource.Delete)
  57. QueueDel(source);
  58. return true;
  59. }
  60. }