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