|
5 | 5 | using System.Numerics;
|
6 | 6 | using CounterStrikeSharp.API.Modules.Entities;
|
7 | 7 | 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; |
8 | 12 |
|
9 | 13 | namespace CS2DropKnife;
|
10 | 14 |
|
11 | 15 | public class CS2DropKnife : BasePlugin
|
12 | 16 | {
|
13 | 17 | public override string ModuleName => "CS2 Drop Knife";
|
14 | 18 |
|
15 |
| - public override string ModuleVersion => "2.1.0"; |
| 19 | + public override string ModuleVersion => "3.0.0"; |
16 | 20 |
|
17 | 21 | private List<int> player_slot_ids = new List<int>();
|
18 | 22 |
|
| 23 | + private List<int> ct_players = new List<int>(); |
| 24 | + private List<int> t_players = new List<int>(); |
| 25 | + |
19 | 26 | public override void Load(bool hotReload)
|
20 | 27 | {
|
21 | 28 | base.Load(hotReload);
|
@@ -43,6 +50,16 @@ public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
|
43 | 50 | }
|
44 | 51 |
|
45 | 52 | 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 | + |
46 | 63 | }
|
47 | 64 |
|
48 | 65 | return HookResult.Continue;
|
@@ -83,79 +100,119 @@ public void DropKnife(CCSPlayerController player)
|
83 | 100 | return;
|
84 | 101 | }
|
85 | 102 |
|
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) |
88 | 106 | {
|
89 |
| - player.GiveNamedItem("weapon_knife"); |
| 107 | + return; |
90 | 108 | }
|
91 | 109 |
|
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) |
135 | 114 | {
|
136 |
| - CCSPlayerController player = Utilities.GetPlayerFromSlot(player_slot)!; |
137 |
| - if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) |
| 115 | + foreach (int teammate in ct_players) |
138 | 116 | {
|
139 |
| - return HookResult.Continue; |
| 117 | + if (teammate != self_slot) |
| 118 | + { |
| 119 | + teammates.Add(teammate); |
| 120 | + } |
140 | 121 | }
|
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) |
150 | 126 | {
|
151 |
| - DropKnife(player); |
| 127 | + if (teammate != self_slot) |
| 128 | + { |
| 129 | + teammates.Add(teammate); |
| 130 | + } |
152 | 131 | }
|
153 | 132 | }
|
154 |
| - catch (System.Exception) |
| 133 | + |
| 134 | + // Drop knives |
| 135 | + for (int i = 0; i < teammates.Count; i++) |
155 | 136 | {
|
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 | + } |
157 | 176 | }
|
158 | 177 |
|
159 |
| - return HookResult.Continue; |
| 178 | + // No more chance to drop in this round |
| 179 | + player_slot_ids.Remove(player.Slot); |
| 180 | + |
| 181 | + return; |
160 | 182 | }
|
| 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 | + // } |
161 | 218 | }
|
0 commit comments