misc.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. -- Minetest: builtin/misc.lua
  2. --
  3. -- Misc. API functions
  4. --
  5. function core.check_player_privs(name, ...)
  6. local arg_type = type(name)
  7. if (arg_type == "userdata" or arg_type == "table") and
  8. name.get_player_name then -- If it quacks like a Player...
  9. name = name:get_player_name()
  10. elseif arg_type ~= "string" then
  11. error("Invalid core.check_player_privs argument type: " .. arg_type, 2)
  12. end
  13. local requested_privs = {...}
  14. local player_privs = core.get_player_privs(name)
  15. local missing_privileges = {}
  16. if type(requested_privs[1]) == "table" then
  17. -- We were provided with a table like { privA = true, privB = true }.
  18. for priv, value in pairs(requested_privs[1]) do
  19. if value and not player_privs[priv] then
  20. missing_privileges[#missing_privileges + 1] = priv
  21. end
  22. end
  23. else
  24. -- Only a list, we can process it directly.
  25. for key, priv in pairs(requested_privs) do
  26. if not player_privs[priv] then
  27. missing_privileges[#missing_privileges + 1] = priv
  28. end
  29. end
  30. end
  31. if #missing_privileges > 0 then
  32. return false, missing_privileges
  33. end
  34. return true, ""
  35. end
  36. local player_list = {}
  37. function core.send_join_message(player_name)
  38. if not minetest.is_singleplayer() then
  39. core.chat_send_all("*** " .. player_name .. " joined the game.")
  40. end
  41. end
  42. function core.send_leave_message(player_name, timed_out)
  43. local announcement = "*** " .. player_name .. " left the game."
  44. if timed_out then
  45. announcement = announcement .. " (timed out)"
  46. end
  47. core.chat_send_all(announcement)
  48. end
  49. core.register_on_joinplayer(function(player)
  50. local player_name = player:get_player_name()
  51. player_list[player_name] = player
  52. core.send_join_message(player_name)
  53. end)
  54. core.register_on_leaveplayer(function(player, timed_out)
  55. local player_name = player:get_player_name()
  56. player_list[player_name] = nil
  57. core.send_leave_message(player_name, timed_out)
  58. end)
  59. function core.get_connected_players()
  60. local temp_table = {}
  61. for index, value in pairs(player_list) do
  62. if value:is_player_connected() then
  63. temp_table[#temp_table + 1] = value
  64. end
  65. end
  66. return temp_table
  67. end
  68. function minetest.player_exists(name)
  69. return minetest.get_auth_handler().get_auth(name) ~= nil
  70. end
  71. -- Returns two position vectors representing a box of `radius` in each
  72. -- direction centered around the player corresponding to `player_name`
  73. function core.get_player_radius_area(player_name, radius)
  74. local player = core.get_player_by_name(player_name)
  75. if player == nil then
  76. return nil
  77. end
  78. local p1 = player:getpos()
  79. local p2 = p1
  80. if radius then
  81. p1 = vector.subtract(p1, radius)
  82. p2 = vector.add(p2, radius)
  83. end
  84. return p1, p2
  85. end
  86. function core.hash_node_position(pos)
  87. return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
  88. end
  89. function core.get_position_from_hash(hash)
  90. local pos = {}
  91. pos.x = (hash%65536) - 32768
  92. hash = math.floor(hash/65536)
  93. pos.y = (hash%65536) - 32768
  94. hash = math.floor(hash/65536)
  95. pos.z = (hash%65536) - 32768
  96. return pos
  97. end
  98. function core.get_item_group(name, group)
  99. if not core.registered_items[name] or not
  100. core.registered_items[name].groups[group] then
  101. return 0
  102. end
  103. return core.registered_items[name].groups[group]
  104. end
  105. function core.get_node_group(name, group)
  106. core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
  107. return core.get_item_group(name, group)
  108. end
  109. function core.setting_get_pos(name)
  110. local value = core.settings:get(name)
  111. if not value then
  112. return nil
  113. end
  114. return core.string_to_pos(value)
  115. end
  116. -- To be overriden by protection mods
  117. function core.is_protected(pos, name)
  118. return false
  119. end
  120. function core.record_protection_violation(pos, name)
  121. for _, func in pairs(core.registered_on_protection_violation) do
  122. func(pos, name)
  123. end
  124. end
  125. local raillike_ids = {}
  126. local raillike_cur_id = 0
  127. function core.raillike_group(name)
  128. local id = raillike_ids[name]
  129. if not id then
  130. raillike_cur_id = raillike_cur_id + 1
  131. raillike_ids[name] = raillike_cur_id
  132. id = raillike_cur_id
  133. end
  134. return id
  135. end
  136. -- HTTP callback interface
  137. function core.http_add_fetch(httpenv)
  138. httpenv.fetch = function(req, callback)
  139. local handle = httpenv.fetch_async(req)
  140. local function update_http_status()
  141. local res = httpenv.fetch_async_get(handle)
  142. if res.completed then
  143. callback(res)
  144. else
  145. core.after(0, update_http_status)
  146. end
  147. end
  148. core.after(0, update_http_status)
  149. end
  150. return httpenv
  151. end
  152. function core.close_formspec(player_name, formname)
  153. return minetest.show_formspec(player_name, formname, "")
  154. end
  155. function core.cancel_shutdown_requests()
  156. core.request_shutdown("", false, -1)
  157. end