Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting handlers, only if it is required by plugins #125

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions python/minqlx/_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
import re

_re_vote = re.compile(r"^(?P<cmd>[^ ]+)(?: \"?(?P<args>.*?)\"?)?$")
hot_plugged_events = (
"frame",
"player_connect",
"player_loaded",
"player_disconnect",
"player_spawn",
"kamikaze_use",
"kamikaze_explode",
"damage",
)

# ====================================================================
# EVENTS
Expand Down Expand Up @@ -130,6 +140,9 @@ def add_hook(self, plugin, handler, priority=minqlx.PRI_NORMAL):
if self.need_zmq_stats_enabled and not bool(int(minqlx.get_cvar("zmq_stats_enable"))):
raise AssertionError("{} hook requires zmq_stats_enabled cvar to have nonzero value".format(self.name))

if self.name in hot_plugged_events and len(self.plugins) == 0:
minqlx.register_handler(self.name, getattr(minqlx, f"handle_{self.name}"))

if plugin not in self.plugins:
# Initialize tuple.
self.plugins[plugin] = ([], [], [], [], []) # 5 priority levels.
Expand Down Expand Up @@ -157,6 +170,9 @@ def remove_hook(self, plugin, handler, priority=minqlx.PRI_NORMAL):
for hook in self.plugins[plugin][priority]:
if handler == hook:
self.plugins[plugin][priority].remove(handler)
if self.name in hot_plugged_events and len(self.plugins) == 0:
minqlx.register_handler(self.name, None)

return

raise ValueError("The event has not been hooked with the handler provided")
Expand Down
8 changes: 0 additions & 8 deletions python/minqlx/_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,14 +499,6 @@ def register_handlers():
minqlx.register_handler("rcon", handle_rcon)
minqlx.register_handler("client_command", handle_client_command)
minqlx.register_handler("server_command", handle_server_command)
minqlx.register_handler("frame", handle_frame)
minqlx.register_handler("new_game", handle_new_game)
minqlx.register_handler("set_configstring", handle_set_configstring)
minqlx.register_handler("player_connect", handle_player_connect)
minqlx.register_handler("player_loaded", handle_player_loaded)
minqlx.register_handler("player_disconnect", handle_player_disconnect)
minqlx.register_handler("player_spawn", handle_player_spawn)
minqlx.register_handler("console_print", handle_console_print)

minqlx.register_handler("kamikaze_use", handle_kamikaze_use)
minqlx.register_handler("kamikaze_explode", handle_kamikaze_explode)