Skip to content

Commit a5beb60

Browse files
Merge pull request #2 from CSGALS/allow-weapon-switching
Only inhibit weapon select when using number key binds
2 parents 42490ab + 3750a67 commit a5beb60

2 files changed

Lines changed: 38 additions & 8 deletions

File tree

src/SharpModMenu/PlayerMenuState.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public bool HasKeyBinds
4545
public bool WasPressingReload { get; set; }
4646
public bool WasPressingTab { get; set; }
4747

48+
public int ClientTick { get; set; }
49+
public int? InhibitWeaponSelection { get; set; }
50+
4851
public void HandleInput(PlayerKey key, bool fromBind)
4952
{
5053
if (key is >= PlayerKey.SelectItem1 and <= PlayerKey.SelectItem0 && fromBind)
@@ -53,6 +56,9 @@ public void HandleInput(PlayerKey key, bool fromBind)
5356
if (CurrentMenu is null)
5457
return;
5558

59+
if (key is >= PlayerKey.SelectItem1 and <= PlayerKey.SelectItem0 && fromBind)
60+
InhibitWeaponSelection = ClientTick + 1;
61+
5662
var itemsStart = CurrentMenu.CurrentPage * Menu.ItemsPerPage;
5763
var buttonState = CurrentMenu.GetButtonStates();
5864

src/SharpModMenu/SharpModMenu.cs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
using CSSUniversalMenuAPI;
1616
using static CounterStrikeSharp.API.Core.Listeners;
1717

18-
1918
namespace SharpModMenu;
2019

21-
2220
[MinimumApiVersion(314)]
2321
public sealed class SharpModMenuPlugin : BasePlugin
2422
{
@@ -88,45 +86,63 @@ private void OnCheckTransmit(CCheckTransmitInfoList infoList)
8886
[GameEventHandler(HookMode.Pre)]
8987
public HookResult OnPlayerDisconnect(EventPlayerDisconnect e, GameEventInfo info)
9088
{
89+
if (DriverInstance?.ActiveMenuStates is null)
90+
return HookResult.Continue;
91+
9192
DriverInstance?.PlayerDisconnected(e.Userid);
9293
return HookResult.Continue;
9394
}
9495

9596
[GameEventHandler(HookMode.Post)]
96-
public HookResult OnPlayerDisconnect(EventGameStart e, GameEventInfo info)
97+
public HookResult OnGameStart(EventGameStart e, GameEventInfo info)
9798
{
99+
if (DriverInstance?.ActiveMenuStates is null)
100+
return HookResult.Continue;
101+
98102
foreach (var state in DriverInstance!.ActiveMenuStates)
99103
state.ForceRefresh = true;
100104
return HookResult.Continue;
101105
}
102106

103107
[GameEventHandler(HookMode.Post)]
104-
public HookResult OnPlayerDisconnect(EventRoundStart e, GameEventInfo info)
108+
public HookResult OnRoundStart(EventRoundStart e, GameEventInfo info)
105109
{
110+
if (DriverInstance?.ActiveMenuStates is null)
111+
return HookResult.Continue;
112+
106113
foreach (var state in DriverInstance!.ActiveMenuStates)
107114
state.ForceRefresh = true;
108115
return HookResult.Continue;
109116
}
110117

111118
[GameEventHandler(HookMode.Post)]
112-
public HookResult OnPlayerDisconnect(EventBeginNewMatch e, GameEventInfo info)
119+
public HookResult OnBeginNewMatch(EventBeginNewMatch e, GameEventInfo info)
113120
{
121+
if (DriverInstance?.ActiveMenuStates is null)
122+
return HookResult.Continue;
123+
114124
foreach (var state in DriverInstance!.ActiveMenuStates)
115125
state.ForceRefresh = true;
116126
return HookResult.Continue;
117127
}
118128

119129
[GameEventHandler(HookMode.Post)]
120-
public HookResult OnPlayerDisconnect(EventGameInit e, GameEventInfo info)
130+
public HookResult OnGameInit(EventGameInit e, GameEventInfo info)
121131
{
132+
if (DriverInstance?.ActiveMenuStates is null)
133+
return HookResult.Continue;
134+
122135
foreach (var state in DriverInstance!.ActiveMenuStates)
123136
state.ForceRefresh = true;
124137
return HookResult.Continue;
125138
}
126139

127140
[GameEventHandler(HookMode.Post)]
128-
public HookResult OnPlayerDisconnect(EventPlayerSpawned e, GameEventInfo info)
141+
public HookResult OnPlayerSpawned(EventPlayerSpawned e, GameEventInfo info)
129142
{
143+
if (DriverInstance?.ActiveMenuStates is null)
144+
return HookResult.Continue;
145+
130146
foreach (var state in DriverInstance!.ActiveMenuStates)
131147
if (e.Userid == state.Player)
132148
state.ForceRefresh = true;
@@ -255,13 +271,20 @@ private unsafe HookResult ProcessUserCmds(DynamicHook hook)
255271
var pressingTab = false;
256272
var pressingReload = false;
257273

274+
int? inhibitWeaponSelectionTick = menuState.InhibitWeaponSelection;
275+
menuState.InhibitWeaponSelection = null;
276+
bool inhibitWeaponSelect = false;
277+
258278
for (ulong i = 0; i < (ulong)cmdsCount; i++)
259279
{
260280
var cmd = (CUserCmdPB*)((ulong)cmdsPtr + i * CUserCmdPB.Size);
261281

262282
if ((nint)cmd->Base == nint.Zero)
263283
continue;
264284

285+
inhibitWeaponSelect |= inhibitWeaponSelectionTick.HasValue && inhibitWeaponSelectionTick.Value <= cmd->Base->ClientTick;
286+
menuState.ClientTick = cmd->Base->ClientTick;
287+
265288
var cmdPtr = (nint)cmd->Base;
266289
var span = new Span<byte>(cmd->Base, 0x82);
267290

@@ -283,7 +306,8 @@ private unsafe HookResult ProcessUserCmds(DynamicHook hook)
283306

284307
if (menuState.IsUsingKeybinds)
285308
{
286-
cmd->Base->WeaponSelect = 0;
309+
if (inhibitWeaponSelect)
310+
cmd->Base->WeaponSelect = 0;
287311
}
288312
else
289313
{

0 commit comments

Comments
 (0)