Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Fix respawn button visual bug #241

Merged
merged 2 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions Content.Server/Corvax/Respawn/RespawnSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Content.Shared.Mobs;
using Content.Shared.Mobs.Components;
using Robust.Server.Player;
using Robust.Shared.Network;
using Robust.Shared.Player;
using Robust.Shared.Timing;

namespace Content.Server.Corvax.Respawn;
Expand All @@ -15,13 +15,15 @@ public sealed class RespawnSystem : EntitySystem
[Dependency] private readonly IPlayerManager _player = default!;
[Dependency] private readonly IGameTiming _timing = default!;

private readonly Dictionary<NetUserId, TimeSpan> _respawnResetTimes = [];
private readonly Dictionary<ICommonSession, TimeSpan> _respawnResetTimes = [];

public override void Initialize()
{
SubscribeLocalEvent<MobStateChangedEvent>(OnMobStateChanged);
SubscribeLocalEvent<MindContainerComponent, MindRemovedMessage>(OnMindRemoved);
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundRestartCleanup);

_player.PlayerStatusChanged += OnPlayerStatusChanged;
}

private void OnMobStateChanged(MobStateChangedEvent e)
Expand All @@ -32,7 +34,7 @@ private void OnMobStateChanged(MobStateChangedEvent e)
if (!_player.TryGetSessionByEntity(e.Target, out var session))
return;

ResetRespawnTime(e.Target, session.UserId);
ResetRespawnTime(e.Target, session);
}

private void OnMindRemoved(EntityUid entity, MindContainerComponent component, MindRemovedMessage e)
Expand All @@ -43,36 +45,39 @@ private void OnMindRemoved(EntityUid entity, MindContainerComponent component, M
if (TryComp<MobStateComponent>(entity, out var state) && state.CurrentState == MobState.Dead)
return;

ResetRespawnTime(entity, e.Mind.Comp.UserId.Value);
ResetRespawnTime(entity, _player.GetSessionById(e.Mind.Comp.UserId.Value));
}

private void OnRoundRestartCleanup(RoundRestartCleanupEvent e)
{
foreach (var player in _respawnResetTimes.Keys)
SendRespawnResetTime(player, null);

_respawnResetTimes.Clear();
}

private void ResetRespawnTime(EntityUid entity, NetUserId player)
private void OnPlayerStatusChanged(object? sender, SessionStatusEventArgs e)
{
if (e.NewStatus == Robust.Shared.Enums.SessionStatus.Connected)
SendRespawnResetTime(e.Session, GetRespawnResetTime(e.Session));
}

private void ResetRespawnTime(EntityUid entity, ICommonSession session)
{
if (!HasComp<RespawnResetComponent>(entity))
return;

ref var respawnTime = ref CollectionsMarshal.GetValueRefOrAddDefault(_respawnResetTimes, player, out _);
ref var respawnTime = ref CollectionsMarshal.GetValueRefOrAddDefault(_respawnResetTimes, session, out _);

respawnTime = _timing.CurTime;

SendRespawnResetTime(player, _timing.CurTime);
SendRespawnResetTime(session, _timing.CurTime);
}

private void SendRespawnResetTime(NetUserId player, TimeSpan? time)
private void SendRespawnResetTime(ICommonSession session, TimeSpan? time)
{
RaiseNetworkEvent(new RespawnResetEvent(time), _player.GetSessionById(player));
RaiseNetworkEvent(new RespawnResetEvent(time), session);
}

public TimeSpan? GetRespawnResetTime(NetUserId user)
public TimeSpan? GetRespawnResetTime(ICommonSession session)
{
return _respawnResetTimes.TryGetValue(user, out var time) ? time : null;
return _respawnResetTimes.TryGetValue(session, out var time) ? time : null;
}
}
2 changes: 1 addition & 1 deletion Content.Server/_NF/Commands/GhostRespawnCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void Execute(IConsoleShell shell, string argStr, string[] args)
return;
}

var respawnResetTime = _entity.GetEntitySystem<RespawnSystem>().GetRespawnResetTime(shell.Player.UserId);
var respawnResetTime = _entity.GetEntitySystem<RespawnSystem>().GetRespawnResetTime(shell.Player);

if (respawnResetTime is not null)
{
Expand Down
Loading