2
0

misc.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. -- Minetest: builtin/misc.lua
  2. --
  3. -- Misc. API functions
  4. --
  5. minetest.timers_to_add = {}
  6. minetest.timers = {}
  7. minetest.register_globalstep(function(dtime)
  8. for _, timer in ipairs(minetest.timers_to_add) do
  9. table.insert(minetest.timers, timer)
  10. end
  11. minetest.timers_to_add = {}
  12. for index, timer in ipairs(minetest.timers) do
  13. timer.time = timer.time - dtime
  14. if timer.time <= 0 then
  15. timer.func(unpack(timer.args or {}))
  16. table.remove(minetest.timers,index)
  17. end
  18. end
  19. end)
  20. function minetest.after(time, func, ...)
  21. table.insert(minetest.timers_to_add, {time=time, func=func, args={...}})
  22. end
  23. function minetest.check_player_privs(name, privs)
  24. local player_privs = minetest.get_player_privs(name)
  25. local missing_privileges = {}
  26. for priv, val in pairs(privs) do
  27. if val then
  28. if not player_privs[priv] then
  29. table.insert(missing_privileges, priv)
  30. end
  31. end
  32. end
  33. if #missing_privileges > 0 then
  34. return false, missing_privileges
  35. end
  36. return true, ""
  37. end
  38. local player_list = {}
  39. minetest.register_on_joinplayer(function(player)
  40. player_list[player:get_player_name()] = player
  41. end)
  42. minetest.register_on_leaveplayer(function(player)
  43. player_list[player:get_player_name()] = nil
  44. end)
  45. function minetest.get_connected_players()
  46. local temp_table = {}
  47. for index, value in pairs(player_list) do
  48. if value:is_player_connected() then
  49. table.insert(temp_table, value)
  50. end
  51. end
  52. return temp_table
  53. end
  54. function minetest.hash_node_position(pos)
  55. return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
  56. end
  57. function minetest.get_position_from_hash(hash)
  58. local pos = {}
  59. pos.x = (hash%65536) - 32768
  60. hash = math.floor(hash/65536)
  61. pos.y = (hash%65536) - 32768
  62. hash = math.floor(hash/65536)
  63. pos.z = (hash%65536) - 32768
  64. return pos
  65. end
  66. function minetest.get_item_group(name, group)
  67. if not minetest.registered_items[name] or not
  68. minetest.registered_items[name].groups[group] then
  69. return 0
  70. end
  71. return minetest.registered_items[name].groups[group]
  72. end
  73. function minetest.get_node_group(name, group)
  74. return minetest.get_item_group(name, group)
  75. end
  76. function minetest.string_to_pos(value)
  77. local p = {}
  78. p.x, p.y, p.z = string.match(value, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
  79. if p.x and p.y and p.z then
  80. p.x = tonumber(p.x)
  81. p.y = tonumber(p.y)
  82. p.z = tonumber(p.z)
  83. return p
  84. end
  85. local p = {}
  86. p.x, p.y, p.z = string.match(value, "^%( *([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+) *%)$")
  87. if p.x and p.y and p.z then
  88. p.x = tonumber(p.x)
  89. p.y = tonumber(p.y)
  90. p.z = tonumber(p.z)
  91. return p
  92. end
  93. return nil
  94. end
  95. assert(minetest.string_to_pos("10.0, 5, -2").x == 10)
  96. assert(minetest.string_to_pos("( 10.0, 5, -2)").z == -2)
  97. assert(minetest.string_to_pos("asd, 5, -2)") == nil)
  98. function minetest.setting_get_pos(name)
  99. local value = minetest.setting_get(name)
  100. if not value then
  101. return nil
  102. end
  103. return minetest.string_to_pos(value)
  104. end
  105. -- To be overriden by protection mods
  106. function minetest.is_protected(pos, name)
  107. return false
  108. end
  109. function minetest.record_protection_violation(pos, name)
  110. for _, func in pairs(minetest.registered_on_protection_violation) do
  111. func(pos, name)
  112. end
  113. end