Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve knife giving #31

Merged
merged 2 commits into from
Nov 15, 2023
Merged
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
47 changes: 25 additions & 22 deletions WeaponPaints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,32 +138,17 @@ private void OnClientDisconnect(int playerSlot)
private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
{
var player = @event.Userid;
if (!player.IsValid || !player.PlayerPawn.IsValid || !player.PawnIsAlive)
if (!player.IsValid || !player.PlayerPawn.IsValid)
{
return HookResult.Continue;
}
if (player.IsBot)
{
player.GiveNamedItem("weapon_knife");
return HookResult.Continue;
}

if (!PlayerHasKnife(player))
{
if (g_playersKnife.TryGetValue((int)player.EntityIndex!.Value.Value, out var knife))
{
player.GiveNamedItem(knife);
}
else
{
player.GiveNamedItem("weapon_knife");
}
}
GiveKnifeToPlayer(player);

// Check the best slot and set it. Weird solution but works xD
AddTimer(0.1f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot3"));
AddTimer(0.1f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot2"));
AddTimer(0.1f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot1"));
AddTimer(0.25f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot2"));
AddTimer(0.35f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot1"));

return HookResult.Continue;
}
Expand Down Expand Up @@ -212,9 +197,28 @@ private void OnEntitySpawned(CEntityInstance entity)
}
});
}
public void GiveKnifeToPlayer(CCSPlayerController player)
{
if (player.IsBot)
{
player.GiveNamedItem((CsTeam)player.TeamNum == CsTeam.Terrorist ? "weapon_knife_t" : "weapon_knife");
return;
}

if (!PlayerHasKnife(player))
{
if (g_playersKnife.TryGetValue((int)player.EntityIndex!.Value.Value, out var knife))
{
player.GiveNamedItem(knife);
}
else
{
player.GiveNamedItem((CsTeam)player.TeamNum == CsTeam.Terrorist ? "weapon_knife_t" : "weapon_knife");
}
}
}
public void RemoveKnifeFromPlayer(CCSPlayerController player)
{
if (!player.PawnIsAlive) return;
if (!g_playersKnife.ContainsKey((int)player.EntityIndex!.Value.Value)) return;
var weapons = player.PlayerPawn.Value.WeaponServices!.MyWeapons;
foreach (var weapon in weapons)
Expand All @@ -225,15 +229,13 @@ public void RemoveKnifeFromPlayer(CCSPlayerController player)
if (weapon.Value.DesignerName.Contains("knife"))
{
weapon.Value.Remove();
player.GiveNamedItem(g_playersKnife[(int)player.EntityIndex!.Value.Value]);
break;
}
}
}
}
public static bool PlayerHasKnife(CCSPlayerController player)
{
if (!player.PawnIsAlive) return false;
var weapons = player.PlayerPawn.Value.WeaponServices!.MyWeapons;
foreach (var weapon in weapons)
{
Expand Down Expand Up @@ -261,6 +263,7 @@ private void SetupMenus()
player.PrintToChat(ReplaceTags(temp));
}
RemoveKnifeFromPlayer(player);
GiveKnifeToPlayer(player);
}
};
foreach (var knife in knifeTypes)
Expand Down
Loading