From 4b981f3482347acba159441654d859e250e78037 Mon Sep 17 00:00:00 2001 From: whosit Date: Sun, 8 Dec 2024 16:31:09 +0300 Subject: [PATCH] a simple /eval command --- init.lua | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 init.lua diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..dd90e9a --- /dev/null +++ b/init.lua @@ -0,0 +1,51 @@ +core.register_chatcommand("eval", + { + params = "", + description = "Execute and dump value into chat", + privs = { server = true }, + func = function(name, param) + if param == "" then + return false, "Gib code pls" + end + local code = "return " .. param + + -- echo input back + core.chat_send_player(name, "> " .. code) + + local func, err = loadstring(code, "usercode") + if not func then + return false, err + end + + local ok + local helper = function(...) + -- We need this helper to access returned values + -- twice - to get the number and to make a table with + -- them. + -- + -- This is a little convoluted, but it's to make sure + -- that evaluating functions like: + -- + -- (function() return 1,nil,3 end)() + -- + -- will display all returned values. + local n = select("#", ...) + local res = {...} + ok = res[1] + if n == 2 then + -- returned single value or error - just return it + return dump(res[2]) + else + -- returned multiple values: display one per line + local ret_vals = {} + for i=2, n do + table.insert(ret_vals, dump(res[i])) + end + return table.concat(ret_vals, ',\n') + end + end + local res = helper(pcall(func)) + return ok, res + end + } +)