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 core.register_on_joinplayer(function(player)
46 local player_name = player:get_player_name()
47 player_list[player_name] = player
48 if not minetest.is_singleplayer() then
49 core.chat_send_all("*** " .. player_name .. " joined the game.")
53 core.register_on_leaveplayer(function(player, timed_out)
54 local player_name = player:get_player_name()
55 player_list[player_name] = nil
56 local announcement = "*** " .. player_name .. " left the game."
58 announcement = announcement .. " (timed out)"
60 core.chat_send_all(announcement)
63 function core.get_connected_players()
65 for index, value in pairs(player_list) do
66 if value:is_player_connected() then
67 temp_table[#temp_table + 1] = value
73 function minetest.player_exists(name)
74 return minetest.get_auth_handler().get_auth(name) ~= nil
77 -- Returns two position vectors representing a box of `radius` in each
78 -- direction centered around the player corresponding to `player_name`
79 function core.get_player_radius_area(player_name, radius)
80 local player = core.get_player_by_name(player_name)
85 local p1 = player:getpos()
89 p1 = vector.subtract(p1, radius)
90 p2 = vector.add(p2, radius)
96 function core.hash_node_position(pos)
97 return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
100 function core.get_position_from_hash(hash)
102 pos.x = (hash%65536) - 32768
103 hash = math.floor(hash/65536)
104 pos.y = (hash%65536) - 32768
105 hash = math.floor(hash/65536)
106 pos.z = (hash%65536) - 32768
110 function core.get_item_group(name, group)
111 if not core.registered_items[name] or not
112 core.registered_items[name].groups[group] then
115 return core.registered_items[name].groups[group]
118 function core.get_node_group(name, group)
119 core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
120 return core.get_item_group(name, group)
123 function core.setting_get_pos(name)
124 local value = core.setting_get(name)
128 return core.string_to_pos(value)
131 -- To be overriden by protection mods
132 function core.is_protected(pos, name)
136 function core.record_protection_violation(pos, name)
137 for _, func in pairs(core.registered_on_protection_violation) do
142 local raillike_ids = {}
143 local raillike_cur_id = 0
144 function core.raillike_group(name)
145 local id = raillike_ids[name]
147 raillike_cur_id = raillike_cur_id + 1
148 raillike_ids[name] = raillike_cur_id
154 -- HTTP callback interface
155 function core.http_add_fetch(httpenv)
156 httpenv.fetch = function(req, callback)
157 local handle = httpenv.fetch_async(req)
159 local function update_http_status()
160 local res = httpenv.fetch_async_get(handle)
161 if res.completed then
164 core.after(0, update_http_status)
167 core.after(0, update_http_status)
173 if minetest.setting_getbool("disable_escape_sequences") then
175 function core.get_color_escape_sequence(color)
179 function core.get_background_escape_sequence(color)
183 function core.colorize(color, message)
189 local ESCAPE_CHAR = string.char(0x1b)
190 function core.get_color_escape_sequence(color)
191 return ESCAPE_CHAR .. "(c@" .. color .. ")"
194 function core.get_background_escape_sequence(color)
195 return ESCAPE_CHAR .. "(b@" .. color .. ")"
198 function core.colorize(color, message)
199 return core.get_color_escape_sequence(color) .. message .. core.get_color_escape_sequence("#ffffff")
204 function core.close_formspec(player_name, formname)
205 return minetest.show_formspec(player_name, formname, "")