diff --git a/worldeditadditions_commands/commands/meta/listentities.lua b/worldeditadditions_commands/commands/meta/listentities.lua index 538b964..46fe163 100644 --- a/worldeditadditions_commands/commands/meta/listentities.lua +++ b/worldeditadditions_commands/commands/meta/listentities.lua @@ -1,6 +1,6 @@ -- Lists all currently loaded entities. -local weac = worldeditadditions_core +local wea_c = worldeditadditions_core minetest.register_chatcommand("/listentities", { params = "", @@ -22,16 +22,16 @@ minetest.register_chatcommand("/listentities", { obj_name = "[LuaEntity:__UNKNOWN__]" end end - local pos = weac.Vector3.clone(obj:get_pos()) + local pos = wea_c.Vector3.clone(obj:get_pos()) table.insert(table_vals, { id, obj_name, tostring(pos) }) end - worldedit.player_notify(name, table.concat({ + wea_c.notify.info(name, table.concat({ "Currently loaded entities:", - weac.format.make_ascii_table(table_vals), + wea_c.format.make_ascii_table(table_vals), "", "Total "..tostring(#table_vals).." objects" }, "\n")) diff --git a/worldeditadditions_commands/commands/meta/macro.lua b/worldeditadditions_commands/commands/meta/macro.lua index 62ff894..35b4ec5 100644 --- a/worldeditadditions_commands/commands/meta/macro.lua +++ b/worldeditadditions_commands/commands/meta/macro.lua @@ -41,7 +41,7 @@ local function step(params) -- If we haven't run out of values call function again minetest.after(params.delay, step, params) -- Time is in seconds else - worldedit.player_notify(params.player_name, "The macro \"".. + wea_c.notify.ok(params.player_name, "The macro \"".. params.file.."\" was completed in " .. wea_c.format.human_time(params.time)) end diff --git a/worldeditadditions_commands/commands/meta/many.lua b/worldeditadditions_commands/commands/meta/many.lua index 53b7f30..bcc7e10 100644 --- a/worldeditadditions_commands/commands/meta/many.lua +++ b/worldeditadditions_commands/commands/meta/many.lua @@ -7,7 +7,7 @@ local function step(params) local start_time = wea_c.get_ms_time() local full_cmd = params.cmd_name.." "..params.args - worldedit.player_notify(params.name, string.format("[ many | /%s ] %d / %d (~%.2f%%) complete | last: %s, average: %s, ETA: ~%s", + wea_c.notify.info(params.name, string.format("[ many | /%s ] %d / %d (~%.2f%%) complete | last: %s, average: %s, ETA: ~%s", full_cmd, (params.i + 1), params.count, ((params.i + 1) / params.count)*100, @@ -53,7 +53,7 @@ local function step(params) table.insert(done_message, table.concat(message_parts, ", ")) end table.insert(done_message, ")") - worldedit.player_notify(params.name, table.concat(done_message, "")) + wea_c.notify.ok(params.name, table.concat(done_message, "")) end end diff --git a/worldeditadditions_commands/commands/meta/multi.lua b/worldeditadditions_commands/commands/meta/multi.lua index 8e70755..4dfbf42 100644 --- a/worldeditadditions_commands/commands/meta/multi.lua +++ b/worldeditadditions_commands/commands/meta/multi.lua @@ -29,7 +29,7 @@ minetest.register_chatcommand("/multi", { if not args then args = "" end -- print("command_name", command_name) - worldedit.player_notify(name, "#"..i..": "..command) + wea_c.notify.info(name, "#"..i..": "..command) local cmd = minetest.registered_chatcommands[command_name] if not cmd then @@ -61,6 +61,6 @@ minetest.register_chatcommand("/multi", { table.insert(done_message, table.concat(message_parts, ", ")) table.insert(done_message, ")") - worldedit.player_notify(name, table.concat(done_message, "")) + wea_c.notify.ok(name, table.concat(done_message, "")) end }) diff --git a/worldeditadditions_commands/commands/selectors/pos1-2.lua b/worldeditadditions_commands/commands/selectors/pos1-2.lua index 44c2732..d8e00e0 100644 --- a/worldeditadditions_commands/commands/selectors/pos1-2.lua +++ b/worldeditadditions_commands/commands/selectors/pos1-2.lua @@ -53,7 +53,7 @@ minetest.register_chatcommand("//pos", { func = function(name, params_text) local i = tonumber(params_text) if type(i) ~= "number" then - worldedit.player_notify(name, "Error: Invalid index number given.") + weac.notify.error(name, "Error: Invalid index number given.") return end i = math.floor(i) diff --git a/worldeditadditions_commands/commands/selectors/scloud.lua b/worldeditadditions_commands/commands/selectors/scloud.lua index 07289b1..6dc75a9 100644 --- a/worldeditadditions_commands/commands/selectors/scloud.lua +++ b/worldeditadditions_commands/commands/selectors/scloud.lua @@ -13,7 +13,7 @@ minetest.register_on_punchnode(function(pos, node, puncher) if wea.add_pos[name] > 0 then wea.selection.add_point(name, pos) wea.add_pos[name] = wea.add_pos[name] - 1 - worldedit.player_notify(name, "You have "..wea.add_pos[name].." nodes left to punch") + wea_c.notify.info(name, "You have "..wea.add_pos[name].." nodes left to punch") else wea.add_pos[name] = nil end end end) diff --git a/worldeditadditions_commands/commands/selectors/unmark.lua b/worldeditadditions_commands/commands/selectors/unmark.lua index 90ac999..0c72e3b 100644 --- a/worldeditadditions_commands/commands/selectors/unmark.lua +++ b/worldeditadditions_commands/commands/selectors/unmark.lua @@ -28,7 +28,7 @@ local function do_unmark(name, params_text) if params_text == "all" then local removed = do_unmark_all() - worldedit.player_notify(name, "Hidden "..removed.." marker entities") + weac.notify.info(name, "Hidden "..removed.." marker entities") else -- Hide the WorldEditAdditions marker weac.pos.unmark(name) diff --git a/worldeditadditions_commands/commands/test/test.lua b/worldeditadditions_commands/commands/test/test.lua index 13092cc..8130275 100644 --- a/worldeditadditions_commands/commands/test/test.lua +++ b/worldeditadditions_commands/commands/test/test.lua @@ -59,6 +59,8 @@ wea_c.register_command("test", { tests[params_text[1]]:help()}, " ") end end - return tests[subcommand](name, params_text) + return wea_c.format.handle_fn_result( + tests[subcommand](name, params_text) + ) end }) \ No newline at end of file diff --git a/worldeditadditions_commands/commands/test/tests/credits.test.lua b/worldeditadditions_commands/commands/test/tests/credits.test.lua index 1b0f79d..ba41f92 100644 --- a/worldeditadditions_commands/commands/test/tests/credits.test.lua +++ b/worldeditadditions_commands/commands/test/tests/credits.test.lua @@ -23,5 +23,6 @@ worldeditadditions.normalize_test("credits", { end end send_credits(1) + return true, "Thank you!" end }) \ No newline at end of file diff --git a/worldeditadditions_commands/commands/test/tests/init.lua b/worldeditadditions_commands/commands/test/tests/init.lua index d2efccc..2535672 100644 --- a/worldeditadditions_commands/commands/test/tests/init.lua +++ b/worldeditadditions_commands/commands/test/tests/init.lua @@ -3,17 +3,13 @@ local test_id_paths = { "credits.test.lua", "notify.test.lua", "notify_bad.test.lua", - -- "notify_suppress.test.lua", - -- "stacktrace.test.lua", + "notify_suppress.test.lua", + "stacktrace.test.lua", } --- Helper functions -local update = function(a,k,v) a[k] = v end - -- Test loader local test_loader = function (path) for _, v in ipairs(test_id_paths) do dofile(path .. v) end - return ret end return test_loader \ No newline at end of file diff --git a/worldeditadditions_commands/commands/test/tests/notify.test.lua b/worldeditadditions_commands/commands/test/tests/notify.test.lua index b0d433d..dfbe1f8 100644 --- a/worldeditadditions_commands/commands/test/tests/notify.test.lua +++ b/worldeditadditions_commands/commands/test/tests/notify.test.lua @@ -9,5 +9,6 @@ worldeditadditions.normalize_test("notify", { Notify.ok(name, message) Notify.info(name, message) Notify.custom(name, "@" .. name, "Good Job", "#FF00D7") + return true end }) \ No newline at end of file diff --git a/worldeditadditions_commands/commands/test/tests/notify_bad.test.lua b/worldeditadditions_commands/commands/test/tests/notify_bad.test.lua index 8e9c374..615783b 100644 --- a/worldeditadditions_commands/commands/test/tests/notify_bad.test.lua +++ b/worldeditadditions_commands/commands/test/tests/notify_bad.test.lua @@ -23,5 +23,6 @@ worldeditadditions.normalize_test("notifybad", { Notify.warn(name, "Invalid color test:") Notify.custom(name, "bad", message, "#FF00") -- Bad colour + return true, "Test complete." end }) \ No newline at end of file diff --git a/worldeditadditions_commands/commands/test/tests/notify_suppress.test.lua b/worldeditadditions_commands/commands/test/tests/notify_suppress.test.lua index e69de29..5d4f40b 100644 --- a/worldeditadditions_commands/commands/test/tests/notify_suppress.test.lua +++ b/worldeditadditions_commands/commands/test/tests/notify_suppress.test.lua @@ -0,0 +1,20 @@ +local Notify = worldeditadditions_core.notify +worldeditadditions.normalize_test("suppress", { + params = "N/A", + description = "Tests notification suppression system.", + func = function(name, params_table) + Notify.suppress_for_player(name, 5) + Notify.warn(name, "This message should not be shown.") + Notify.suppress_for_player(name, -1) + Notify.ok(name, "This message should be shown.") + local result = Notify.suppress_for_function(name, function() + Notify.error(name, "This message should not be shown.") + return true + end) + if not result then + return false, "Error: suppress_for_function did not call function." + else + return true, "suppress_for_function called function." + end + end +}) \ No newline at end of file diff --git a/worldeditadditions_core/core/safe_function.lua b/worldeditadditions_core/core/safe_function.lua index 150e4fb..e6aa688 100644 --- a/worldeditadditions_core/core/safe_function.lua +++ b/worldeditadditions_core/core/safe_function.lua @@ -57,7 +57,7 @@ Please add any other additional specific system information here too if you thin }, "") print("DEBUG:player_notify player_name", player_name, "msg_compiled", msg_compiled) - worldedit.player_notify(player_name, msg_compiled) + weac.notify.error(player_name, msg_compiled) end diff --git a/worldeditadditions_core/core/safe_region.lua b/worldeditadditions_core/core/safe_region.lua index e85b011..2d5bc7b 100644 --- a/worldeditadditions_core/core/safe_region.lua +++ b/worldeditadditions_core/core/safe_region.lua @@ -5,6 +5,7 @@ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ███████ ██ ██ ██ ███████ ██ ██ ███████ ██████ ██ ██████ ██ ████ +local weac = worldeditadditions_core --- -- @module worldeditadditions_core local worldedit_command_y, worldedit_command_n @@ -41,7 +42,7 @@ minetest.override_chatcommand("/y", { if minetest.global_exists("worldedit") and worldedit_command_y ~= nil then worldedit_command_y(player_name) else - worldedit.player_notify(player_name, "There aren't any pending operations at the moment.") + weac.notify.info(player_name, "There aren't any pending operations at the moment.") end else pending_calls[player_name].func() @@ -58,10 +59,10 @@ minetest.override_chatcommand("/n", { if minetest.global_exists("worldedit") and worldedit_command_y ~= nil then worldedit_command_n(player_name) else - worldedit.player_notify(player_name, "There aren't any operations pending, so there's nothing to abort.") + weac.notify.info(player_name, "There aren't any operations pending, so there's nothing to abort.") end else - worldedit.player_notify(player_name, "Aborting captured command /"..pending_calls[player_name].cmdname..".") + weac.notify.info(player_name, "Aborting captured command /"..pending_calls[player_name].cmdname..".") pending_calls[player_name] = nil end end diff --git a/worldeditadditions_core/utils/format/handle_fn_result.lua b/worldeditadditions_core/utils/format/handle_fn_result.lua new file mode 100644 index 0000000..5bc5eb7 --- /dev/null +++ b/worldeditadditions_core/utils/format/handle_fn_result.lua @@ -0,0 +1,28 @@ +--- +-- @module worldeditadditions_core +local wea_c = worldeditadditions_core + +--- Handles the result of a function call. +--- @param ... any The full output of the function call. +local function handle_fn_result(...) + local result = { ... } + local ret = "" + local success = table.remove(result, 1) + if #result > 1 then + ret = wea_c.table.tostring(result) + elseif #result == 1 then + if type(result[1]) == "table" then + ret = "Function returned table:\n" .. + wea_c.table.tostring(result[1]) + else ret = tostring(result[1]) end + else + ret = table.concat({ + "Function returned \"", + tostring(success), + "\" with no other output." + }, "") + end + return success or false, ret +end + +return handle_fn_result \ No newline at end of file diff --git a/worldeditadditions_core/utils/format/init.lua b/worldeditadditions_core/utils/format/init.lua index 4e2b4ee..b9178f6 100644 --- a/worldeditadditions_core/utils/format/init.lua +++ b/worldeditadditions_core/utils/format/init.lua @@ -3,11 +3,12 @@ local wea_c = worldeditadditions_core wea_c.format = { array_2d = dofile(wea_c.modpath.."/utils/format/array_2d.lua"), + escape = dofile(wea_c.modpath.."/utils/format/escape.lua"), + handle_fn_result = dofile(wea_c.modpath.."/utils/format/handle_fn_result.lua"), human_size = dofile(wea_c.modpath.."/utils/format/human_size.lua"), human_time = dofile(wea_c.modpath.."/utils/format/human_time.lua"), - node_distribution = dofile(wea_c.modpath.."/utils/format/node_distribution.lua"), make_ascii_table = dofile(wea_c.modpath.."/utils/format/make_ascii_table.lua"), map = dofile(wea_c.modpath.."/utils/format/map.lua"), - escape = dofile(wea_c.modpath.."/utils/format/escape.lua") + node_distribution = dofile(wea_c.modpath.."/utils/format/node_distribution.lua") } diff --git a/worldeditadditions_core/utils/notify/notify.lua b/worldeditadditions_core/utils/notify/notify.lua index 6d63024..617eb71 100644 --- a/worldeditadditions_core/utils/notify/notify.lua +++ b/worldeditadditions_core/utils/notify/notify.lua @@ -1,4 +1,5 @@ --- A player notification system for worldeditadditions. +-- @module worldeditadditions_core.notify local wea_c = worldeditadditions_core -- Helper functions @@ -60,10 +61,10 @@ end --- Send a notification of type `ntype` (for metatable). -- (Same as `Notify[ntype](name, message)`) --- @param table _self Provided automatically by Lua. You do not need to set this automatically - see example. --- @param string name The name of the player to send the notification to. --- @param string ntype The type of notification. --- @param string message The message to send. +-- @param _self table Provided automatically by Lua. You do not need to set this automatically - see example. +-- @param name string The name of the player to send the notification to. +-- @param ntype string The type of notification. +-- @param message string The message to send. -- @return table The Notify instance. -- @example Basic usage -- worldeditadditions_core.notify(player_name, "info", "All registered commands:\n....") @@ -80,11 +81,11 @@ end setmetatable(Notify, {__call = call}) --- Send a custom notification. --- @param string name The name of the player to send the notification to. --- @param string ntype The type of notification. --- @param string message The message to send. --- @param string? colour Optional. The colour of the notification. --- @param boolean? message_coloured Optional. Whether the message should be coloured. +-- @param name string The name of the player to send the notification to. +-- @param ntype string The type of notification. +-- @param message string The message to send. +-- @param colour string? Optional. The colour of the notification. +-- @param message_coloured boolean? Optional. Whether the message should be coloured. -- @return boolean True if all parameters are valid, false otherwise. -- @example Custom notification types -- Notify.custom(name, "custom", "This one is magenta!", "#FF00FF", true) @@ -96,8 +97,8 @@ end --- Register the aforementioned predefined notification types. --- @param string name The name of the player to send the notification to. --- @param string message The message to send. +-- @param name string The name of the player to send the notification to. +-- @param message string The message to send. -- @example -- Notify.error(name, "multi-line error!\n" .. debug.traceback()) -- Notify.warn(name, "This is the last coloured message type!") @@ -118,8 +119,8 @@ do end --- Local suppression status handler --- @param string name The name of the player to check. --- @param table suppress The table of suppressed players. +-- @param name string The name of the player to check. +-- @param suppress table The table of suppressed players. -- @return boolean True if the player is not suppressed or -- if the player is clear(ed), false otherwise. local check_clear_suppressed = function(name, suppress) @@ -133,8 +134,8 @@ local check_clear_suppressed = function(name, suppress) end --- Suppress a player's notifications. --- @param string name The name of the player to suppress. --- @param number > 1 time The number of seconds to suppress notifications for. +-- @param name string The name of the player to suppress. +-- @param time number > 1 The number of seconds to suppress notifications for. -- number < 1 immediately removes the suppression. function Notify.suppress_for_player(name, time) local suppress = globalstate.suppressed_players @@ -148,20 +149,25 @@ function Notify.suppress_for_player(name, time) end --- Suppress a player's notifications while function executes. +-- @param name string The name of the player to suppress. +-- @param func function The function to execute while the player is suppressed. +-- @returns bool, string Success, result of the executed function. function Notify.suppress_for_function(name, func) local suppress = globalstate.suppressed_players -- If the player is already suppressed, cancel it unless it's a function if not check_clear_suppressed(name, suppress) then return false end suppress[name] = func - suppress[name]() + local result_table = { suppress[name]() } suppress[name] = nil + return wea_c.table.unpack(result_table) end --- WorldEdit compatibility --- if worldedit and type(worldedit.player_notify) == "function" then --- worldedit.player_notify = function(name, message, ntype) --- Notify(name, ntype, message) --- end --- end +if worldedit and type(worldedit.player_notify) == "function" then + worldedit.player_notify = function(name, message, ntype) + if not ntype then ntype = "info" end + Notify(name, ntype, message) + end +end return Notify \ No newline at end of file diff --git a/worldeditadditions_core/utils/notify/validate.lua b/worldeditadditions_core/utils/notify/validate.lua index dadba2a..91a29cc 100644 --- a/worldeditadditions_core/utils/notify/validate.lua +++ b/worldeditadditions_core/utils/notify/validate.lua @@ -13,8 +13,8 @@ end local validate = {} --- Validate name --- @param string name The name of the player to validate. --- @returns boolean True if the name is valid, false otherwise. +-- @param name string The name of the player to validate. +-- @returns boolean True if the name is valid, false otherwise. validate.name = function(name) if type(name) ~= "string" then log_error(tostring(name) .. " is a " .. type(name) .. " not a string.\n") @@ -30,13 +30,13 @@ validate.name = function(name) end --- Validate message --- @param string message The message to validate. --- @returns boolean True if the message is a string, false otherwise. +-- @param message string The message to validate. +-- @returns boolean True if the message is a string, false otherwise. validate.message = function(message) return type(message) == "string" end --- Validate colour --- @param string colour The colour to validate. --- @returns boolean True if the colour is valid, false otherwise. +-- @param colour string The colour to validate. +-- @returns boolean True if the colour is valid, false otherwise. validate.colour = function(colour) return ( type(colour) == "string" and colour:match("^#%x+$") and @@ -44,9 +44,9 @@ validate.colour = function(colour) end --- Validate all --- @param string name The name of the player to validate. --- @param string message The message to validate. --- @param string colour The colour to validate. +-- @param name string The name of the player to validate. +-- @param message string The message to validate. +-- @param colour string The colour to validate. -- @returns boolean, table|nil Returns the validation status, followed by details of the failure if bool == false. -- | Return arg | Meaning | -- |------------|---------|