Hopefully fix alerts disappearing (#6986)

This commit is contained in:
Leon Friedrich
2022-03-04 23:52:51 +13:00
committed by GitHub
parent de6b34aaa3
commit ee60ac982e
2 changed files with 49 additions and 35 deletions

View File

@@ -1,14 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using Content.Shared.Alert; using Content.Shared.Alert;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Client.GameObjects; using Robust.Client.GameObjects;
using Robust.Client.Player; using Robust.Client.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
namespace Content.Client.Alerts; namespace Content.Client.Alerts;
@@ -28,12 +23,11 @@ internal sealed class ClientAlertsSystem : AlertsSystem
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<AlertsComponent, PlayerAttachedEvent>((_, component, _) => PlayerAttached(component)); SubscribeLocalEvent<AlertsComponent, PlayerAttachedEvent>(OnPlayerAttached);
SubscribeLocalEvent<AlertsComponent, PlayerDetachedEvent>((_, _, _) => PlayerDetached()); SubscribeLocalEvent<AlertsComponent, PlayerDetachedEvent>(OnPlayerDetached);
SubscribeLocalEvent<AlertsComponent, ComponentHandleState>(ClientAlertsHandleState); SubscribeLocalEvent<AlertsComponent, ComponentHandleState>(ClientAlertsHandleState);
} }
protected override void LoadPrototypes() protected override void LoadPrototypes()
{ {
base.LoadPrototypes(); base.LoadPrototypes();
@@ -56,7 +50,7 @@ internal sealed class ClientAlertsSystem : AlertsSystem
protected override void AfterShowAlert(AlertsComponent alertsComponent) protected override void AfterShowAlert(AlertsComponent alertsComponent)
{ {
if (!CurControlled(alertsComponent.Owner, _playerManager)) if (_playerManager.LocalPlayer?.ControlledEntity != alertsComponent.Owner)
return; return;
SyncAlerts?.Invoke(this, alertsComponent.Alerts); SyncAlerts?.Invoke(this, alertsComponent.Alerts);
@@ -64,7 +58,7 @@ internal sealed class ClientAlertsSystem : AlertsSystem
protected override void AfterClearAlert(AlertsComponent alertsComponent) protected override void AfterClearAlert(AlertsComponent alertsComponent)
{ {
if (!CurControlled(alertsComponent.Owner, _playerManager)) if (_playerManager.LocalPlayer?.ControlledEntity != alertsComponent.Owner)
return; return;
SyncAlerts?.Invoke(this, alertsComponent.Alerts); SyncAlerts?.Invoke(this, alertsComponent.Alerts);
@@ -72,31 +66,40 @@ internal sealed class ClientAlertsSystem : AlertsSystem
private void ClientAlertsHandleState(EntityUid uid, AlertsComponent component, ref ComponentHandleState args) private void ClientAlertsHandleState(EntityUid uid, AlertsComponent component, ref ComponentHandleState args)
{ {
if (_playerManager.LocalPlayer?.ControlledEntity != uid)
return;
var componentAlerts = (args.Current as AlertsComponentState)?.Alerts; var componentAlerts = (args.Current as AlertsComponentState)?.Alerts;
if (componentAlerts == null) return; if (componentAlerts == null) return;
//TODO: Do we really want to send alerts for non-attached entity?
component.Alerts = new(componentAlerts); component.Alerts = new(componentAlerts);
if (!CurControlled(component.Owner, _playerManager)) return;
SyncAlerts?.Invoke(this, componentAlerts); SyncAlerts?.Invoke(this, componentAlerts);
} }
private void PlayerAttached(AlertsComponent clientAlertsComponent) private void OnPlayerAttached(EntityUid uid, AlertsComponent component, PlayerAttachedEvent args)
{ {
if (!CurControlled(clientAlertsComponent.Owner, _playerManager)) return; if (_playerManager.LocalPlayer?.ControlledEntity != uid)
SyncAlerts?.Invoke(this, clientAlertsComponent.Alerts); return;
SyncAlerts?.Invoke(this, component.Alerts);
} }
protected override void HandleComponentShutdown(EntityUid uid) protected override void HandleComponentShutdown(EntityUid uid, AlertsComponent component, ComponentShutdown args)
{ {
base.HandleComponentShutdown(uid); base.HandleComponentShutdown(uid, component, args);
PlayerDetached(); if (_playerManager.LocalPlayer?.ControlledEntity != uid)
return;
ClearAlerts?.Invoke(this, EventArgs.Empty);
} }
private void PlayerDetached() private void OnPlayerDetached(EntityUid uid, AlertsComponent component, PlayerDetachedEvent args)
{ {
if (_playerManager.LocalPlayer?.ControlledEntity != uid)
return;
ClearAlerts?.Invoke(this, EventArgs.Empty); ClearAlerts?.Invoke(this, EventArgs.Empty);
} }
@@ -104,12 +107,4 @@ internal sealed class ClientAlertsSystem : AlertsSystem
{ {
RaiseNetworkEvent(new ClickAlertEvent(alertType)); RaiseNetworkEvent(new ClickAlertEvent(alertType));
} }
/// <summary>
/// Allows calculating if we need to act due to this component being controlled by the current mob
/// </summary>
private static bool CurControlled(EntityUid entity, IPlayerManager playerManager)
{
return playerManager.LocalPlayer != null && playerManager.LocalPlayer.ControlledEntity == entity;
}
} }

View File

@@ -1,10 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
namespace Content.Shared.Alert; namespace Content.Shared.Alert;
@@ -14,6 +9,8 @@ public abstract class AlertsSystem : EntitySystem
[Dependency] [Dependency]
private readonly IPrototypeManager _prototypeManager = default!; private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly MetaDataSystem _metaSystem = default!;
private readonly Dictionary<AlertType, AlertPrototype> _typeToAlert = new(); private readonly Dictionary<AlertType, AlertPrototype> _typeToAlert = new();
public IReadOnlyDictionary<AlertKey, AlertState>? GetActiveAlerts(EntityUid euid) public IReadOnlyDictionary<AlertKey, AlertState>? GetActiveAlerts(EntityUid euid)
@@ -158,19 +155,41 @@ public abstract class AlertsSystem : EntitySystem
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<AlertsComponent, ComponentStartup>((uid, _, _) => RaiseLocalEvent(uid, new AlertSyncEvent(uid))); SubscribeLocalEvent<AlertsComponent, ComponentStartup>(HandleComponentStartup);
SubscribeLocalEvent<AlertsComponent, ComponentShutdown>((uid, _, _) => HandleComponentShutdown(uid)); SubscribeLocalEvent<AlertsComponent, ComponentShutdown>(HandleComponentShutdown);
SubscribeLocalEvent<AlertsComponent, MetaFlagRemoveAttemptEvent>(OnMetaFlagRemoval);
SubscribeLocalEvent<AlertsComponent, ComponentGetState>(ClientAlertsGetState); SubscribeLocalEvent<AlertsComponent, ComponentGetState>(ClientAlertsGetState);
SubscribeLocalEvent<AlertsComponent, ComponentGetStateAttemptEvent>(OnCanGetState);
SubscribeNetworkEvent<ClickAlertEvent>(HandleClickAlert); SubscribeNetworkEvent<ClickAlertEvent>(HandleClickAlert);
LoadPrototypes(); LoadPrototypes();
_prototypeManager.PrototypesReloaded += HandlePrototypesReloaded; _prototypeManager.PrototypesReloaded += HandlePrototypesReloaded;
} }
protected virtual void HandleComponentShutdown(EntityUid uid) private void OnMetaFlagRemoval(EntityUid uid, AlertsComponent component, ref MetaFlagRemoveAttemptEvent args)
{
if (component.LifeStage == ComponentLifeStage.Running)
args.Cancelled = true;
}
private void OnCanGetState(EntityUid uid, AlertsComponent component, ref ComponentGetStateAttemptEvent args)
{
// Only send alert state data to the relevant player.
if (args.Player.AttachedEntity != uid)
args.Cancelled = true;
}
protected virtual void HandleComponentShutdown(EntityUid uid, AlertsComponent component, ComponentShutdown args)
{ {
RaiseLocalEvent(uid, new AlertSyncEvent(uid)); RaiseLocalEvent(uid, new AlertSyncEvent(uid));
_metaSystem.RemoveFlag(uid, MetaDataFlags.EntitySpecific);
}
private void HandleComponentStartup(EntityUid uid, AlertsComponent component, ComponentStartup args)
{
RaiseLocalEvent(uid, new AlertSyncEvent(uid));
_metaSystem.AddFlag(uid, MetaDataFlags.EntitySpecific);
} }
public override void Shutdown() public override void Shutdown()