4 local homes_file = minetest.get_worldpath() .. "/homes"
7 local function loadhomes()
8 local input = io.open(homes_file, "r")
10 return -- no longer an error
13 -- Iterate over all stored positions in the format "x y z player" for each line
14 for pos, name in input:read("*a"):gmatch("(%S+ %S+ %S+)%s([%w_-]+)[\r\n]") do
15 homepos[name] = minetest.string_to_pos(pos)
22 sethome.set = function(name, pos)
23 local player = minetest.get_player_by_name(name)
24 if not player or not pos then
27 player:set_attribute("sethome:home", minetest.pos_to_string(pos))
29 -- remove `name` from the old storage file
31 local output = io.open(homes_file, "w")
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))
37 output:write(table.concat(data))
41 return true -- if the file doesn't exist - don't return an error.
44 sethome.get = function(name)
45 local player = minetest.get_player_by_name(name)
46 local pos = minetest.string_to_pos(player:get_attribute("sethome:home"))
51 -- fetch old entry from storage table
54 return vector.new(pos)
60 sethome.go = function(name)
61 local pos = sethome.get(name)
62 local player = minetest.get_player_by_name(name)
63 if player and pos then
70 minetest.register_privilege("home", {
71 description = "Can use /sethome and /home",
72 give_to_singleplayer = false
75 minetest.register_chatcommand("home", {
76 description = "Teleport you to your home point",
77 privs = {home = true},
79 if sethome.go(name) then
80 return true, "Teleported to home!"
82 return false, "Set a home using /sethome"
86 minetest.register_chatcommand("sethome", {
87 description = "Set your home point",
88 privs = {home = true},
90 name = name or "" -- fallback to blank name if nil
91 local player = minetest.get_player_by_name(name)
92 if player and sethome.set(name, player:get_pos()) then
93 return true, "Home set!"
95 return false, "Player not found!"