init.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. local data = {}
  27. local output = io.open(homes_file, "w")
  28. if output then
  29. homepos[name] = nil
  30. for i, v in pairs(homepos) do
  31. table.insert(data, string.format("%.1f %.1f %.1f %s\n", v.x, v.y, v.z, i))
  32. end
  33. output:write(table.concat(data))
  34. io.close(output)
  35. return true
  36. end
  37. return true -- if the file doesn't exist - don't return an error.
  38. end
  39. sethome.get = function(name)
  40. local player = minetest.get_player_by_name(name)
  41. local pos = minetest.string_to_pos(player:get_attribute("sethome:home"))
  42. if pos then
  43. return pos
  44. end
  45. -- fetch old entry from storage table
  46. pos = homepos[name]
  47. if pos then
  48. return vector.new(pos)
  49. else
  50. return nil
  51. end
  52. end
  53. sethome.go = function(name)
  54. local pos = sethome.get(name)
  55. local player = minetest.get_player_by_name(name)
  56. if player and pos then
  57. player:set_pos(pos)
  58. return true
  59. end
  60. return false
  61. end
  62. minetest.register_privilege("home", {
  63. description = S("Can use /sethome and /home"),
  64. give_to_singleplayer = false
  65. })
  66. minetest.register_chatcommand("home", {
  67. description = S("Teleport you to your home point"),
  68. privs = {home = true},
  69. func = function(name)
  70. if sethome.go(name) then
  71. return true, S("Teleported to home!")
  72. end
  73. return false, S("Set a home using /sethome")
  74. end,
  75. })
  76. minetest.register_chatcommand("sethome", {
  77. description = S("Set your home point"),
  78. privs = {home = true},
  79. func = function(name)
  80. name = name or "" -- fallback to blank name if nil
  81. local player = minetest.get_player_by_name(name)
  82. local pos = player:get_pos()
  83. if minetest.is_protected(pos, name) then
  84. return false, S("Could not set home!")
  85. else
  86. if player and sethome.set(name, player:get_pos()) then
  87. return true, S("Home set!")
  88. end
  89. end
  90. end,
  91. })