init.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. -- sethome/init.lua
  2. sethome = {}
  3. -- Load support for MT game translation.
  4. local S = minetest.get_translator("sethome")
  5. local homes_file = minetest.get_worldpath() .. "/homes"
  6. local homepos = {}
  7. local function loadhomes()
  8. local input = io.open(homes_file, "r")
  9. if not input then
  10. return -- no longer an error
  11. end
  12. -- Iterate over all stored positions in the format "x y z player" for each line
  13. for pos, name in input:read("*a"):gmatch("(%S+ %S+ %S+)%s([%w_-]+)[\r\n]") do
  14. homepos[name] = minetest.string_to_pos(pos)
  15. end
  16. input:close()
  17. end
  18. loadhomes()
  19. sethome.set = function(name, pos)
  20. local player = minetest.get_player_by_name(name)
  21. if not player or not pos then
  22. return false
  23. end
  24. player:set_attribute("sethome:home", minetest.pos_to_string(pos))
  25. -- remove `name` from the old storage file
  26. if not homepos[name] then
  27. return true
  28. end
  29. local data = {}
  30. local output = io.open(homes_file, "w")
  31. if output then
  32. homepos[name] = nil
  33. for i, v in pairs(homepos) do
  34. table.insert(data, string.format("%.1f %.1f %.1f %s\n", v.x, v.y, v.z, i))
  35. end
  36. output:write(table.concat(data))
  37. io.close(output)
  38. return true
  39. end
  40. return true -- if the file doesn't exist - don't return an error.
  41. end
  42. sethome.get = function(name)
  43. local player = minetest.get_player_by_name(name)
  44. local pos = minetest.string_to_pos(player:get_attribute("sethome:home"))
  45. if pos then
  46. return pos
  47. end
  48. -- fetch old entry from storage table
  49. pos = homepos[name]
  50. if pos then
  51. return vector.new(pos)
  52. else
  53. return nil
  54. end
  55. end
  56. sethome.go = function(name)
  57. local pos = sethome.get(name)
  58. local player = minetest.get_player_by_name(name)
  59. if player and pos then
  60. player:set_pos(pos)
  61. return true
  62. end
  63. return false
  64. end
  65. minetest.register_privilege("home", {
  66. description = S("Can use /sethome and /home"),
  67. give_to_singleplayer = false
  68. })
  69. minetest.register_chatcommand("home", {
  70. description = S("Teleport you to your home point"),
  71. privs = {home = true},
  72. func = function(name)
  73. if sethome.go(name) then
  74. return true, S("Teleported to home!")
  75. end
  76. return false, S("Set a home using /sethome")
  77. end,
  78. })
  79. minetest.register_chatcommand("sethome", {
  80. description = S("Set your home point"),
  81. privs = {home = true},
  82. func = function(name)
  83. name = name or "" -- fallback to blank name if nil
  84. local player = minetest.get_player_by_name(name)
  85. if player and sethome.set(name, player:get_pos()) then
  86. return true, S("Home set!")
  87. end
  88. return false, S("Player not found!")
  89. end,
  90. })