diff options
author | HybridDog <3192173+HybridDog@users.noreply.github.com> | 2021-03-13 11:18:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-13 11:18:25 +0100 |
commit | 88b052cbea346fd29120837f5b802427bc889be2 (patch) | |
tree | 6229a14001e4c343e06b54e52ad38fab549cd0ae /builtin/game | |
parent | 051bc9e6624c63c3612e041644ec587b328bae55 (diff) | |
download | minetest-88b052cbea346fd29120837f5b802427bc889be2.tar.gz minetest-88b052cbea346fd29120837f5b802427bc889be2.tar.bz2 minetest-88b052cbea346fd29120837f5b802427bc889be2.zip |
Chatcommands: Show the execution time if the command takes a long time (#10472)
Diffstat (limited to 'builtin/game')
-rw-r--r-- | builtin/game/chat.lua | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/builtin/game/chat.lua b/builtin/game/chat.lua index e05e83a27..bf2d7851e 100644 --- a/builtin/game/chat.lua +++ b/builtin/game/chat.lua @@ -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 @@ -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"] @@ -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, |