1515using CSSUniversalMenuAPI ;
1616using static CounterStrikeSharp . API . Core . Listeners ;
1717
18-
1918namespace SharpModMenu ;
2019
21-
2220[ MinimumApiVersion ( 314 ) ]
2321public 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