Skip to content

Commit

Permalink
Add API callback specifically for players receiving mail
Browse files Browse the repository at this point in the history
  • Loading branch information
y5nw committed Jan 13, 2024
1 parent 3046f46 commit 1910605
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
5 changes: 5 additions & 0 deletions api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion api.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,21 @@ local success, error = mail.send({
```

# Hooks
On-receive mail hook:
Generic on-receive mail hook:

```lua
mail.register_on_receive(function(m)
-- "m" is an object in the form: "Mail format"
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

Expand Down
8 changes: 5 additions & 3 deletions player_recipients.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)

Expand Down

0 comments on commit 1910605

Please sign in to comment.