Skip to content

Commit ed6c92a

Browse files
committed
feature: freeze time only, directly send
1 parent 18166ee commit ed6c92a

File tree

2 files changed

+121
-64
lines changed

2 files changed

+121
-64
lines changed

CS2DropKnife.cs

Lines changed: 120 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@
55
using System.Numerics;
66
using CounterStrikeSharp.API.Modules.Entities;
77
using CounterStrikeSharp.API.Modules.Menu;
8+
using CounterStrikeSharp.API.Modules.Utils;
9+
using Microsoft.VisualBasic;
10+
using Microsoft.Extensions.Logging.Abstractions;
11+
using System.Runtime.InteropServices;
812

913
namespace CS2DropKnife;
1014

1115
public class CS2DropKnife : BasePlugin
1216
{
1317
public override string ModuleName => "CS2 Drop Knife";
1418

15-
public override string ModuleVersion => "2.1.0";
19+
public override string ModuleVersion => "3.0.0";
1620

1721
private List<int> player_slot_ids = new List<int>();
1822

23+
private List<int> ct_players = new List<int>();
24+
private List<int> t_players = new List<int>();
25+
1926
public override void Load(bool hotReload)
2027
{
2128
base.Load(hotReload);
@@ -43,6 +50,16 @@ public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
4350
}
4451

4552
player_slot_ids.Add(player.Slot);
53+
54+
if (player.Team == CsTeam.CounterTerrorist)
55+
{
56+
ct_players.Add(player.Slot);
57+
}
58+
if (player.Team == CsTeam.Terrorist)
59+
{
60+
t_players.Add(player.Slot);
61+
}
62+
4663
}
4764

4865
return HookResult.Continue;
@@ -83,79 +100,119 @@ public void DropKnife(CCSPlayerController player)
83100
return;
84101
}
85102

86-
// Drop knives
87-
for (int i = 0; i < 4; i++)
103+
// Only allow dropping knife at freeze time
104+
var gameRules = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules")?.FirstOrDefault()?.GameRules;
105+
if (gameRules != null && !gameRules.FreezePeriod)
88106
{
89-
player.GiveNamedItem("weapon_knife");
107+
return;
90108
}
91109

