folderized notify

This commit is contained in:
VorTechnix 2024-10-14 15:41:43 -07:00
parent d006a91eaa
commit a0f3fb5a3d
No known key found for this signature in database
GPG Key ID: 091E91A69545D5BA
3 changed files with 90 additions and 23 deletions

@ -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")

@ -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 <string>: The name of the player to send the notification to.

@ -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 <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" .. 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 <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 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
(#colour == 4 or #colour == 7) )
end
--- Validate all
-- - @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>:
-- - <boolean>: True if all parameters are valid, false otherwise.
-- - <table|nil>: 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