SharedPowerReceiverSystem.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Diagnostics.CodeAnalysis;
  2. using Content.Shared.Administration.Logs;
  3. using Content.Shared.Database;
  4. using Content.Shared.Power.Components;
  5. using Robust.Shared.Audio;
  6. using Robust.Shared.Audio.Systems;
  7. namespace Content.Shared.Power.EntitySystems;
  8. public abstract class SharedPowerReceiverSystem : EntitySystem
  9. {
  10. [Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
  11. [Dependency] private readonly SharedAudioSystem _audio = default!;
  12. public abstract bool ResolveApc(EntityUid entity, [NotNullWhen(true)] ref SharedApcPowerReceiverComponent? component);
  13. public void SetNeedsPower(EntityUid uid, bool value, SharedApcPowerReceiverComponent? receiver = null)
  14. {
  15. if (!ResolveApc(uid, ref receiver) || receiver.NeedsPower == value)
  16. return;
  17. receiver.NeedsPower = value;
  18. Dirty(uid, receiver);
  19. }
  20. public void SetPowerDisabled(EntityUid uid, bool value, SharedApcPowerReceiverComponent? receiver = null)
  21. {
  22. if (!ResolveApc(uid, ref receiver) || receiver.PowerDisabled == value)
  23. return;
  24. receiver.PowerDisabled = value;
  25. Dirty(uid, receiver);
  26. }
  27. /// <summary>
  28. /// Turn this machine on or off.
  29. /// Returns true if we turned it on, false if we turned it off.
  30. /// </summary>
  31. public bool TogglePower(EntityUid uid, bool playSwitchSound = true, SharedApcPowerReceiverComponent? receiver = null, EntityUid? user = null)
  32. {
  33. if (!ResolveApc(uid, ref receiver))
  34. return true;
  35. // it'll save a lot of confusion if 'always powered' means 'always powered'
  36. if (!receiver.NeedsPower)
  37. {
  38. SetPowerDisabled(uid, false, receiver);
  39. return true;
  40. }
  41. SetPowerDisabled(uid, !receiver.PowerDisabled, receiver);
  42. if (user != null)
  43. _adminLogger.Add(LogType.Action, LogImpact.Low, $"{ToPrettyString(user.Value):player} hit power button on {ToPrettyString(uid)}, it's now {(!receiver.PowerDisabled ? "on" : "off")}");
  44. if (playSwitchSound)
  45. {
  46. _audio.PlayPredicted(new SoundPathSpecifier("/Audio/Machines/machine_switch.ogg"), uid, user: user,
  47. AudioParams.Default.WithVolume(-2f));
  48. }
  49. return !receiver.PowerDisabled; // i.e. PowerEnabled
  50. }
  51. /// <summary>
  52. /// Checks if entity is APC-powered device, and if it have power.
  53. /// </summary>
  54. public bool IsPowered(Entity<SharedApcPowerReceiverComponent?> entity)
  55. {
  56. if (!ResolveApc(entity.Owner, ref entity.Comp))
  57. return true;
  58. return entity.Comp.Powered;
  59. }
  60. protected string GetExamineText(bool powered)
  61. {
  62. return Loc.GetString("power-receiver-component-on-examine-main",
  63. ("stateText", Loc.GetString(powered
  64. ? "power-receiver-component-on-examine-powered"
  65. : "power-receiver-component-on-examine-unpowered")));
  66. }
  67. }