From c52a7d49daf25643f6943f79d0684a8353f993b8 Mon Sep 17 00:00:00 2001 From: Bram van den Heuvel Date: Tue, 20 Feb 2024 12:18:54 +0100 Subject: [PATCH] Lua HTTP bug workaround --- init.lua | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/init.lua b/init.lua index ce1239d..b6a8c7c 100644 --- a/init.lua +++ b/init.lua @@ -5,13 +5,15 @@ local function load(name) end -- Load HTTP privileges -local http_api = minetest.request_http_api and minetest.request_http_api() -if http_api == nil then - error( - "This mod has no access to HTTP requests - which is crucial for it ".. - "to relay messages to Matrix. Please enable it at the ".. - "`secure.http_mods` or the `secure.trusted_mods` settings!" - ) +local function get_api() + local http_api = minetest.request_http_api and minetest.request_http_api() + if http_api == nil then + error( + "This mod has no access to HTTP requests - which is crucial for it ".. + "to relay messages to Matrix. Please enable it at the ".. + "`secure.http_mods` or the `secure.trusted_mods` settings!" + ) + end end -- Load standard configuration @@ -30,24 +32,24 @@ load("lua/send") load("lua/sync") -- Mod Minetest server -minetest.register_on_chat_message(matrix.say(http_api)) +minetest.register_on_chat_message(matrix.say(get_api())) minetest.register_on_joinplayer(function(ObjectRef, last_login) local name = ObjectRef:get_player_name() - matrix.send(http_api, name .. " has joined the Minetest server") + matrix.send(get_api(), name .. " has joined the Minetest server") end) minetest.register_on_leaveplayer(function(ObjectRef, timed_out) local name = ObjectRef:get_player_name() - matrix.send(http_api, name .. " has left the Minetest server") + matrix.send(get_api(), name .. " has left the Minetest server") end) minetest.register_on_dieplayer(function(ObjectRef, reason) local name = ObjectRef:get_player_name() - matrix.send(http_api, name .. " has died! Reason:", reason) + matrix.send(get_api(), name .. " has died! Reason:", reason) end) -- Start Matrix sync local names = {} -matrix.sync_forever(http_api, function(event) +matrix.sync_forever(get_api(), function(event) local name = names[event.sender] or event.sender if event.type == nil then