init.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. if not player then
  46. return false, S("This command can only be executed in-game!")
  47. end
  48. local player_meta = player:get_meta()
  49. local pos = minetest.string_to_pos(player_meta:get_string("sethome:home"))
  50. if pos then
  51. return pos
  52. end
  53. -- fetch old entry from storage table
  54. pos = homepos[name]
  55. if pos then
  56. return vector.new(pos)
  57. else
  58. return nil
  59. end
  60. end
  61. sethome.go = function(name)
  62. local pos = sethome.get(name)
  63. local player = minetest.get_player_by_name(name)
  64. if player and pos then
  65. player:set_pos(pos)
  66. return true
  67. end
  68. return false
  69. end
  70. minetest.register_privilege("home", {
  71. description = S("Can use /sethome and /home"),
  72. give_to_singleplayer = false
  73. })
  74. minetest.register_chatcommand("home", {
  75. description = S("Teleport you to your home point"),
  76. privs = {home = true},
  77. func = function(name)
  78. local player = minetest.get_player_by_name(name)
  79. if not player then
  80. return false, S("This command can only be executed in-game!")
  81. end
  82. if sethome.go(name) then
  83. return true, S("Teleported to home!")
  84. end
  85. return false, S("Set a home using /sethome")
  86. end,
  87. })
  88. minetest.register_chatcommand("sethome", {
  89. description = S("Set your home point"),
  90. privs = {home = true},
  91. func = function(name)
  92. name = name or "" -- fallback to blank name if nil
  93. local player = minetest.get_player_by_name(name)
  94. if player and sethome.set(name, player:get_pos()) then
  95. return true, S("Home set!")
  96. end
  97. return false, S("Player not found!")
  98. end,
  99. })