1 -- Minetest: builtin/misc.lua
7 function core.check_player_privs(name, ...)
8 local arg_type = type(name)
9 if (arg_type == "userdata" or arg_type == "table") and
10 name.get_player_name then -- If it quacks like a Player...
11 name = name:get_player_name()
12 elseif arg_type ~= "string" then
13 error("Invalid core.check_player_privs argument type: " .. arg_type, 2)
16 local requested_privs = {...}
17 local player_privs = core.get_player_privs(name)
18 local missing_privileges = {}
20 if type(requested_privs[1]) == "table" then
21 -- We were provided with a table like { privA = true, privB = true }.
22 for priv, value in pairs(requested_privs[1]) do
23 if value and not player_privs[priv] then
24 missing_privileges[#missing_privileges + 1] = priv
28 -- Only a list, we can process it directly.
29 for key, priv in pairs(requested_privs) do
30 if not player_privs[priv] then
31 missing_privileges[#missing_privileges + 1] = priv
36 if #missing_privileges > 0 then
37 return false, missing_privileges
43 local player_list = {}
45 function core.send_join_message(player_name)
46 if not minetest.is_singleplayer() then
47 core.chat_send_all("*** " .. player_name .. " joined the game.")
51 function core.send_leave_message(player_name, timed_out)
52 local announcement = "*** " .. player_name .. " left the game."
54 announcement = announcement .. " (timed out)"
56 core.chat_send_all(announcement)
59 core.register_on_joinplayer(function(player)
60 local player_name = player:get_player_name()
61 player_list[player_name] = player
62 core.send_join_message(player_name)
65 core.register_on_leaveplayer(function(player, timed_out)
66 local player_name = player:get_player_name()
67 player_list[player_name] = nil
68 core.send_leave_message(player_name, timed_out)
71 function core.get_connected_players()
73 for index, value in pairs(player_list) do
74 if value:is_player_connected() then
75 temp_table[#temp_table + 1] = value
81 function minetest.player_exists(name)
82 return minetest.get_auth_handler().get_auth(name) ~= nil
85 -- Returns two position vectors representing a box of `radius` in each
86 -- direction centered around the player corresponding to `player_name`
87 function core.get_player_radius_area(player_name, radius)
88 local player = core.get_player_by_name(player_name)
93 local p1 = player:getpos()
97 p1 = vector.subtract(p1, radius)
98 p2 = vector.add(p2, radius)
104 function core.hash_node_position(pos)
105 return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
108 function core.get_position_from_hash(hash)
110 pos.x = (hash%65536) - 32768
111 hash = math.floor(hash/65536)
112 pos.y = (hash%65536) - 32768
113 hash = math.floor(hash/65536)
114 pos.z = (hash%65536) - 32768
118 function core.get_item_group(name, group)
119 if not core.registered_items[name] or not
120 core.registered_items[name].groups[group] then
123 return core.registered_items[name].groups[group]
126 function core.get_node_group(name, group)
127 core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
128 return core.get_item_group(name, group)
131 function core.setting_get_pos(name)
132 local value = core.settings:get(name)
136 return core.string_to_pos(value)
139 -- To be overriden by protection mods
140 function core.is_protected(pos, name)
144 function core.record_protection_violation(pos, name)
145 for _, func in pairs(core.registered_on_protection_violation) do
150 local raillike_ids = {}
151 local raillike_cur_id = 0
152 function core.raillike_group(name)
153 local id = raillike_ids[name]
155 raillike_cur_id = raillike_cur_id + 1
156 raillike_ids[name] = raillike_cur_id
162 -- HTTP callback interface
163 function core.http_add_fetch(httpenv)
164 httpenv.fetch = function(req, callback)
165 local handle = httpenv.fetch_async(req)
167 local function update_http_status()
168 local res = httpenv.fetch_async_get(handle)
169 if res.completed then
172 core.after(0, update_http_status)
175 core.after(0, update_http_status)
181 function core.close_formspec(player_name, formname)
182 return minetest.show_formspec(player_name, formname, "")
185 function core.cancel_shutdown_requests()
186 core.request_shutdown("", false, -1)