Skip to content

Commit 88b052c

Browse files
authoredMar 13, 2021
Chatcommands: Show the execution time if the command takes a long time (#10472)
1 parent 051bc9e commit 88b052c

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed
 

‎builtin/game/chat.lua

+18-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ end
4747

4848
core.chatcommands = core.registered_chatcommands -- BACKWARDS COMPATIBILITY
4949

50+
local msg_time_threshold =
51+
tonumber(core.settings:get("chatcommand_msg_time_threshold")) or 0.1
5052
core.register_on_chat_message(function(name, message)
5153
if message:sub(1,1) ~= "/" then
5254
return
@@ -73,7 +75,9 @@ core.register_on_chat_message(function(name, message)
7375
local has_privs, missing_privs = core.check_player_privs(name, cmd_def.privs)
7476
if has_privs then
7577
core.set_last_run_mod(cmd_def.mod_origin)
78+
local t_before = minetest.get_us_time()
7679
local success, result = cmd_def.func(name, param)
80+
local delay = (minetest.get_us_time() - t_before) / 1000000
7781
if success == false and result == nil then
7882
core.chat_send_player(name, "-!- "..S("Invalid command usage."))
7983
local help_def = core.registered_chatcommands["help"]
@@ -83,8 +87,20 @@ core.register_on_chat_message(function(name, message)
8387
core.chat_send_player(name, helpmsg)
8488
end
8589
end
86-
elseif result then
87-
core.chat_send_player(name, result)
90+
else
91+
if delay > msg_time_threshold then
92+
-- Show how much time it took to execute the command
93+
if result then
94+
result = result ..
95+
minetest.colorize("#f3d2ff", " (%.5g s)"):format(delay)
96+
else
97+
result = minetest.colorize("#f3d2ff",
98+
"Command execution took %.5f s"):format(delay)
99+
end
100+
end
101+
if result then
102+
core.chat_send_player(name, result)
103+
end
88104
end
89105
else
90106
core.chat_send_player(name,

‎builtin/settingtypes.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,10 @@ enable_rollback_recording (Rollback recording) bool false
11361136
# @name, @message, @timestamp (optional)
11371137
chat_message_format (Chat message format) string <@name> @message
11381138

1139+
# If the execution of a chat command takes longer than this specified time in
1140+
# seconds, add the time information to the chat command message
1141+
chatcommand_msg_time_threshold (Chat command time message threshold) float 0.1
1142+
11391143
# A message to be displayed to all clients when the server shuts down.
11401144
kick_msg_shutdown (Shutdown message) string Server shutting down.
11411145

0 commit comments

Comments
 (0)
Please sign in to comment.