From 66dcd6bbd75d06eb2cd7319a724000689d41d175 Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Tue, 15 Oct 2024 17:31:40 -0700 Subject: [PATCH] added suppression --- .../utils/notify/notify.lua | 71 ++++++++++++++++--- 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/worldeditadditions_core/utils/notify/notify.lua b/worldeditadditions_core/utils/notify/notify.lua index 994c652..83556e7 100644 --- a/worldeditadditions_core/utils/notify/notify.lua +++ b/worldeditadditions_core/utils/notify/notify.lua @@ -10,6 +10,7 @@ local globalstate = { -- Remember to connect Notify.get_player_suppressed in -- ..\worldeditadditions_commands\player_notify_suppress.lua suppressed_players = {}, + -- Mostly for testing, may make get() and set() for this in future on_error_send_all = false, } @@ -22,6 +23,8 @@ local Notify = {} -- Local send handler 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 local sucess, details = validate.all(name, message, colour) -- Report errors if any @@ -55,13 +58,13 @@ local send = function(name, ntype, message, colour, message_coloured) return true end ---- Send a notification of type `ntype`. +--- Send a notification of type `ntype` (for metatable). -- (Same as `Notify[ntype](name, message)`) -- @param string name The name of the player to send the notification to. -- @param string ntype The type of notification. -- @param string message The message to send. -- @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 Notify.error(name, "Invalid notification type: " .. ntype) Notify.error(name, "Message: " .. message) @@ -71,6 +74,8 @@ function Notify.__call(name, ntype, message) return true end +setmetatable(Notify, {__call = call}) + --- Send a custom notification. -- @param string name The name of the player to send the notification to. -- @param string ntype The type of notification. @@ -79,12 +84,8 @@ end -- @param message_coloured boolean? Optional. Whether the message should be coloured. -- @return boolean True if all parameters are valid, false otherwise. -- @example Predefined notification types --- Notify.error(name, message) --- Notify.errinfo(name, message) -- For verbose errors and stack traces --- Notify.warn(name, message) --- Notify.wrninfo(name, message) -- For verbose warnings and stack traces --- Notify.ok(name, message) --- Notify.info(name, message) +-- Notify.custom(name, "custom", "This one is magenta!", "#FF00FF", true) +-- Notify.custom(name, "custom", "This one is gold with white text!", "#FFC700") function Notify.custom(name, ntype, message, colour, message_coloured) if not colour then colour = "#FFFFFF" end return send(name, ntype, message, colour, message_coloured) @@ -92,6 +93,13 @@ end --- 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 local type_colours = { error = {"#FF5555", true}, @@ -106,4 +114,51 @@ do end end +--- Local suppression status handler +-- - @param name : The name of the player to check. +-- - @param suppress : The table of suppressed players. +-- - @return : 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 name : The name of the player to suppress. +-- - @param time 1>: 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 \ No newline at end of file