mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-11-01 05:43:01 +00:00
Merge branch 'dev' of github.com:sbrl/Minetest-WorldEditAdditions into dev
This commit is contained in:
commit
c352446226
8 changed files with 96 additions and 25 deletions
|
@ -21,7 +21,9 @@ local metatable = {
|
||||||
}
|
}
|
||||||
metatable.__index = metatable
|
metatable.__index = metatable
|
||||||
|
|
||||||
local normalize_test = function(test_name, def)
|
local registered_tests = {}
|
||||||
|
|
||||||
|
local register_test = function(test_name, def)
|
||||||
---
|
---
|
||||||
-- 1: Validation
|
-- 1: Validation
|
||||||
---
|
---
|
||||||
|
@ -45,8 +47,22 @@ local normalize_test = function(test_name, def)
|
||||||
-- 2: Normalisation
|
-- 2: Normalisation
|
||||||
---
|
---
|
||||||
setmetatable(def, metatable)
|
setmetatable(def, metatable)
|
||||||
|
registered_tests[test_name] = def
|
||||||
return test_name, def
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return normalize_test
|
local normalize_test = {}
|
||||||
|
normalize_test.__index = normalize_test
|
||||||
|
|
||||||
|
normalize_test.__call = function(self, test_name, def)
|
||||||
|
register_test(test_name, def)
|
||||||
|
end
|
||||||
|
|
||||||
|
normalize_test.get_registered_tests = function()
|
||||||
|
local ret = {}
|
||||||
|
for k, v in pairs(registered_tests) do
|
||||||
|
ret[k] = v
|
||||||
|
end
|
||||||
|
return ret
|
||||||
|
end
|
||||||
|
|
||||||
|
return setmetatable({}, normalize_test)
|
|
@ -10,7 +10,9 @@ local wea_cmd = worldeditadditions_commands
|
||||||
local test_dir = wea_cmd.modpath .. "/commands/test/tests/"
|
local test_dir = wea_cmd.modpath .. "/commands/test/tests/"
|
||||||
|
|
||||||
-- Load tests with init function
|
-- Load tests with init function
|
||||||
local tests = dofile(test_dir .. "init.lua")(test_dir)
|
dofile(test_dir .. "init.lua")(test_dir)
|
||||||
|
local tests = worldeditadditions.normalize_test.get_registered_tests()
|
||||||
|
|
||||||
-- Helper functions
|
-- Helper functions
|
||||||
local set_colour = function(colour, text)
|
local set_colour = function(colour, text)
|
||||||
return minetest.colorize(colour, text)
|
return minetest.colorize(colour, text)
|
||||||
|
|
|
@ -12,7 +12,7 @@ local credits = {
|
||||||
}
|
}
|
||||||
|
|
||||||
local Notify = worldeditadditions_core.notify
|
local Notify = worldeditadditions_core.notify
|
||||||
return worldeditadditions.normalize_test("credits", {
|
worldeditadditions.normalize_test("credits", {
|
||||||
params = "N\\A",
|
params = "N\\A",
|
||||||
description = "Sends WEA credits to player in info notification format.",
|
description = "Sends WEA credits to player in info notification format.",
|
||||||
func = function(name, params_table)
|
func = function(name, params_table)
|
||||||
|
|
|
@ -3,6 +3,7 @@ local test_id_paths = {
|
||||||
"credits.test.lua",
|
"credits.test.lua",
|
||||||
"notify.test.lua",
|
"notify.test.lua",
|
||||||
"notify_bad.test.lua",
|
"notify_bad.test.lua",
|
||||||
|
-- "notify_suppress.test.lua",
|
||||||
-- "stacktrace.test.lua",
|
-- "stacktrace.test.lua",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,10 +12,7 @@ local update = function(a,k,v) a[k] = v end
|
||||||
|
|
||||||
-- Test loader
|
-- Test loader
|
||||||
local test_loader = function (path)
|
local test_loader = function (path)
|
||||||
local ret = {}
|
for _, v in ipairs(test_id_paths) do dofile(path .. v) end
|
||||||
for _, v in ipairs(test_id_paths) do
|
|
||||||
update(ret, dofile(path .. v))
|
|
||||||
end
|
|
||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
local Notify = worldeditadditions_core.notify
|
local Notify = worldeditadditions_core.notify
|
||||||
return worldeditadditions.normalize_test("notify", {
|
worldeditadditions.normalize_test("notify", {
|
||||||
params = "<message>",
|
params = "<message>",
|
||||||
description = "Sends message to player in all main notification formats (error, warn, ok, info).",
|
description = "Sends message to player in all main notification formats (error, warn, ok, info).",
|
||||||
func = function(name, params_table)
|
func = function(name, params_table)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
local Notify = worldeditadditions_core.notify
|
local Notify = worldeditadditions_core.notify
|
||||||
return worldeditadditions.normalize_test("notifybad", {
|
worldeditadditions.normalize_test("notifybad", {
|
||||||
params = "N/A",
|
params = "N/A",
|
||||||
description = "Sends badly formed messages to player to test error handling.",
|
description = "Sends badly formed messages to player to test error handling.",
|
||||||
func = function(name, params_table)
|
func = function(name, params_table)
|
||||||
|
|
|
@ -10,6 +10,7 @@ local globalstate = {
|
||||||
-- Remember to connect Notify.get_player_suppressed in
|
-- Remember to connect Notify.get_player_suppressed in
|
||||||
-- ..\worldeditadditions_commands\player_notify_suppress.lua
|
-- ..\worldeditadditions_commands\player_notify_suppress.lua
|
||||||
suppressed_players = {},
|
suppressed_players = {},
|
||||||
|
-- Mostly for testing, may make get() and set() for this in future
|
||||||
on_error_send_all = false,
|
on_error_send_all = false,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +23,8 @@ local Notify = {}
|
||||||
|
|
||||||
-- Local send handler
|
-- Local send handler
|
||||||
local send = function(name, ntype, message, colour, message_coloured)
|
local send = function(name, ntype, message, colour, message_coloured)
|
||||||
|
-- Check if notifications are suppressed for the player
|
||||||
|
if globalstate.suppressed_players[name] then return false end
|
||||||
-- Do validation
|
-- Do validation
|
||||||
local sucess, details = validate.all(name, message, colour)
|
local sucess, details = validate.all(name, message, colour)
|
||||||
-- Report errors if any
|
-- Report errors if any
|
||||||
|
@ -55,13 +58,13 @@ local send = function(name, ntype, message, colour, message_coloured)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Send a notification of type `ntype`.
|
--- Send a notification of type `ntype` (for metatable).
|
||||||
-- (Same as `Notify[ntype](name, message)`)
|
-- (Same as `Notify[ntype](name, message)`)
|
||||||
-- @param string name The name of the player to send the notification to.
|
-- @param string name The name of the player to send the notification to.
|
||||||
-- @param string ntype The type of notification.
|
-- @param string ntype The type of notification.
|
||||||
-- @param string message The message to send.
|
-- @param string message The message to send.
|
||||||
-- @return table The Notify instance.
|
-- @return table The Notify instance.
|
||||||
function Notify.__call(name, ntype, message)
|
local call = function(name, ntype, message)
|
||||||
if ntype ~= "__call" and not Notify[ntype] then
|
if ntype ~= "__call" and not Notify[ntype] then
|
||||||
Notify.error(name, "Invalid notification type: " .. ntype)
|
Notify.error(name, "Invalid notification type: " .. ntype)
|
||||||
Notify.error(name, "Message: " .. message)
|
Notify.error(name, "Message: " .. message)
|
||||||
|
@ -71,20 +74,18 @@ function Notify.__call(name, ntype, message)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
setmetatable(Notify, {__call = call})
|
||||||
|
|
||||||
--- Send a custom notification.
|
--- Send a custom notification.
|
||||||
-- @param string name The name of the player to send the notification to.
|
-- @param string name The name of the player to send the notification to.
|
||||||
-- @param string ntype The type of notification.
|
-- @param string ntype The type of notification.
|
||||||
-- @param string message The message to send.
|
-- @param string message The message to send.
|
||||||
-- @param string colour optional): The colour of the notification.
|
-- @param string? colour Optional. The colour of the notification.
|
||||||
-- @param message_coloured boolean? Optional. Whether the message should be coloured.
|
-- @param boolean? message_coloured Optional. Whether the message should be coloured.
|
||||||
-- @return boolean True if all parameters are valid, false otherwise.
|
-- @return boolean True if all parameters are valid, false otherwise.
|
||||||
-- @example Predefined notification types
|
-- @example Predefined notification types
|
||||||
-- Notify.error(name, message)
|
-- Notify.custom(name, "custom", "This one is magenta!", "#FF00FF", true)
|
||||||
-- Notify.errinfo(name, message) -- For verbose errors and stack traces
|
-- Notify.custom(name, "custom", "This one is gold with white text!", "#FFC700")
|
||||||
-- Notify.warn(name, message)
|
|
||||||
-- Notify.wrninfo(name, message) -- For verbose warnings and stack traces
|
|
||||||
-- Notify.ok(name, message)
|
|
||||||
-- Notify.info(name, message)
|
|
||||||
function Notify.custom(name, ntype, message, colour, message_coloured)
|
function Notify.custom(name, ntype, message, colour, message_coloured)
|
||||||
if not colour then colour = "#FFFFFF" end
|
if not colour then colour = "#FFFFFF" end
|
||||||
return send(name, ntype, message, colour, message_coloured)
|
return send(name, ntype, message, colour, message_coloured)
|
||||||
|
@ -92,6 +93,13 @@ end
|
||||||
|
|
||||||
|
|
||||||
--- Register the aforementioned predefined notification types.
|
--- 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.
|
||||||
|
-- @example
|
||||||
|
-- Notify.error(name, "multi-line error!\n" .. debug.traceback())
|
||||||
|
-- Notify.warn(name, "This is the last coloured message type!")
|
||||||
|
-- Notify.ok(name, "I am ok!")
|
||||||
|
-- Notify.info(name, "There once was a ship that put to sea...")
|
||||||
do
|
do
|
||||||
local type_colours = {
|
local type_colours = {
|
||||||
error = {"#FF5555", true},
|
error = {"#FF5555", true},
|
||||||
|
@ -106,4 +114,51 @@ do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Local suppression status handler
|
||||||
|
-- @param string name The name of the player to check.
|
||||||
|
-- @param table suppress 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)
|
||||||
|
if suppress[name] then
|
||||||
|
if type(suppress[name]) == "function" then return false end
|
||||||
|
if suppress[name].cancel then
|
||||||
|
suppress[name]:cancel()
|
||||||
|
else suppress[name] = nil end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
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.
|
||||||
|
-- number < 1 immediately removes the suppression.
|
||||||
|
function Notify.suppress_for_player(name, time)
|
||||||
|
local suppress = globalstate.suppress
|
||||||
|
-- If the player is already suppressed, cancel it unless it's a function
|
||||||
|
if not check_clear_suppressed(name, suppress) then return false end
|
||||||
|
if time < 1 then return end
|
||||||
|
if not minetest.after then -- Just being paranoid again
|
||||||
|
Notify.warn(name, "minetest.after does not exist. THIS SHOULD NOT HAPPEN.")
|
||||||
|
Notify.warn(name, "Notifications will not be suppressed. Please open an issue!")
|
||||||
|
else suppress[name] = minetest.after(time, Notify.suppress_for_player, name, -1) end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Suppress a player's notifications while function executes.
|
||||||
|
function Notify.suppress_for_function(name, func)
|
||||||
|
local suppress = globalstate.suppress
|
||||||
|
-- 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]()
|
||||||
|
suppress[name] = nil
|
||||||
|
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
|
||||||
|
|
||||||
return Notify
|
return Notify
|
Loading…
Reference in a new issue