mirror of
https://github.com/mt-mods/mail.git
synced 2025-04-30 08:21:44 -04:00
Add API callback specifically for players receiving mail
This commit is contained in:
parent
3046f46414
commit
c7f631664e
3 changed files with 21 additions and 5 deletions
5
api.lua
5
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)
|
||||
|
|
9
api.md
9
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, msg)
|
||||
-- "player" is the name of a recipient; "msg" is a mail object (see "Mail format")
|
||||
end)
|
||||
```
|
||||
|
||||
# Recipient handler
|
||||
Recipient handlers are registered using
|
||||
|
||||
|
|
|
@ -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,18 @@ 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)
|
||||
return true, function(msg)
|
||||
for _, on_player_receive in ipairs(mail.registered_on_player_receives) do
|
||||
if on_player_receive(pname, msg) then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue