From 4e8365c3407676b4c5f2c5d34990b5c040ae00c9 Mon Sep 17 00:00:00 2001
From: SimpleStation14 <130339894+SimpleStation14@users.noreply.github.com>
Date: Wed, 29 May 2024 00:18:31 -0400
Subject: [PATCH] Mirror: Use entity queries in ambient sound & power receiver
systems (#376)
## Mirror of PR #26410: [Use entity queries in ambient sound & power
receiver
systems](https://github.com/space-wizards/space-station-14/pull/26410)
from
[space-wizards](https://github.com/space-wizards)/[space-station-14](https://github.com/space-wizards/space-station-14)
###### `31d70db547f855d2a3d4075e4fcf54d1c87c0f06`
PR opened by ElectroJr at
2024-03-24 20:29:56 UTC
---
PR changed 2 files with 18 additions and 10 deletions.
The PR had the following labels:
---
Original Body
>
Co-authored-by: SimpleStation14
Co-authored-by: VMSolidus
---
.../Power/EntitySystems/PowerReceiverSystem.cs | 17 +++++++++++------
.../Audio/SharedAmbientSoundSystem.cs | 11 +++++++----
2 files changed, 18 insertions(+), 10 deletions(-)
diff --git a/Content.Server/Power/EntitySystems/PowerReceiverSystem.cs b/Content.Server/Power/EntitySystems/PowerReceiverSystem.cs
index 5ed39d51787..048fda23553 100644
--- a/Content.Server/Power/EntitySystems/PowerReceiverSystem.cs
+++ b/Content.Server/Power/EntitySystems/PowerReceiverSystem.cs
@@ -20,6 +20,8 @@ public sealed class PowerReceiverSystem : EntitySystem
[Dependency] private readonly IAdminManager _adminManager = default!;
[Dependency] private readonly AppearanceSystem _appearance = default!;
[Dependency] private readonly AudioSystem _audio = default!;
+ private EntityQuery _recQuery;
+ private EntityQuery _provQuery;
public override void Initialize()
{
@@ -35,6 +37,9 @@ public override void Initialize()
SubscribeLocalEvent>(OnGetVerbs);
SubscribeLocalEvent>(AddSwitchPowerVerb);
+
+ _recQuery = GetEntityQuery();
+ _provQuery = GetEntityQuery();
}
private void OnGetVerbs(EntityUid uid, ApcPowerReceiverComponent component, GetVerbsEvent args)
@@ -77,7 +82,7 @@ private void OnProviderShutdown(EntityUid uid, ApcPowerProviderComponent compone
private void OnProviderConnected(Entity receiver, ref ExtensionCableSystem.ProviderConnectedEvent args)
{
var providerUid = args.Provider.Owner;
- if (!EntityManager.TryGetComponent(providerUid, out var provider))
+ if (!_provQuery.TryGetComponent(providerUid, out var provider))
return;
receiver.Comp.Provider = provider;
@@ -94,7 +99,7 @@ private void OnProviderDisconnected(Entity receiver,
private void OnReceiverConnected(Entity provider, ref ExtensionCableSystem.ReceiverConnectedEvent args)
{
- if (EntityManager.TryGetComponent(args.Receiver, out ApcPowerReceiverComponent? receiver))
+ if (_recQuery.TryGetComponent(args.Receiver, out var receiver))
{
provider.Comp.AddReceiver(receiver);
}
@@ -102,7 +107,7 @@ private void OnReceiverConnected(Entity provider, ref
private void OnReceiverDisconnected(EntityUid uid, ApcPowerProviderComponent provider, ExtensionCableSystem.ReceiverDisconnectedEvent args)
{
- if (EntityManager.TryGetComponent(args.Receiver, out ApcPowerReceiverComponent? receiver))
+ if (_recQuery.TryGetComponent(args.Receiver, out var receiver))
{
provider.RemoveReceiver(receiver);
}
@@ -116,7 +121,7 @@ private void AddSwitchPowerVerb(EntityUid uid, PowerSwitchComponent component, G
if (!HasComp(args.User))
return;
- if (!TryComp(uid, out var receiver))
+ if (!_recQuery.TryGetComponent(uid, out var receiver))
return;
if (!receiver.NeedsPower)
@@ -152,7 +157,7 @@ private void ProviderChanged(Entity receiver)
///
public bool IsPowered(EntityUid uid, ApcPowerReceiverComponent? receiver = null)
{
- if (!Resolve(uid, ref receiver, false))
+ if (!_recQuery.Resolve(uid, ref receiver, false))
return true;
return receiver.Powered;
@@ -164,7 +169,7 @@ public bool IsPowered(EntityUid uid, ApcPowerReceiverComponent? receiver = null)
///
public bool TogglePower(EntityUid uid, bool playSwitchSound = true, ApcPowerReceiverComponent? receiver = null, EntityUid? user = null)
{
- if (!Resolve(uid, ref receiver, false))
+ if (!_recQuery.Resolve(uid, ref receiver, false))
return true;
// it'll save a lot of confusion if 'always powered' means 'always powered'
diff --git a/Content.Shared/Audio/SharedAmbientSoundSystem.cs b/Content.Shared/Audio/SharedAmbientSoundSystem.cs
index 6318ba25573..5f17261825c 100644
--- a/Content.Shared/Audio/SharedAmbientSoundSystem.cs
+++ b/Content.Shared/Audio/SharedAmbientSoundSystem.cs
@@ -5,16 +5,19 @@ namespace Content.Shared.Audio;
public abstract class SharedAmbientSoundSystem : EntitySystem
{
+ private EntityQuery _query;
+
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent(GetCompState);
SubscribeLocalEvent(HandleCompState);
+ _query = GetEntityQuery();
}
public virtual void SetAmbience(EntityUid uid, bool value, AmbientSoundComponent? ambience = null)
{
- if (!Resolve(uid, ref ambience, false) || ambience.Enabled == value)
+ if (!_query.Resolve(uid, ref ambience, false) || ambience.Enabled == value)
return;
ambience.Enabled = value;
@@ -24,7 +27,7 @@ public virtual void SetAmbience(EntityUid uid, bool value, AmbientSoundComponent
public virtual void SetRange(EntityUid uid, float value, AmbientSoundComponent? ambience = null)
{
- if (!Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Range, value))
+ if (!_query.Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Range, value))
return;
ambience.Range = value;
@@ -39,7 +42,7 @@ protected virtual void QueueUpdate(EntityUid uid, AmbientSoundComponent ambience
public virtual void SetVolume(EntityUid uid, float value, AmbientSoundComponent? ambience = null)
{
- if (!Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Volume, value))
+ if (!_query.Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Volume, value))
return;
ambience.Volume = value;
@@ -48,7 +51,7 @@ public virtual void SetVolume(EntityUid uid, float value, AmbientSoundComponent?
public virtual void SetSound(EntityUid uid, SoundSpecifier sound, AmbientSoundComponent? ambience = null)
{
- if (!Resolve(uid, ref ambience, false) || ambience.Sound == sound)
+ if (!_query.Resolve(uid, ref ambience, false) || ambience.Sound == sound)
return;
ambience.Sound = sound;