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

Commit

Permalink
Less code less hook
Browse files Browse the repository at this point in the history
  • Loading branch information
LaplaceTor committed Oct 7, 2024
1 parent 9f4085d commit 0c95c7c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 49 deletions.
57 changes: 9 additions & 48 deletions cs2-jointeamfix/cs2-jointeamfix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,20 @@ public class JoinTeamFix : BasePlugin
{
public override string ModuleName => "cs2-jointeamfix";
public override string ModuleAuthor => "Lapl";
public override string ModuleVersion => "5.1";
private string[] TeamValue = new string[3];
private List<string[]> TeamHistory = new();
private List<CBaseEntity> spawnentityct = new();
private List<CBaseEntity> spawnentityt = new();
public override string ModuleVersion => "final";
private List<Vector> spawnoriginct = new();
private List<Vector> spawnorigint = new();

public override void Load(bool hotLoad)
{
RegisterListener<Listeners.OnMapStart>((onmapstart) =>
{
spawnentityct.Clear();
spawnentityt.Clear();
Server.ExecuteCommand("mp_spectators_max 64");
spawnoriginct.Clear();
spawnorigint.Clear();
Server.NextFrame(()=>
{
spawnentityct = Utilities.FindAllEntitiesByDesignerName<CBaseEntity>("info_player_counterterrorist").ToList();
var spawnentityct = Utilities.FindAllEntitiesByDesignerName<CBaseEntity>("info_player_counterterrorist").ToList();
if(spawnentityct.Count != 0)
{
foreach(var entity in spawnentityct)
Expand All @@ -35,7 +30,7 @@ public override void Load(bool hotLoad)
spawnoriginct.Add(entity.AbsOrigin!);
}
}
spawnentityt = Utilities.FindAllEntitiesByDesignerName<CBaseEntity>("info_player_terrorist").ToList();
var spawnentityt = Utilities.FindAllEntitiesByDesignerName<CBaseEntity>("info_player_terrorist").ToList();
if(spawnentityt.Count != 0)
{
foreach(var entity in spawnentityt)
Expand All @@ -49,47 +44,24 @@ public override void Load(bool hotLoad)

RegisterEventHandler<EventRoundStart>((@event, info) =>
{
TeamHistory.Clear();
var playerlist = Utilities.GetPlayers().Where((x) => x.TeamNum > 1).ToList();
foreach (var player in playerlist)
if (!player.PawnIsAlive)
TryJoinTeam(player);
return HookResult.Continue;
});
RegisterEventHandler<EventPlayerTeam>((@event, info) =>
{
if (!@event.Isbot && !@event.Disconnect)
{
TeamValue = new string[] { @event.Userid!.SteamID.ToString(), @event.Team.ToString(), @event.Oldteam.ToString() };
TeamHistory.Add(TeamValue);
}
else
TeamHistory = TeamHistory.Where(x => !x.Contains(@event.Userid!.SteamID.ToString())).ToList();
if (!player.PawnIsAlive || player.InSwitchTeam)
player.Respawn();
return HookResult.Continue;
});

RegisterEventHandler<EventJointeamFailed>((@event, info) =>
{
if(@event.Userid == null)
return HookResult.Continue;
TryJoinTeam(@event.Userid);
TeamHistory = TeamHistory.Where(x => !x.Contains(@event.Userid!.SteamID.ToString())).ToList();
return HookResult.Continue;
});
}

public void TryJoinTeam(CCSPlayerController player)
{
if (TeamHistory.Any(x => x.Contains(player.SteamID.ToString())))
{
var playerhistory = TeamHistory.Where(x => x.Contains(player.SteamID.ToString())).FirstOrDefault();
if (player.TeamNum == int.Parse(playerhistory![2]))
switch (int.Parse(playerhistory[1]))
{
case 1: player.ChangeTeam(CsTeam.Spectator); break;
case 2: player.ChangeTeam(CsTeam.Terrorist); break;
case 3: player.ChangeTeam(CsTeam.CounterTerrorist); break;
}
}
bool foundTeamJoin = false;
Vector? spawnorigin;
List<Vector> spawnorigingroup = new();
Expand All @@ -98,18 +70,9 @@ public void TryJoinTeam(CCSPlayerController player)

while (!foundTeamJoin)
{
if (player.TeamNum == 2)
{
spawnorigingroup = spawnorigint;
if(searchany == true)
spawnorigingroup = spawnoriginct;
}
else if (player.TeamNum == 3)
{
spawnorigingroup = spawnorigint;
if(searchany == true)
spawnorigingroup = spawnoriginct;
if(searchany == true)
spawnorigingroup = spawnorigint;
}
if (spawnorigingroup.Count() != 0)
{
spawnorigin = spawnorigingroup[random.Next(0,spawnorigingroup.Count-1)];
Expand All @@ -118,9 +81,7 @@ public void TryJoinTeam(CCSPlayerController player)
foundTeamJoin = true;
}
else
{
searchany = true;
}
}
}
}
2 changes: 1 addition & 1 deletion cs2-jointeamfix/cs2-jointeamfix.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.244" />
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.276" />
</ItemGroup>

</Project>

0 comments on commit 0c95c7c

Please sign in to comment.