diff --git a/api.lua b/api.lua index 53bed29..f949e9e 100644 --- a/api.lua +++ b/api.lua @@ -10,6 +10,11 @@ function mail.register_on_receive(func) mail.registered_on_receives[#mail.registered_on_receives + 1] = func end +mail.registered_on_player_receives = {} +function mail.register_on_player_receive(func) + table.insert(mail.registered_on_player_receives, func) +end + mail.registered_recipient_handlers = {} function mail.register_recipient_handler(func) table.insert(mail.registered_recipient_handlers, func) diff --git a/api.md b/api.md index d2a6bc7..1a2f12c 100644 --- a/api.md +++ b/api.md @@ -34,7 +34,7 @@ local success, error = mail.send({ ``` # Hooks -On-receive mail hook: +Generic on-receive mail hook: ```lua mail.register_on_receive(function(m) @@ -42,6 +42,13 @@ mail.register_on_receive(function(m) end) ``` +Player-specific on-receive mail hook: +```lua +mail.register_on_player_receive(function(player, mail) + -- "player" is the name of a recipient; "mail" is a mail object (see "Mail format") +end) +``` + # Recipient handler Recipient handlers are registered using diff --git a/player_recipients.lua b/player_recipients.lua index 604ca6b..5e43142 100644 --- a/player_recipients.lua +++ b/player_recipients.lua @@ -1,7 +1,7 @@ local S = minetest.get_translator("mail") local has_canonical_name = minetest.get_modpath("canonical_name") -local function deliver_mail_to_player(name, msg) +mail.register_on_player_receive(function(name, msg) -- add to inbox local entry = mail.get_storage_entry(name) table.insert(entry.inbox, msg) @@ -26,14 +26,16 @@ local function deliver_mail_to_player(name, msg) local receiver_messages = receiver_entry.inbox mail.hud_update(name, receiver_messages) end -end +end) mail.register_recipient_handler(function(_, pname) if not minetest.player_exists(pname) then return nil end return true, function(mail) - deliver_mail_to_player(pname, mail) + for _, on_player_receive in ipairs(mail.registered_on_player_receives) do + on_player_receive(pname, mail) + end end end)