GlueSystem.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Content.Server.Administration.Logs;
  2. using Content.Shared.Chemistry.EntitySystems;
  3. using Content.Shared.Database;
  4. using Content.Shared.Glue;
  5. using Content.Shared.Hands;
  6. using Content.Shared.Interaction;
  7. using Content.Shared.Interaction.Components;
  8. using Content.Shared.Item;
  9. using Content.Shared.NameModifier.EntitySystems;
  10. using Content.Shared.Nutrition.EntitySystems;
  11. using Content.Shared.Popups;
  12. using Content.Shared.Verbs;
  13. using Robust.Shared.Audio.Systems;
  14. using Robust.Shared.Timing;
  15. namespace Content.Server.Glue;
  16. public sealed class GlueSystem : SharedGlueSystem
  17. {
  18. [Dependency] private readonly SharedAudioSystem _audio = default!;
  19. [Dependency] private readonly SharedPopupSystem _popup = default!;
  20. [Dependency] private readonly SharedSolutionContainerSystem _solutionContainer = default!;
  21. [Dependency] private readonly IGameTiming _timing = default!;
  22. [Dependency] private readonly IAdminLogManager _adminLogger = default!;
  23. [Dependency] private readonly OpenableSystem _openable = default!;
  24. [Dependency] private readonly NameModifierSystem _nameMod = default!;
  25. public override void Initialize()
  26. {
  27. base.Initialize();
  28. SubscribeLocalEvent<GlueComponent, AfterInteractEvent>(OnInteract, after: new[] { typeof(OpenableSystem) });
  29. SubscribeLocalEvent<GluedComponent, ComponentInit>(OnGluedInit);
  30. SubscribeLocalEvent<GlueComponent, GetVerbsEvent<UtilityVerb>>(OnUtilityVerb);
  31. SubscribeLocalEvent<GluedComponent, GotEquippedHandEvent>(OnHandPickUp);
  32. SubscribeLocalEvent<GluedComponent, RefreshNameModifiersEvent>(OnRefreshNameModifiers);
  33. }
  34. // When glue bottle is used on item it will apply the glued and unremoveable components.
  35. private void OnInteract(Entity<GlueComponent> entity, ref AfterInteractEvent args)
  36. {
  37. if (args.Handled)
  38. return;
  39. if (!args.CanReach || args.Target is not { Valid: true } target)
  40. return;
  41. if (TryGlue(entity, target, args.User))
  42. args.Handled = true;
  43. }
  44. private void OnUtilityVerb(Entity<GlueComponent> entity, ref GetVerbsEvent<UtilityVerb> args)
  45. {
  46. if (!args.CanInteract || !args.CanAccess || args.Target is not { Valid: true } target ||
  47. _openable.IsClosed(entity))
  48. return;
  49. var user = args.User;
  50. var verb = new UtilityVerb()
  51. {
  52. Act = () => TryGlue(entity, target, user),
  53. IconEntity = GetNetEntity(entity),
  54. Text = Loc.GetString("glue-verb-text"),
  55. Message = Loc.GetString("glue-verb-message")
  56. };
  57. args.Verbs.Add(verb);
  58. }
  59. private bool TryGlue(Entity<GlueComponent> entity, EntityUid target, EntityUid actor)
  60. {
  61. // if item is glued then don't apply glue again so it can be removed for reasonable time
  62. if (HasComp<GluedComponent>(target) || !HasComp<ItemComponent>(target))
  63. {
  64. _popup.PopupEntity(Loc.GetString("glue-failure", ("target", target)), actor, actor, PopupType.Medium);
  65. return false;
  66. }
  67. if (HasComp<ItemComponent>(target) && _solutionContainer.TryGetSolution(entity.Owner, entity.Comp.Solution, out _, out var solution))
  68. {
  69. var quantity = solution.RemoveReagent(entity.Comp.Reagent, entity.Comp.ConsumptionUnit);
  70. if (quantity > 0)
  71. {
  72. EnsureComp<GluedComponent>(target).Duration = quantity.Double() * entity.Comp.DurationPerUnit;
  73. _adminLogger.Add(LogType.Action, LogImpact.Medium, $"{ToPrettyString(actor):actor} glued {ToPrettyString(target):subject} with {ToPrettyString(entity.Owner):tool}");
  74. _audio.PlayPvs(entity.Comp.Squeeze, entity.Owner);
  75. _popup.PopupEntity(Loc.GetString("glue-success", ("target", target)), actor, actor, PopupType.Medium);
  76. return true;
  77. }
  78. }
  79. _popup.PopupEntity(Loc.GetString("glue-failure", ("target", target)), actor, actor, PopupType.Medium);
  80. return false;
  81. }
  82. public override void Update(float frameTime)
  83. {
  84. base.Update(frameTime);
  85. var query = EntityQueryEnumerator<GluedComponent, UnremoveableComponent>();
  86. while (query.MoveNext(out var uid, out var glue, out var _))
  87. {
  88. if (_timing.CurTime < glue.Until)
  89. continue;
  90. RemComp<UnremoveableComponent>(uid);
  91. RemComp<GluedComponent>(uid);
  92. _nameMod.RefreshNameModifiers(uid);
  93. }
  94. }
  95. private void OnGluedInit(Entity<GluedComponent> entity, ref ComponentInit args)
  96. {
  97. _nameMod.RefreshNameModifiers(entity.Owner);
  98. }
  99. private void OnHandPickUp(Entity<GluedComponent> entity, ref GotEquippedHandEvent args)
  100. {
  101. var comp = EnsureComp<UnremoveableComponent>(entity);
  102. comp.DeleteOnDrop = false;
  103. entity.Comp.Until = _timing.CurTime + entity.Comp.Duration;
  104. }
  105. private void OnRefreshNameModifiers(Entity<GluedComponent> entity, ref RefreshNameModifiersEvent args)
  106. {
  107. args.AddModifier("glued-name-prefix");
  108. }
  109. }