1 -- Minetest: builtin/misc.lua
3 local S = core.get_translator("__builtin")
9 -- @spec core.kick_player(String, String) :: Boolean
10 function core.kick_player(player_name, reason)
11 if type(reason) == "string" then
12 reason = "Kicked: " .. reason
16 return core.disconnect_player(player_name, reason)
19 function core.check_player_privs(name, ...)
20 if core.is_player(name) then
21 name = name:get_player_name()
22 elseif type(name) ~= "string" then
23 error("core.check_player_privs expects a player or playername as " ..
27 local requested_privs = {...}
28 local player_privs = core.get_player_privs(name)
29 local missing_privileges = {}
31 if type(requested_privs[1]) == "table" then
32 -- We were provided with a table like { privA = true, privB = true }.
33 for priv, value in pairs(requested_privs[1]) do
34 if value and not player_privs[priv] then
35 missing_privileges[#missing_privileges + 1] = priv
39 -- Only a list, we can process it directly.
40 for key, priv in pairs(requested_privs) do
41 if not player_privs[priv] then
42 missing_privileges[#missing_privileges + 1] = priv
47 if #missing_privileges > 0 then
48 return false, missing_privileges
55 function core.send_join_message(player_name)
56 if not core.is_singleplayer() then
57 core.chat_send_all("*** " .. S("@1 joined the game.", player_name))
62 function core.send_leave_message(player_name, timed_out)
63 local announcement = "*** " .. S("@1 left the game.", player_name)
65 announcement = "*** " .. S("@1 left the game (timed out).", player_name)
67 core.chat_send_all(announcement)
71 core.register_on_joinplayer(function(player)
72 local player_name = player:get_player_name()
73 if not core.is_singleplayer() then
74 local status = core.get_server_status(player_name, true)
75 if status and status ~= "" then
76 core.chat_send_player(player_name, status)
79 core.send_join_message(player_name)
83 core.register_on_leaveplayer(function(player, timed_out)
84 local player_name = player:get_player_name()
85 core.send_leave_message(player_name, timed_out)
89 function core.is_player(player)
90 -- a table being a player is also supported because it quacks sufficiently
91 -- like a player if it has the is_player function
92 local t = type(player)
93 return (t == "userdata" or t == "table") and
94 type(player.is_player) == "function" and player:is_player()
98 function core.player_exists(name)
99 return core.get_auth_handler().get_auth(name) ~= nil
103 -- Returns two position vectors representing a box of `radius` in each
104 -- direction centered around the player corresponding to `player_name`
106 function core.get_player_radius_area(player_name, radius)
107 local player = core.get_player_by_name(player_name)
108 if player == nil then
112 local p1 = player:get_pos()
116 p1 = vector.subtract(p1, radius)
117 p2 = vector.add(p2, radius)
124 -- To be overriden by protection mods
126 function core.is_protected(pos, name)
131 function core.record_protection_violation(pos, name)
132 for _, func in pairs(core.registered_on_protection_violation) do
137 -- To be overridden by Creative mods
139 local creative_mode_cache = core.settings:get_bool("creative_mode")
140 function core.is_creative_enabled(name)
141 return creative_mode_cache
144 -- Checks if specified volume intersects a protected volume
146 function core.is_area_protected(minp, maxp, player_name, interval)
147 -- 'interval' is the largest allowed interval for the 3D lattice of checks.
149 -- Compute the optimal float step 'd' for each axis so that all corners and
150 -- borders are checked. 'd' will be smaller or equal to 'interval'.
151 -- Subtracting 1e-4 ensures that the max co-ordinate will be reached by the
152 -- for loop (which might otherwise not be the case due to rounding errors).
155 interval = interval or 4
158 for _, c in pairs({"x", "y", "z"}) do
159 if minp[c] > maxp[c] then
160 -- Repair positions: 'minp' > 'maxp'
166 if maxp[c] > minp[c] then
167 d[c] = (maxp[c] - minp[c]) /
168 math.ceil((maxp[c] - minp[c]) / interval) - 1e-4
170 d[c] = 1 -- Any value larger than 0 to avoid division by zero
174 for zf = minp.z, maxp.z, d.z do
175 local z = math.floor(zf + 0.5)
176 for yf = minp.y, maxp.y, d.y do
177 local y = math.floor(yf + 0.5)
178 for xf = minp.x, maxp.x, d.x do
179 local x = math.floor(xf + 0.5)
180 local pos = vector.new(x, y, z)
181 if core.is_protected(pos, player_name) then
191 local raillike_ids = {}
192 local raillike_cur_id = 0
193 function core.raillike_group(name)
194 local id = raillike_ids[name]
196 raillike_cur_id = raillike_cur_id + 1
197 raillike_ids[name] = raillike_cur_id
204 -- HTTP callback interface
206 core.set_http_api_lua(function(httpenv)
207 httpenv.fetch = function(req, callback)
208 local handle = httpenv.fetch_async(req)
210 local function update_http_status()
211 local res = httpenv.fetch_async_get(handle)
212 if res.completed then
215 core.after(0, update_http_status)
218 core.after(0, update_http_status)
223 core.set_http_api_lua = nil
226 function core.close_formspec(player_name, formname)
227 return core.show_formspec(player_name, formname, "")
231 function core.cancel_shutdown_requests()
232 core.request_shutdown("", false, -1)
236 -- Used for callback handling with dynamic_add_media
237 core.dynamic_media_callbacks = {}
240 -- Transfer of certain globals into async environment
241 -- see builtin/async/game.lua for the other side
243 local function copy_filtering(t, seen)
244 if type(t) == "userdata" or type(t) == "function" then
245 return true -- don't use nil so presence can still be detected
246 elseif type(t) ~= "table" then
252 for k, v in pairs(t) do
253 local k_ = seen[k] or copy_filtering(k, seen)
254 local v_ = seen[v] or copy_filtering(v, seen)
260 function core.get_globals_to_transfer()
262 registered_items = copy_filtering(core.registered_items),
263 registered_aliases = core.registered_aliases,
265 return core.serialize(all)