diff options
Diffstat (limited to 'builtin/game/misc.lua')
-rw-r--r-- | builtin/game/misc.lua | 86 |
1 files changed, 59 insertions, 27 deletions
diff --git a/builtin/game/misc.lua b/builtin/game/misc.lua index b8c5e16a9..e86efc50c 100644 --- a/builtin/game/misc.lua +++ b/builtin/game/misc.lua @@ -1,9 +1,21 @@ -- Minetest: builtin/misc.lua +local S = core.get_translator("__builtin") + -- -- Misc. API functions -- +-- @spec core.kick_player(String, String) :: Boolean +function core.kick_player(player_name, reason) + if type(reason) == "string" then + reason = "Kicked: " .. reason + else + reason = "Kicked." + end + return core.disconnect_player(player_name, reason) +end + function core.check_player_privs(name, ...) if core.is_player(name) then name = name:get_player_name() @@ -42,15 +54,15 @@ end function core.send_join_message(player_name) if not core.is_singleplayer() then - core.chat_send_all("*** " .. player_name .. " joined the game.") + core.chat_send_all("*** " .. S("@1 joined the game.", player_name)) end end function core.send_leave_message(player_name, timed_out) - local announcement = "*** " .. player_name .. " left the game." + local announcement = "*** " .. S("@1 left the game.", player_name) if timed_out then - announcement = announcement .. " (timed out)" + announcement = "*** " .. S("@1 left the game (timed out).", player_name) end core.chat_send_all(announcement) end @@ -117,13 +129,12 @@ end function core.get_position_from_hash(hash) - local pos = {} - pos.x = (hash % 65536) - 32768 + local x = (hash % 65536) - 32768 hash = math.floor(hash / 65536) - pos.y = (hash % 65536) - 32768 + local y = (hash % 65536) - 32768 hash = math.floor(hash / 65536) - pos.z = (hash % 65536) - 32768 - return pos + local z = (hash % 65536) - 32768 + return vector.new(x, y, z) end @@ -213,7 +224,7 @@ function core.is_area_protected(minp, maxp, player_name, interval) local y = math.floor(yf + 0.5) for xf = minp.x, maxp.x, d.x do local x = math.floor(xf + 0.5) - local pos = {x = x, y = y, z = z} + local pos = vector.new(x, y, z) if core.is_protected(pos, player_name) then return pos end @@ -239,7 +250,7 @@ end -- HTTP callback interface -function core.http_add_fetch(httpenv) +core.set_http_api_lua(function(httpenv) httpenv.fetch = function(req, callback) local handle = httpenv.fetch_async(req) @@ -255,7 +266,8 @@ function core.http_add_fetch(httpenv) end return httpenv -end +end) +core.set_http_api_lua = nil function core.close_formspec(player_name, formname) @@ -268,24 +280,44 @@ function core.cancel_shutdown_requests() end --- Callback handling for dynamic_add_media +-- Used for callback handling with dynamic_add_media +core.dynamic_media_callbacks = {} + -local dynamic_add_media_raw = core.dynamic_add_media_raw -core.dynamic_add_media_raw = nil -function core.dynamic_add_media(filepath, callback) - local ret = dynamic_add_media_raw(filepath) - if ret == false then - return ret +-- PNG encoder safety wrapper + +local o_encode_png = core.encode_png +function core.encode_png(width, height, data, compression) + if type(width) ~= "number" then + error("Incorrect type for 'width', expected number, got " .. type(width)) end - if callback == nil then - core.log("deprecated", "Calling minetest.dynamic_add_media without ".. - "a callback is deprecated and will stop working in future versions.") - else - -- At the moment async loading is not actually implemented, so we - -- immediately call the callback ourselves - for _, name in ipairs(ret) do - callback(name) + if type(height) ~= "number" then + error("Incorrect type for 'height', expected number, got " .. type(height)) + end + + local expected_byte_count = width * height * 4 + + if type(data) ~= "table" and type(data) ~= "string" then + error("Incorrect type for 'data', expected table or string, got " .. type(data)) + end + + local data_length = type(data) == "table" and #data * 4 or string.len(data) + + if data_length ~= expected_byte_count then + error(string.format( + "Incorrect length of 'data', width and height imply %d bytes but %d were provided", + expected_byte_count, + data_length + )) + end + + if type(data) == "table" then + local dataBuf = {} + for i = 1, #data do + dataBuf[i] = core.colorspec_to_bytes(data[i]) end + data = table.concat(dataBuf) end - return true + + return o_encode_png(width, height, data, compression or 6) end |