ScoopableSolutionSystem.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using Content.Shared.Chemistry.Components;
  2. using Content.Shared.Interaction;
  3. using Content.Shared.Popups;
  4. using Robust.Shared.Network;
  5. namespace Content.Shared.Chemistry.EntitySystems;
  6. /// <summary>
  7. /// Handles solution transfer when a beaker is used on a scoopable entity.
  8. /// </summary>
  9. public sealed class ScoopableSolutionSystem : EntitySystem
  10. {
  11. [Dependency] private readonly INetManager _netManager = default!;
  12. [Dependency] private readonly SharedPopupSystem _popup = default!;
  13. [Dependency] private readonly SharedSolutionContainerSystem _solution = default!;
  14. [Dependency] private readonly SolutionTransferSystem _solutionTransfer = default!;
  15. public override void Initialize()
  16. {
  17. base.Initialize();
  18. SubscribeLocalEvent<ScoopableSolutionComponent, InteractUsingEvent>(OnInteractUsing);
  19. }
  20. private void OnInteractUsing(Entity<ScoopableSolutionComponent> ent, ref InteractUsingEvent args)
  21. {
  22. if (args.Handled)
  23. return;
  24. args.Handled = TryScoop(ent, args.Used, args.User);
  25. }
  26. public bool TryScoop(Entity<ScoopableSolutionComponent> ent, EntityUid beaker, EntityUid user)
  27. {
  28. if (!_solution.TryGetSolution(ent.Owner, ent.Comp.Solution, out var src, out var srcSolution) ||
  29. !_solution.TryGetRefillableSolution(beaker, out var target, out _))
  30. return false;
  31. var scooped = _solutionTransfer.Transfer(user, ent, src.Value, beaker, target.Value, srcSolution.Volume);
  32. if (scooped == 0)
  33. return false;
  34. _popup.PopupClient(Loc.GetString(ent.Comp.Popup, ("scooped", ent.Owner), ("beaker", beaker)), user, user);
  35. if (srcSolution.Volume == 0 && ent.Comp.Delete)
  36. {
  37. // deletion isnt predicted so do this to prevent spam clicking to see "the ash is empty!"
  38. RemCompDeferred<ScoopableSolutionComponent>(ent);
  39. if (!_netManager.IsClient)
  40. QueueDel(ent);
  41. }
  42. return true;
  43. }
  44. }