Skip to content

Commit

Permalink
Chatcommands: Show the execution time if the command takes a long time (
Browse files Browse the repository at this point in the history
  • Loading branch information
HybridDog committed Mar 13, 2021
1 parent 051bc9e commit 88b052c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
20 changes: 18 additions & 2 deletions builtin/game/chat.lua
Expand Up @@ -47,6 +47,8 @@ end

core.chatcommands = core.registered_chatcommands -- BACKWARDS COMPATIBILITY

local msg_time_threshold =
tonumber(core.settings:get("chatcommand_msg_time_threshold")) or 0.1
core.register_on_chat_message(function(name, message)
if message:sub(1,1) ~= "/" then
return
Expand All @@ -73,7 +75,9 @@ core.register_on_chat_message(function(name, message)
local has_privs, missing_privs = core.check_player_privs(name, cmd_def.privs)
if has_privs then
core.set_last_run_mod(cmd_def.mod_origin)
local t_before = minetest.get_us_time()
local success, result = cmd_def.func(name, param)
local delay = (minetest.get_us_time() - t_before) / 1000000
if success == false and result == nil then
core.chat_send_player(name, "-!- "..S("Invalid command usage."))
local help_def = core.registered_chatcommands["help"]
Expand All @@ -83,8 +87,20 @@ core.register_on_chat_message(function(name, message)
core.chat_send_player(name, helpmsg)
end
end
elseif result then
core.chat_send_player(name, result)
else
if delay > msg_time_threshold then
-- Show how much time it took to execute the command
if result then
result = result ..
minetest.colorize("#f3d2ff", " (%.5g s)"):format(delay)
else
result = minetest.colorize("#f3d2ff",
"Command execution took %.5f s"):format(delay)
end
end
if result then
core.chat_send_player(name, result)
end
end
else
core.chat_send_player(name,
Expand Down
4 changes: 4 additions & 0 deletions builtin/settingtypes.txt
Expand Up @@ -1136,6 +1136,10 @@ enable_rollback_recording (Rollback recording) bool false
# @name, @message, @timestamp (optional)
chat_message_format (Chat message format) string <@name> @message

# If the execution of a chat command takes longer than this specified time in
# seconds, add the time information to the chat command message
chatcommand_msg_time_threshold (Chat command time message threshold) float 0.1

# A message to be displayed to all clients when the server shuts down.
kick_msg_shutdown (Shutdown message) string Server shutting down.

Expand Down

0 comments on commit 88b052c

Please sign in to comment.