diff --git a/src/TeamBalancer+Utilities.cs b/src/TeamBalancer+Utilities.cs index 400e0dc..257305e 100644 --- a/src/TeamBalancer+Utilities.cs +++ b/src/TeamBalancer+Utilities.cs @@ -21,6 +21,7 @@ public Tuple CountActivePlayers() int count_ct = 0; foreach (CCSPlayerController player in Utilities.GetPlayers()) { + if (player.IsBot) continue; if (player.Team == CsTeam.CounterTerrorist) { count_ct++; diff --git a/src/TeamBalancer.cs b/src/TeamBalancer.cs index 7140c3e..bab3dba 100644 --- a/src/TeamBalancer.cs +++ b/src/TeamBalancer.cs @@ -7,7 +7,7 @@ public partial class TeamBalancer : BasePlugin { public override string ModuleName => "Team Balancer"; public override string ModuleAuthor => "Jon-Mailes Graeffe / Kalle "; - public override string ModuleVersion => "0.0.3"; + public override string ModuleVersion => "0.0.4"; public override void Load(bool hotReload) { @@ -32,6 +32,8 @@ public HookResult OnPlayerTeam(EventPlayerTeam @event, GameEventInfo info) { var player = @event.Userid; if (player == null + || !player.IsValid + || player.IsBot || @event.Team == (byte)CsTeam.Spectator || @event.Team == (byte)CsTeam.None) return HookResult.Continue; // get initial data @@ -39,11 +41,11 @@ public HookResult OnPlayerTeam(EventPlayerTeam @event, GameEventInfo info) int score_ct = GetTeamScore(CsTeam.CounterTerrorist); var (count_t, count_ct) = CountActivePlayers(); // substract counter depending on team to match current players per team (because this is a Hook) - if (@event.Team == (byte)CsTeam.Terrorist) + if (@event.Oldteam == (byte)CsTeam.Terrorist) { count_t -= 1; } - else if (@event.Team == (byte)CsTeam.CounterTerrorist) + else if (@event.Oldteam == (byte)CsTeam.CounterTerrorist) { count_ct -= 1; } @@ -52,21 +54,31 @@ public HookResult OnPlayerTeam(EventPlayerTeam @event, GameEventInfo info) && count_ct <= count_t && score_t - score_ct >= 2) { - player.ChangeTeam(CsTeam.CounterTerrorist); + AddTimer(0f, () => + { + if (player == null || !player.IsValid) return; + player.ChangeTeam(CsTeam.CounterTerrorist); + var @tmpEvent = new EventNextlevelChanged(true); + @tmpEvent.FireEvent(false); + }); // inform players player.PrintToCenterAlert(Localizer["switch.to_ct_center"].Value .Replace("{player}", player.PlayerName)); SendGlobalChatMessage(Localizer["switch.to_ct_chat"].Value .Replace("{player}", player.PlayerName)); // update scoreboard - var @tmpEvent = new EventNextlevelChanged(true); - @tmpEvent.FireEvent(false); } else if (@event.Team == (byte)CsTeam.CounterTerrorist && count_t <= count_ct && score_ct - score_t >= 2) { - player.ChangeTeam(CsTeam.Terrorist); + AddTimer(0f, () => + { + if (player == null || !player.IsValid) return; + player.ChangeTeam(CsTeam.Terrorist); + var @tmpEvent = new EventNextlevelChanged(true); + @tmpEvent.FireEvent(false); + }); // inform players player.PrintToCenterAlert(Localizer["switch.to_t_center"].Value .Replace("{player}", player.PlayerName));