Skip to content

Commit d5dbf77

Browse files
committed
only allow to drop once in each round
1 parent bc35220 commit d5dbf77

File tree

4 files changed

+71
-78
lines changed

4 files changed

+71
-78
lines changed

CS2DropKnife.cs

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
using CounterStrikeSharp.API.Modules.Commands;
33
using CounterStrikeSharp.API.Core.Attributes.Registration;
44
using CounterStrikeSharp.API;
5+
using System.Numerics;
6+
using CounterStrikeSharp.API.Modules.Entities;
57

68
namespace CS2DropKnife;
79

810
public class CS2DropKnife : BasePlugin
911
{
1012
public override string ModuleName => "CS2 Drop Knife";
1113

12-
public override string ModuleVersion => "1.1.0";
14+
public override string ModuleVersion => "2.0.0";
15+
16+
private List<int> player_slot_ids = new List<int>();
1317

1418
public override void Load(bool hotReload)
1519
{
@@ -18,16 +22,32 @@ public override void Load(bool hotReload)
1822
Console.WriteLine("[CS2DropKnife] Registering listeners.");
1923
RegisterListener<Listeners.OnMapStart>(OnMapStartHandler);
2024

21-
// Enabling chat filtering might cause high frame time.
22-
// AddCommandListener("say", OnPlayerChat);
23-
// AddCommandListener("say_team", OnPlayerChatTeam);
24-
2525
if (hotReload)
2626
{
2727
Server.ExecuteCommand("mp_drop_knife_enable 1");
2828
}
2929
}
3030

31+
32+
[GameEventHandler]
33+
public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
34+
{
35+
player_slot_ids.Clear();
36+
37+
foreach(var player in Utilities.GetPlayers())
38+
{
39+
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV)
40+
{
41+
continue;
42+
}
43+
44+
player_slot_ids.Add(player.Slot);
45+
}
46+
47+
return HookResult.Continue;
48+
}
49+
50+
3151
public void OnMapStartHandler(string map)
3252
{
3353
Server.ExecuteCommand("mp_drop_knife_enable 1");
@@ -47,64 +67,60 @@ public void OnTakeKnifeCommand(CCSPlayerController player, CommandInfo commandIn
4767
DropKnife(player);
4868
}
4969

50-
private HookResult OnPlayerChat(CCSPlayerController? player, CommandInfo info)
51-
{
52-
// Filter chat message
53-
if (info.GetArg(1).StartsWith("!drop") || info.GetArg(1).StartsWith("/drop") || info.GetArg(1).StartsWith(".drop") ||
54-
info.GetArg(1).StartsWith("!takeknife") || info.GetArg(1).StartsWith("/takeknife") || info.GetArg(1).StartsWith(".takeknife"))
55-
{
56-
DropKnife(player);
57-
}
58-
59-
return HookResult.Continue;
60-
}
61-
62-
private HookResult OnPlayerChatTeam(CCSPlayerController? player, CommandInfo info)
63-
{
64-
// Filter chat message
65-
if (info.GetArg(1).StartsWith("!drop") || info.GetArg(1).StartsWith("/drop") || info.GetArg(1).StartsWith(".drop") ||
66-
info.GetArg(1).StartsWith("!takeknife") || info.GetArg(1).StartsWith("/takeknife") || info.GetArg(1).StartsWith(".takeknife"))
67-
{
68-
DropKnife(player);
69-
}
70-
71-
return HookResult.Continue;
72-
}
7370

7471
public void DropKnife(CCSPlayerController player)
7572
{
7673
// Player might not be alive.
77-
if (player == null || player.PlayerPawn?.Value == null || player.PlayerPawn?.Value.WeaponServices == null || player.PlayerPawn?.Value.ItemServices == null)
74+
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV || !player.PawnIsAlive || player.Pawn?.Value == null) // || player.PlayerPawn?.Value.WeaponServices == null || player.PlayerPawn?.Value.ItemServices == null)
7875
{
7976
return;
8077
}
8178

82-
var weapons = player.PlayerPawn.Value.WeaponServices?.MyWeapons;
83-
84-
// Player might have no weapon.
85-
if (weapons == null)
79+
// It is not allowed for a single player to drop knives multiple times in a round
80+
if (!player_slot_ids.Contains(player.Slot))
8681
{
8782
return;
8883
}
8984

90-
// Find the knife.
91-
foreach (var weapon in weapons)
85+
// Drop knives
86+
for (int i = 0; i < 4; i++)
9287
{
93-
if (weapon != null && weapon.IsValid && weapon.Value != null && weapon.Value.IsValid)
94-
{
95-
if (weapon.Value.DesignerName.Contains("knife") || weapon.Value.DesignerName.Contains("bayonet"))
96-
{
97-
// Console.WriteLine("[CS2DropKnife] knife index = " + weapon.Index + ", entityindex = " + weapon.Value.Index + ", designer name = " + weapon.Value.DesignerName);
98-
for (int i = 0; i < 5; i++)
99-
{
100-
player.GiveNamedItem(weapon.Value.DesignerName);
101-
}
102-
103-
return;
104-
}
105-
}
88+
player.GiveNamedItem("weapon_knife");
10689
}
10790

108-
player.PrintToChat("[CS2DropKnife] Can't find a knife on you. Get one and try again please.");
91+
// No more chance to drop in this round
92+
player_slot_ids.Remove(player.Slot);
93+
94+
return;
95+
96+
// var weapons = player.PlayerPawn.Value.WeaponServices?.MyWeapons;
97+
98+
// // Player might have no weapon.
99+
// if (weapons == null)
100+
// {
101+
// return;
102+
// }
103+
104+
// // Find the knife.
105+
// foreach (var weapon in weapons)
106+
// {
107+
// if (weapon != null && weapon.IsValid && weapon.Value != null && weapon.Value.IsValid)
108+
// {
109+
// if (weapon.Value.DesignerName.Contains("knife") || weapon.Value.DesignerName.Contains("bayonet"))
110+
// {
111+
// // Console.WriteLine("[CS2DropKnife] knife index = " + weapon.Index + ", entityindex = " + weapon.Value.Index + ", designer name = " + weapon.Value.DesignerName);
112+
// for (int i = 0; i < 5; i++)
113+
// {
114+
// player.GiveNamedItem(weapon.Value.DesignerName);
115+
// }
116+
117+
// player_slot_ids.Remove(player.Slot);
118+
119+
// return;
120+
// }
121+
// }
122+
// }
123+
124+
// player.PrintToChat("[CS2DropKnife] Can't find a knife on you. Get one and try again please.");
109125
}
110126
}

CS2DropKnife.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7+
8+
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
79
</PropertyGroup>
810

911
<ItemGroup>
10-
<Reference Include="CounterStrikeSharp.API">
11-
<HintPath>../addons/counterstrikesharp/api/CounterStrikeSharp.API.dll</HintPath>
12-
</Reference>
12+
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.264" />
1313
</ItemGroup>
1414

1515
</Project>

CS2DropKnife.sln

Lines changed: 0 additions & 25 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Install CounterStrikeSharp first ([Installation Guide](https://docs.cssharp.dev/
99

1010
As long as you have a knife equipped, type "!drop" or "!takeknife" in the chat box and you should see 5 of your knives dropped on the ground.
1111

12+
Note: Every player is allowed to drop knives only once in each round.
13+
1214
## Which version should I use?
1315

1416
For most of the cases, I would recommand to use [the version without chat filtering](https://github.com/lengran/CS2DropKnife/releases/download/v1.1.0/CS2DropKnife-v1.1.0.zip). You can bind css_drop or css_takeknife to your prefered key to achieve the effect of one-key-drop-knife.

0 commit comments

Comments
 (0)