diff --git a/SpectatorList/EventHandler.cs b/SpectatorList/EventHandler.cs index 3b0e153..4240507 100644 --- a/SpectatorList/EventHandler.cs +++ b/SpectatorList/EventHandler.cs @@ -1,45 +1,56 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using MEC; using Exiled.API.Features; +using MEC; using PlayerRoles; +using UnityEngine; namespace SpectatorList { public class EventHandler { + private const string CoroutineTag = "Spectator-List"; + private Config _config => SpectatorList.Instance.Config; - - public EventHandler() => Timing.RunCoroutine(DoList()); + + public EventHandler() + { + Exiled.Events.Handlers.Server.RoundStarted += OnRoundStarted; + } + + ~EventHandler() + { + Exiled.Events.Handlers.Server.RoundStarted -= OnRoundStarted; + Timing.KillCoroutines(CoroutineTag); + } + + private void OnRoundStarted() => Timing.RunCoroutine(DoList().CancelWith(Server.Host.GameObject), CoroutineTag); private IEnumerator DoList() { - for (; ;) + Log.Debug("haii"); + while (true) { - if (Round.IsEnded) - { - Timing.WaitForSeconds(5); - continue; - } - + Log.Debug("Running foreach loop."); + foreach (Player player in Player.List) { + Log.Debug("Checking condition for Spec List"); if (player.IsDead || _config.HiddenFor.Contains(player.Role.Team)) continue; - + Log.Debug("Player is able to see Spectator List"); + int count = player.CurrentSpectatingPlayers.Count(p => p.Role != RoleTypeId.Overwatch); - + StringBuilder sb = new StringBuilder(); sb.AppendLine($"{count} players are spectating{(count == 0 ? string.Empty : ":")}"); - + foreach (Player spectator in player.CurrentSpectatingPlayers.Where(p => p.Role != RoleTypeId.Overwatch)) - { - sb.AppendLine(_config.PlayerDisplay.Replace("%name%", spectator.DisplayNickname)); - } + sb.AppendLine(_config.PlayerDisplay.Replace("%name%", spectator.CustomName)); player.ShowHint(_config.FullText.Replace("%display%", sb.ToString()), _config.RefreshRate + 0.15f); } - + yield return Timing.WaitForSeconds(_config.RefreshRate); } } diff --git a/SpectatorList/SpectatorList.cs b/SpectatorList/SpectatorList.cs index e8941fe..7e12f0c 100644 --- a/SpectatorList/SpectatorList.cs +++ b/SpectatorList/SpectatorList.cs @@ -9,7 +9,7 @@ public class SpectatorList : Plugin public override string Name { get; } = "Spectator List"; public override string Author { get; } = "@misfiy"; - public override Version Version { get; } = new Version(1,1,0); + public override Version Version { get; } = new Version(1,1,1); public override Version RequiredExiledVersion { get; } = new Version(8, 8, 1); private EventHandler _handler;