From a0f3fb5a3d4434532bbde35055f4b464c2329bcb Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Mon, 14 Oct 2024 15:41:43 -0700 Subject: [PATCH] folderized notify --- worldeditadditions_core/init.lua | 2 +- .../utils/{ => notify}/notify.lua | 49 ++++++++------- .../utils/notify/validate.lua | 62 +++++++++++++++++++ 3 files changed, 90 insertions(+), 23 deletions(-) rename worldeditadditions_core/utils/{ => notify}/notify.lua (70%) create mode 100644 worldeditadditions_core/utils/notify/validate.lua diff --git a/worldeditadditions_core/init.lua b/worldeditadditions_core/init.lua index 95391cf..1095a9e 100644 --- a/worldeditadditions_core/init.lua +++ b/worldeditadditions_core/init.lua @@ -24,7 +24,7 @@ worldeditadditions_core = EventEmitter.new({ local wea_c = worldeditadditions_core wea_c.EventEmitter = EventEmitter -wea_c.notify = dofile(wea_c.modpath.."/utils/notify.lua") -- BEFORE anything that could use this +wea_c.notify = dofile(wea_c.modpath.."/utils/notify/notify.lua") -- BEFORE anything that could use this wea_c.Set = dofile(wea_c.modpath.."/utils/set.lua") diff --git a/worldeditadditions_core/utils/notify.lua b/worldeditadditions_core/utils/notify/notify.lua similarity index 70% rename from worldeditadditions_core/utils/notify.lua rename to worldeditadditions_core/utils/notify/notify.lua index 899a2f3..fd6cd06 100644 --- a/worldeditadditions_core/utils/notify.lua +++ b/worldeditadditions_core/utils/notify/notify.lua @@ -5,30 +5,38 @@ local set_colour = function(colour, text) return minetest.colorize(colour, text) end -local validate = { - name = function(name) - -- Very paranoid is me - if not minetest.get_player_by_name then return false end - local player = minetest.get_player_by_name(name) - return (player and player:is_player()) or false -- Paranoid is me - end, - message = function(message) return type(message) == "string" end, - colour = function(colour) - return ( type(colour) == "string" and - colour:match("^#%x+$") and - (#colour == 4 or #colour == 7) ) - end +local globalstate = { + -- Remember to connect Notify.get_player_suppressed in + -- ..\worldeditadditions_commands\player_notify_suppress.lua + suppressed_players = {}, + on_error_send_all = false, } -validate.all = function(name, message, colour) - return validate.name(name) and - validate.message(message) and - validate.colour(colour) -end +local validate = dofile(worldeditadditions_core.modpath .. + "/utils/notify/validate.lua") + +-- @class worldeditadditions_core.notify +local Notify = {} + +-- Local send handler local send = function(name, ntype, message, colour, message_coloured) -- Do validation - if not validate.all(name, message, colour) then + local sucess, details = validate.all(name, message, colour) + -- Report errors if any + if not sucess then + if not details.name then + -- Send error to all players or log it + if globalstate.on_error_send_all then + minetest.chat_send_all(details.name_err) + else minetest.log("error", details.name_err) end + elseif not details.message then + Notify.error(name, "Invalid message: " .. tostring(message) .. + " is not a string. " .. debug.traceback()) + elseif not details.colour then + Notify.error(name, "Invalid colour: " .. tostring(colour) .. ".\n" .. + "Message: " .. message .. "\n" .. debug.traceback()) + end return false end -- Colour the message if applicable @@ -44,9 +52,6 @@ local send = function(name, ntype, message, colour, message_coloured) return true end --- @class worldeditadditions_core.notify -local Notify = {} - --- Send a notification of type `ntype`. -- (Same as `Notify[ntype](name, message)`) -- @param name : The name of the player to send the notification to. diff --git a/worldeditadditions_core/utils/notify/validate.lua b/worldeditadditions_core/utils/notify/validate.lua new file mode 100644 index 0000000..72a6ebb --- /dev/null +++ b/worldeditadditions_core/utils/notify/validate.lua @@ -0,0 +1,62 @@ +--- Validation functions for WorldEditAdditions notifications. + +-- Helper functions +local log_error = function(message) + minetest.log("error", "[-- WEA :: error --] " .. message) +end + +-- @class validate +local validate = {} + +--- Validate name +-- - @param name : The name of the player to validate. +-- - @returns : 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" .. debug.traceback()) + return false + elseif not minetest.get_player_by_name then -- Very paranoid is me + log_error("minetest.get_player_by_name is not a function. THIS SHOULD NEVER HAPPEN.") + return false + end + -- Check if the player exists and is online + local player = minetest.get_player_by_name(name) + return (player and player:is_player()) or + ("Player \"" .. name .. "\" is not online or does not exist. " .. debug.traceback()) +end + +--- Validate message +-- - @param message : The message to validate. +-- - @returns : True if the message is a string, false otherwise. +validate.message = function(message) return type(message) == "string" end + +--- Validate colour +-- - @param colour : The colour to validate. +-- - @returns : True if the colour is valid, false otherwise. +validate.colour = function(colour) + return ( type(colour) == "string" and + colour:match("^#%x+$") and + (#colour == 4 or #colour == 7) ) +end + +--- Validate all +-- - @param name : The name of the player to validate. +-- - @param message : The message to validate. +-- - @param colour : The colour to validate. +-- - @returns , : +-- - : True if all parameters are valid, false otherwise. +-- - : A table containing the fail state of the parameters +-- - or nil if player name is invalid. +validate.all = function(name, message, colour) + local name_checked = validate.name(name) + local failed = { + name = name_checked == true, + name_err = type(name_checked) == "string" and name_checked, + message = validate.message(message), + colour = validate.colour(colour), + } + return failed.name and failed.message and failed.colour, failed +end + +return validate \ No newline at end of file