92-
// No more chance to drop in this round
93-
player_slot_ids.Remove(player.Slot);
94-
95-
return;
96-
97-
// var weapons = player.PlayerPawn.Value.WeaponServices?.MyWeapons;
98-
99-
// // Player might have no weapon.
100-
// if (weapons == null)
101-
// {
102-
// return;
103-
// }
104-
105-
// // Find the knife.
106-
// foreach (var weapon in weapons)
107-
// {
108-
// if (weapon != null && weapon.IsValid && weapon.Value != null && weapon.Value.IsValid)
109-
// {
110-
// if (weapon.Value.DesignerName.Contains("knife") || weapon.Value.DesignerName.Contains("bayonet"))
111-
// {
112-
// // Console.WriteLine("[CS2DropKnife] knife index = " + weapon.Index + ", entityindex = " + weapon.Value.Index + ", designer name = " + weapon.Value.DesignerName);
113-
// for (int i = 0; i < 5; i++)
114-
// {
115-
// player.GiveNamedItem(weapon.Value.DesignerName);
116-
// }
117-
118-
// player_slot_ids.Remove(player.Slot);
119-
120-
// return;
121-
// }
122-
// }
123-
// }
124-
125-
// player.PrintToChat("[CS2DropKnife] Can't find a knife on you. Get one and try again please.");
126-
}
127-
128-
129-
[GameEventHandler]
130-
public HookResult OnPlayerChat(EventPlayerChat @event, GameEventInfo info)
131-
{
132-
int player_slot = @event.Userid;
133-
134-
try
110+
// Find all teammates
111+
List<int> teammates = new List<int>();
112+
int self_slot = player.Slot;
113+
if (player.Team == CsTeam.CounterTerrorist)
135114
{
136-
CCSPlayerController player = Utilities.GetPlayerFromSlot(player_slot)!;
137-
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV)
115+
foreach (int teammate in ct_players)
138116
{
139-
return HookResult.Continue;
117+
if (teammate != self_slot)
118+
{
119+
teammates.Add(teammate);
120+
}
140121
}
141-
142-
string chat_message = @event.Text;
143-
144-
if (chat_message.StartsWith("!drop")
145-
|| chat_message.StartsWith("/drop")
146-
|| chat_message.StartsWith(".drop")
147-
|| chat_message.StartsWith("!takeknife")
148-
|| chat_message.StartsWith("/takeknife")
149-
|| chat_message.StartsWith(".takeknife"))
122+
}
123+
if (player.Team == CsTeam.Terrorist)
124+
{
125+
foreach (int teammate in t_players)
150126
{
151-
DropKnife(player);
127+
if (teammate != self_slot)
128+
{
129+
teammates.Add(teammate);
130+
}
152131
}
153132
}
154-
catch (System.Exception)
133+
134+
// Drop knives
135+
for (int i = 0; i < teammates.Count; i++)
155136
{
156-
return HookResult.Continue;
137+
player.GiveNamedItem("weapon_knife");
138+
139+
// First find the held knife
140+
var weapons = player.PlayerPawn.Value?.WeaponServices?.MyWeapons!;
141+
if (weapons == null)
142+
{
143+
break;
144+
}
145+
int knife_index = -1;
146+
foreach (var weapon in weapons)
147+
{
148+
if (weapon != null && weapon.IsValid && weapon.Value != null)
149+
{
150+
if (weapon.Value.DesignerName.Contains("knife") || weapon.Value.DesignerName.Contains("bayonet"))
151+
{
152+
knife_index = (int)weapon.Value.Index;
153+
break;
154+
}
155+
}
156+
}
157+
158+
// Then drop and teleport
159+
if (knife_index != -1)
160+
{
161+
player.ExecuteClientCommand("slot3");
162+
player.DropActiveWeapon();
163+
164+
// Find the knife by index and teleport it
165+
var knife = Utilities.GetEntityFromIndex<CBasePlayerWeapon>(knife_index);
166+
if (knife != null && knife.IsValid)
167+
{
168+
var teammate = Utilities.GetPlayerFromSlot(teammates[i]);
169+
if (teammate != null && teammate.IsValid && !teammate.IsBot && !teammate.IsHLTV
170+
&& teammate.PawnIsAlive && teammate.Pawn != null && teammate.Pawn.IsValid && teammate.Pawn.Value != null)
171+
{
172+
knife.Teleport(teammate.Pawn.Value.AbsOrigin);
173+
}
174+
}
175+
}
157176
}
158177

159-
return HookResult.Continue;
178+
// No more chance to drop in this round
179+
player_slot_ids.Remove(player.Slot);
180+
181+
return;
160182
}
183+
184+
185+
// Enable this for chat filtering (might cause performance issues)
186+
// [GameEventHandler]
187+
// public HookResult OnPlayerChat(EventPlayerChat @event, GameEventInfo info)
188+
// {
189+
// int player_slot = @event.Userid;
190+
191+
// try
192+
// {
193+
// CCSPlayerController player = Utilities.GetPlayerFromSlot(player_slot)!;
194+
// if (player == null || !player.IsValid || player.IsBot || player.IsHLTV)
195+
// {
196+
// return HookResult.Continue;
197+
// }
198+
199+
// string chat_message = @event.Text;
200+
201+
// if (chat_message.StartsWith("!drop")
202+
// || chat_message.StartsWith("/drop")
203+
// || chat_message.StartsWith(".drop")
204+
// || chat_message.StartsWith("!takeknife")
205+
// || chat_message.StartsWith("/takeknife")
206+
// || chat_message.StartsWith(".takeknife"))
207+
// {
208+
// DropKnife(player);
209+
// }
210+
// }
211+
// catch (System.Exception)
212+
// {
213+
// return HookResult.Continue;
214+
// }
215+
216+
// return HookResult.Continue;
217+
// }
161218
}

CS2DropKnife.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.264" />
12+
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.296" />
1313
</ItemGroup>
1414

1515
</Project>

0 commit comments

Comments
 (0)