misc.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. core.register_on_joinplayer(function(player)
  38. local player_name = player:get_player_name()
  39. player_list[player_name] = player
  40. if not minetest.is_singleplayer() then
  41. core.chat_send_all("*** " .. player_name .. " joined the game.")
  42. end
  43. end)
  44. core.register_on_leaveplayer(function(player, timed_out)
  45. local player_name = player:get_player_name()
  46. player_list[player_name] = nil
  47. local announcement = "*** " .. player_name .. " left the game."
  48. if timed_out then
  49. announcement = announcement .. " (timed out)"
  50. end
  51. core.chat_send_all(announcement)
  52. end)
  53. function core.get_connected_players()
  54. local temp_table = {}
  55. for index, value in pairs(player_list) do
  56. if value:is_player_connected() then
  57. temp_table[#temp_table + 1] = value
  58. end
  59. end
  60. return temp_table
  61. end
  62. function minetest.player_exists(name)
  63. return minetest.get_auth_handler().get_auth(name) ~= nil
  64. end
  65. -- Returns two position vectors representing a box of `radius` in each
  66. -- direction centered around the player corresponding to `player_name`
  67. function core.get_player_radius_area(player_name, radius)
  68. local player = core.get_player_by_name(player_name)
  69. if player == nil then
  70. return nil
  71. end
  72. local p1 = player:getpos()
  73. local p2 = p1
  74. if radius then
  75. p1 = vector.subtract(p1, radius)
  76. p2 = vector.add(p2, radius)
  77. end
  78. return p1, p2
  79. end
  80. function core.hash_node_position(pos)
  81. return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
  82. end
  83. function core.get_position_from_hash(hash)
  84. local pos = {}
  85. pos.x = (hash%65536) - 32768
  86. hash = math.floor(hash/65536)
  87. pos.y = (hash%65536) - 32768
  88. hash = math.floor(hash/65536)
  89. pos.z = (hash%65536) - 32768
  90. return pos
  91. end
  92. function core.get_item_group(name, group)
  93. if not core.registered_items[name] or not
  94. core.registered_items[name].groups[group] then
  95. return 0
  96. end
  97. return core.registered_items[name].groups[group]
  98. end
  99. function core.get_node_group(name, group)
  100. core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
  101. return core.get_item_group(name, group)
  102. end
  103. function core.setting_get_pos(name)
  104. local value = core.settings:get(name)
  105. if not value then
  106. return nil
  107. end
  108. return core.string_to_pos(value)
  109. end
  110. -- To be overriden by protection mods
  111. function core.is_protected(pos, name)
  112. return false
  113. end
  114. function core.record_protection_violation(pos, name)
  115. for _, func in pairs(core.registered_on_protection_violation) do
  116. func(pos, name)
  117. end
  118. end
  119. local raillike_ids = {}
  120. local raillike_cur_id = 0
  121. function core.raillike_group(name)
  122. local id = raillike_ids[name]
  123. if not id then
  124. raillike_cur_id = raillike_cur_id + 1
  125. raillike_ids[name] = raillike_cur_id
  126. id = raillike_cur_id
  127. end
  128. return id
  129. end
  130. -- HTTP callback interface
  131. function core.http_add_fetch(httpenv)
  132. httpenv.fetch = function(req, callback)
  133. local handle = httpenv.fetch_async(req)
  134. local function update_http_status()
  135. local res = httpenv.fetch_async_get(handle)
  136. if res.completed then
  137. callback(res)
  138. else
  139. core.after(0, update_http_status)
  140. end
  141. end
  142. core.after(0, update_http_status)
  143. end
  144. return httpenv
  145. end
  146. function core.close_formspec(player_name, formname)
  147. return minetest.show_formspec(player_name, formname, "")
  148. end
  149. function core.cancel_shutdown_requests()
  150. core.request_shutdown("", false, -1)
  151. end