2
2
using CounterStrikeSharp . API . Modules . Commands ;
3
3
using CounterStrikeSharp . API . Core . Attributes . Registration ;
4
4
using CounterStrikeSharp . API ;
5
+ using System . Numerics ;
6
+ using CounterStrikeSharp . API . Modules . Entities ;
5
7
6
8
namespace CS2DropKnife ;
7
9
8
10
public class CS2DropKnife : BasePlugin
9
11
{
10
12
public override string ModuleName => "CS2 Drop Knife" ;
11
13
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 > ( ) ;
13
17
14
18
public override void Load ( bool hotReload )
15
19
{
@@ -18,16 +22,32 @@ public override void Load(bool hotReload)
18
22
Console . WriteLine ( "[CS2DropKnife] Registering listeners." ) ;
19
23
RegisterListener < Listeners . OnMapStart > ( OnMapStartHandler ) ;
20
24
21
- // Enabling chat filtering might cause high frame time.
22
- // AddCommandListener("say", OnPlayerChat);
23
- // AddCommandListener("say_team", OnPlayerChatTeam);
24
-
25
25
if ( hotReload )
26
26
{
27
27
Server . ExecuteCommand ( "mp_drop_knife_enable 1" ) ;
28
28
}
29
29
}
30
30
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
+
31
51
public void OnMapStartHandler ( string map )
32
52
{
33
53
Server . ExecuteCommand ( "mp_drop_knife_enable 1" ) ;
@@ -47,64 +67,60 @@ public void OnTakeKnifeCommand(CCSPlayerController player, CommandInfo commandIn
47
67
DropKnife ( player ) ;
48
68
}
49
69
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
- }
73
70
74
71
public void DropKnife ( CCSPlayerController player )
75
72
{
76
73
// 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)
78
75
{
79
76
return ;
80
77
}
81
78
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 ) )
86
81
{
87
82
return ;
88
83
}
89
84
90
- // Find the knife.
91
- foreach ( var weapon in weapons )
85
+ // Drop knives
86
+ for ( int i = 0 ; i < 4 ; i ++ )
92
87
{
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" ) ;
106
89
}
107
90
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.");
109
125
}
110
126
}
0 commit comments