mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-11-29 18:33:01 +00:00
added wea_c.notify
This commit is contained in:
parent
86fd6a560d
commit
5e54d112ce
2 changed files with 98 additions and 0 deletions
|
@ -24,6 +24,7 @@ worldeditadditions_core = EventEmitter.new({
|
||||||
local wea_c = worldeditadditions_core
|
local wea_c = worldeditadditions_core
|
||||||
wea_c.EventEmitter = EventEmitter
|
wea_c.EventEmitter = EventEmitter
|
||||||
|
|
||||||
|
wea_c.notify = dofile(wea_c.modpath.."/utils/notify.lua") -- BEFORE anything that could use this
|
||||||
|
|
||||||
wea_c.Set = dofile(wea_c.modpath.."/utils/set.lua")
|
wea_c.Set = dofile(wea_c.modpath.."/utils/set.lua")
|
||||||
|
|
||||||
|
|
97
worldeditadditions_core/utils/notify.lua
Normal file
97
worldeditadditions_core/utils/notify.lua
Normal file
|
@ -0,0 +1,97 @@
|
||||||
|
--- 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
|
||||||
|
|
||||||
|
local send = function(name, message, colour, message_coloured)
|
||||||
|
-- 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({
|
||||||
|
"[--",set_colour(colour, "WEA"),"--]",message
|
||||||
|
}, " ")
|
||||||
|
-- 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.
|
||||||
|
-- @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.
|
||||||
|
function Notify.custom(name, message, colour, message_coloured)
|
||||||
|
if not colour then colour = "#FFFFFF" end
|
||||||
|
return send(name, message, colour, message_coloured)
|
||||||
|
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)
|
||||||
|
return send(name, message, v[1], v[2])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return Notify
|
Loading…
Reference in a new issue