Skip to content

Commit

Permalink
weac.notify: fix __call
Browse files Browse the repository at this point in the history
When you do weac.notify(player_name, "info", "msg"), Lua auto-inserts a `self` table which refers to the main `Notify` table there.

This is because `local function call()` is registered via `setmetatable(Notify, ...)`, which auto-inserts `self` as the 1st argument.

To this end, this command adds a dummy 1st argument `_self` to capture this extra table to avoid all args being shifted by 1.
  • Loading branch information
sbrl committed Oct 16, 2024
1 parent 547cdb9 commit 36a6bf6
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions worldeditadditions_core/utils/notify/notify.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ end

--- 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.
-- @param table _self Provided automatically by Lua. You do not need to set this automatically - see example.
-- @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.
local call = function(name, ntype, message)
-- @example Basic usage
-- worldeditadditions_core.notify(player_name, "info", "All registered commands:\n....")
local call = function(_self, name, ntype, message)
if ntype ~= "__call" and not Notify[ntype] then
Notify.error(name, "Invalid notification type: " .. ntype)
Notify.error(name, "Message: " .. message)
Expand All @@ -83,7 +86,7 @@ setmetatable(Notify, {__call = call})
-- @param string? colour Optional. The colour of the notification.
-- @param boolean? message_coloured Optional. Whether the message should be coloured.
-- @return boolean True if all parameters are valid, false otherwise.
-- @example Predefined notification types
-- @example Custom notification types
-- 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)
Expand Down

0 comments on commit 36a6bf6

Please sign in to comment.