From d7a4479eb382392555ece1638169aeea094acc31 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sat, 6 Mar 2021 04:05:14 +0100 Subject: Fix misleading /shutdown command syntax --- builtin/game/chat.lua | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) (limited to 'builtin/game/chat.lua') diff --git a/builtin/game/chat.lua b/builtin/game/chat.lua index 0bd12c25f..4dbcff1e2 100644 --- a/builtin/game/chat.lua +++ b/builtin/game/chat.lua @@ -1053,24 +1053,58 @@ core.register_chatcommand("days", { end }) +local function parse_shutdown_param(param) + local delay, reconnect, message + local one, two, three + one, two, three = param:match("^(%S+) +(%-r) +(.*)") + if one and two and three then + -- 3 arguments: delay, reconnect and message + return one, two, three + end + -- 2 arguments + one, two = param:match("^(%S+) +(.*)") + if one and two then + if tonumber(one) then + delay = one + if two == "-r" then + reconnect = two + else + message = two + end + elseif one == "-r" then + reconnect, message = one, two + end + return delay, reconnect, message + end + -- 1 argument + one = param:match("(.*)") + if tonumber(one) then + delay = one + elseif one == "-r" then + reconnect = one + else + message = one + end + return delay, reconnect, message +end + core.register_chatcommand("shutdown", { - params = S("[ | -1] [reconnect] []"), - description = S("Shutdown server (-1 cancels a delayed shutdown)"), + params = S("[ | -1] [-r] []"), + description = S("Shutdown server (-1 cancels a delayed shutdown, -r allows players to reconnect)"), privs = {server=true}, func = function(name, param) - local delay, reconnect, message - delay, param = param:match("^%s*(%S+)(.*)") - if param then - reconnect, param = param:match("^%s*(%S+)(.*)") + local delay, reconnect, message = parse_shutdown_param(param) + local bool_reconnect = reconnect == "-r" + if not message then + message = "" end - message = param and param:match("^%s*(.+)") or "" delay = tonumber(delay) or 0 if delay == 0 then core.log("action", name .. " shuts down server") core.chat_send_all("*** "..S("Server shutting down (operator request).")) end - core.request_shutdown(message:trim(), core.is_yes(reconnect), delay) + core.request_shutdown(message:trim(), bool_reconnect, delay) return true end, }) -- cgit v1.2.3