2024-10-13 20:52:27 +00:00
|
|
|
--- A player notification system for worldeditadditions.
|
|
|
|
|
|
|
|
-- Helper functions
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
validate.all = function(name, message, colour)
|
|
|
|
return validate.name(name) and
|
|
|
|
validate.message(message) and
|
|
|
|
validate.colour(colour)
|
|
|
|
end
|
|
|
|
|
2024-10-13 21:23:05 +00:00
|
|
|
local send = function(name, ntype, message, colour, message_coloured)
|
2024-10-13 20:52:27 +00:00
|
|
|
-- Do validation
|
|
|
|
if not validate.all(name, message, colour) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
-- Colour the message if applicable
|
|
|
|
if message_coloured then
|
|
|
|
message = set_colour(colour, message)
|
|
|
|
end
|
|
|
|
-- Create the notification
|
|
|
|
local ret = table.concat({
|
2024-10-13 21:23:05 +00:00
|
|
|
"[--",set_colour(colour, "WEA :: " .. ntype),
|
|
|
|
"--]",message }, " ")
|
2024-10-13 20:52:27 +00:00
|
|
|
-- Send the notification
|
|
|
|
minetest.chat_send_player(name, ret)
|
|
|
|
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.
|
|
|
|
-- @param ntype <string>: The type of notification.
|
|
|
|
-- @param message <string>: The message to send.
|
|
|
|
-- @return <table>: The Notify instance.
|
|
|
|
function Notify.__call(name, ntype, message)
|
|
|
|
if ntype ~= "__call" and not Notify[ntype] then
|
|
|
|
Notify.error(name, "Invalid notification type: " .. ntype)
|
|
|
|
Notify.error(name, "Message: " .. message)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
Notify[ntype](name, message)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Send a custom notification.
|
|
|
|
-- @param name <string>: The name of the player to send the notification to.
|
2024-10-14 04:18:57 +00:00
|
|
|
-- @param ntype <string>: The type of notification.
|
2024-10-13 20:52:27 +00:00
|
|
|
-- @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.
|
2024-10-13 21:23:05 +00:00
|
|
|
function Notify.custom(name, ntype, message, colour, message_coloured)
|
2024-10-13 20:52:27 +00:00
|
|
|
if not colour then colour = "#FFFFFF" end
|
2024-10-13 21:23:05 +00:00
|
|
|
return send(name, ntype, message, colour, message_coloured)
|
2024-10-13 20:52:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Register predefined notification types.
|
|
|
|
-- @usage
|
|
|
|
-- Notify.error(name, message)
|
|
|
|
-- Notify.warn(name, message)
|
|
|
|
-- Notify.ok(name, message)
|
|
|
|
-- Notify.info(name, message)
|
|
|
|
do
|
|
|
|
local type_colours = {
|
|
|
|
error = {"#FF5555", true},
|
|
|
|
warn = {"#FFFF55", true},
|
|
|
|
ok = {"#55FF55", false},
|
|
|
|
info = {"#55B9FF", false},
|
|
|
|
}
|
|
|
|
for k, v in pairs(type_colours) do
|
|
|
|
Notify[k] = function(name, message)
|
2024-10-13 21:23:05 +00:00
|
|
|
return send(name, k, message, v[1], v[2])
|
2024-10-13 20:52:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return Notify
|