Skip to content

Commit 62c3fbd

Browse files
committed
Fixed #299.
1 parent f2c2eed commit 62c3fbd

File tree

5 files changed

+60
-13
lines changed

5 files changed

+60
-13
lines changed

addons/source-python/docs/source-python/source/developing/module_tutorials/listeners.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,20 @@ Called when a player runs a command.
420420
pass
421421
422422
423+
OnPlayerPostRunCommand
424+
----------------------
425+
426+
Called after a player ran a command.
427+
428+
.. code-block:: python
429+
430+
from listeners import OnPlayerPostRunCommand
431+
432+
@OnPlayerPostRunCommand
433+
def on_player_post_run_command(player, user_cmd):
434+
pass
435+
436+
423437
OnPluginLoaded
424438
--------------
425439

addons/source-python/packages/source-python/listeners/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
from _listeners import on_tick_listener_manager
7474
from _listeners import on_server_output_listener_manager
7575
from _listeners import on_player_run_command_listener_manager
76+
from _listeners import on_player_post_run_command_listener_manager
7677
from _listeners import on_button_state_changed_listener_manager
7778

7879

@@ -110,6 +111,7 @@
110111
'OnNetworkidValidated',
111112
'OnButtonStateChanged',
112113
'OnPlayerRunCommand',
114+
'OnPlayerPostRunCommand',
113115
'OnPluginLoaded',
114116
'OnPluginLoading',
115117
'OnPluginUnloaded',
@@ -494,6 +496,10 @@ class OnPlayerRunCommand(ListenerManagerDecorator):
494496

495497
manager = on_player_run_command_listener_manager
496498

499+
class OnPlayerPostRunCommand(ListenerManagerDecorator):
500+
"""Register/unregister a post run command listener."""
501+
502+
manager = on_player_post_run_command_listener_manager
497503

498504
class OnButtonStateChanged(ListenerManagerDecorator):
499505
"""Register/unregister a button state change listener."""

src/core/modules/listeners/listeners_wrap.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ DEFINE_MANAGER_ACCESSOR(OnDataLoaded)
6262
DEFINE_MANAGER_ACCESSOR(OnCombinerPreCache)
6363
DEFINE_MANAGER_ACCESSOR(OnDataUnloaded)
6464
DEFINE_MANAGER_ACCESSOR(OnPlayerRunCommand)
65+
DEFINE_MANAGER_ACCESSOR(OnPlayerPostRunCommand)
6566
DEFINE_MANAGER_ACCESSOR(OnButtonStateChanged)
6667

6768
static CConVarChangedListenerManager s_OnConVarChanged;
@@ -185,5 +186,6 @@ void export_listener_managers(scope _listeners)
185186
_listeners.attr("on_server_output_listener_manager") = object(ptr((CListenerManager *)GetOnServerOutputListenerManager()));
186187

187188
_listeners.attr("on_player_run_command_listener_manager") = object(ptr(GetOnPlayerRunCommandListenerManager()));
189+
_listeners.attr("on_player_post_run_command_listener_manager") = object(ptr(GetOnPlayerPostRunCommandListenerManager()));
188190
_listeners.attr("on_button_state_changed_listener_manager") = object(ptr(GetOnButtonStateChangedListenerManager()));
189191
}

src/core/sp_hooks.cpp

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,22 @@ void InitHooks(CBaseEntity* pEntity)
159159
//---------------------------------------------------------------------------------
160160
bool PrePlayerRunCommand(HookType_t hook_type, CHook* pHook)
161161
{
162-
GET_LISTENER_MANAGER(OnPlayerRunCommand, run_command_manager);
163-
GET_LISTENER_MANAGER(OnButtonStateChanged, button_state_manager);
162+
bool bUsePreRegister = pHook->m_bUsePreRegisters;
163+
pHook->m_bUsePreRegisters = true;
164164

165-
if (!run_command_manager->GetCount() && !button_state_manager->GetCount())
166-
return false;
165+
if (hook_type == HOOKTYPE_PRE) {
166+
GET_LISTENER_MANAGER(OnPlayerRunCommand, run_command_manager);
167+
GET_LISTENER_MANAGER(OnButtonStateChanged, button_state_manager);
168+
169+
if (!run_command_manager->GetCount() && !button_state_manager->GetCount())
170+
return false;
171+
}
172+
else {
173+
GET_LISTENER_MANAGER(OnPlayerPostRunCommand, post_run_command_manager);
174+
175+
if (!post_run_command_manager->GetCount())
176+
return false;
177+
}
167178

168179
static object Player = import("players.entity").attr("Player");
169180

@@ -181,24 +192,33 @@ bool PrePlayerRunCommand(HookType_t hook_type, CHook* pHook)
181192
#endif
182193

183194
object player = Player(index);
184-
CALL_LISTENERS(OnPlayerRunCommand, player, ptr(pCmd));
185195

186-
if (button_state_manager->GetCount())
187-
{
188-
CBaseEntityWrapper* pWrapper = (CBaseEntityWrapper*) pEntity;
189-
static int offset = pWrapper->FindDatamapPropertyOffset("m_nButtons");
196+
if (hook_type == HOOKTYPE_PRE) {
197+
CALL_LISTENERS(OnPlayerRunCommand, player, ptr(pCmd));
190198

191-
int buttons = pWrapper->GetDatamapPropertyByOffset<int>(offset);
192-
if (buttons != pCmd->buttons)
199+
GET_LISTENER_MANAGER(OnButtonStateChanged, button_state_manager);
200+
if (button_state_manager->GetCount())
193201
{
194-
CALL_LISTENERS(OnButtonStateChanged, player, buttons, pCmd->buttons);
202+
CBaseEntityWrapper* pWrapper = (CBaseEntityWrapper*) pEntity;
203+
static int offset = pWrapper->FindDatamapPropertyOffset("m_nButtons");
204+
205+
int buttons = pWrapper->GetDatamapPropertyByOffset<int>(offset);
206+
if (buttons != pCmd->buttons)
207+
{
208+
CALL_LISTENERS(OnButtonStateChanged, player, buttons, pCmd->buttons);
209+
}
195210
}
196211
}
197-
212+
else {
213+
CALL_LISTENERS(OnPlayerPostRunCommand, player, ptr(pCmd));
214+
}
215+
198216
#if defined(ENGINE_BRANCH_TF2)
199217
CUserCmd* pRealCmd = pHook->GetArgument<CUserCmd*>(1);
200218
memcpy(pRealCmd, pCmd, sizeof(CUserCmd));
201219
#endif
202220

221+
pHook->m_bUsePreRegisters = bUsePreRegister;
222+
203223
return false;
204224
}

src/core/sp_main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ bool CSourcePython::Load( CreateInterfaceFn interfaceFactory, CreateInterfaceFn
268268
(HookHandlerFn*) (void*) &PrePlayerRunCommand,
269269
HOOKTYPE_PRE));
270270

271+
g_EntityHooks.push_back(new PlayerHook(
272+
"run_command",
273+
(HookHandlerFn*) (void*) &PrePlayerRunCommand,
274+
HOOKTYPE_POST));
275+
271276
InitHooks();
272277

273278
Msg(MSG_PREFIX "Loaded successfully.\n");

0 commit comments

Comments
 (0)