SmokingSystem.SmokingPipe.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using Content.Server.Nutrition.Components;
  2. using Content.Shared.Chemistry.Components.SolutionManager;
  3. using Content.Shared.Containers.ItemSlots;
  4. using Content.Shared.Interaction;
  5. using Content.Shared.Nutrition.Components;
  6. using Content.Shared.Smoking;
  7. using Content.Shared.Temperature;
  8. namespace Content.Server.Nutrition.EntitySystems
  9. {
  10. public sealed partial class SmokingSystem
  11. {
  12. [Dependency] private readonly ItemSlotsSystem _itemSlotsSystem = default!;
  13. private void InitializePipes()
  14. {
  15. SubscribeLocalEvent<SmokingPipeComponent, InteractUsingEvent>(OnPipeInteractUsingEvent);
  16. SubscribeLocalEvent<SmokingPipeComponent, SmokableSolutionEmptyEvent>(OnPipeSolutionEmptyEvent);
  17. SubscribeLocalEvent<SmokingPipeComponent, AfterInteractEvent>(OnPipeAfterInteract);
  18. SubscribeLocalEvent<SmokingPipeComponent, ComponentInit>(OnComponentInit);
  19. }
  20. public void OnComponentInit(Entity<SmokingPipeComponent> entity, ref ComponentInit args)
  21. {
  22. _itemSlotsSystem.AddItemSlot(entity, SmokingPipeComponent.BowlSlotId, entity.Comp.BowlSlot);
  23. }
  24. private void OnPipeInteractUsingEvent(Entity<SmokingPipeComponent> entity, ref InteractUsingEvent args)
  25. {
  26. if (args.Handled)
  27. return;
  28. if (!EntityManager.TryGetComponent(entity, out SmokableComponent? smokable))
  29. return;
  30. if (smokable.State != SmokableState.Unlit)
  31. return;
  32. var isHotEvent = new IsHotEvent();
  33. RaiseLocalEvent(args.Used, isHotEvent, false);
  34. if (!isHotEvent.IsHot)
  35. return;
  36. if (TryTransferReagents(entity, (entity.Owner, smokable)))
  37. SetSmokableState(entity, SmokableState.Lit, smokable);
  38. args.Handled = true;
  39. }
  40. public void OnPipeAfterInteract(Entity<SmokingPipeComponent> entity, ref AfterInteractEvent args)
  41. {
  42. var targetEntity = args.Target;
  43. if (targetEntity == null ||
  44. !args.CanReach ||
  45. !EntityManager.TryGetComponent(entity, out SmokableComponent? smokable) ||
  46. smokable.State == SmokableState.Lit)
  47. return;
  48. var isHotEvent = new IsHotEvent();
  49. RaiseLocalEvent(targetEntity.Value, isHotEvent, true);
  50. if (!isHotEvent.IsHot)
  51. return;
  52. if (TryTransferReagents(entity, (entity.Owner, smokable)))
  53. SetSmokableState(entity, SmokableState.Lit, smokable);
  54. args.Handled = true;
  55. }
  56. private void OnPipeSolutionEmptyEvent(Entity<SmokingPipeComponent> entity, ref SmokableSolutionEmptyEvent args)
  57. {
  58. _itemSlotsSystem.SetLock(entity, entity.Comp.BowlSlot, false);
  59. SetSmokableState(entity, SmokableState.Unlit);
  60. }
  61. // Convert smokable item into reagents to be smoked
  62. private bool TryTransferReagents(Entity<SmokingPipeComponent> entity, Entity<SmokableComponent> smokable)
  63. {
  64. if (entity.Comp.BowlSlot.Item == null)
  65. return false;
  66. EntityUid contents = entity.Comp.BowlSlot.Item.Value;
  67. if (!TryComp<SolutionContainerManagerComponent>(contents, out var reagents) ||
  68. !_solutionContainerSystem.TryGetSolution(smokable.Owner, smokable.Comp.Solution, out var pipeSolution, out _))
  69. return false;
  70. foreach (var (_, soln) in _solutionContainerSystem.EnumerateSolutions((contents, reagents)))
  71. {
  72. var reagentSolution = soln.Comp.Solution;
  73. _solutionContainerSystem.TryAddSolution(pipeSolution.Value, reagentSolution);
  74. }
  75. EntityManager.DeleteEntity(contents);
  76. _itemSlotsSystem.SetLock(entity.Owner, entity.Comp.BowlSlot, true); //no inserting more until current runs out
  77. return true;
  78. }
  79. }
  80. }