Skip to content

Commit 80d4909

Browse files
tenplus1sfan5
authored andcommittedJul 3, 2016
Tidy sethome code, add global functions, round coords to 1 decimal
- Global functions sethome.set(name, pos) , sethome.get(name) and sethome.go(name) - Tidy: trim coords to one decimal place and write to table and output table in one go. - Add error checking - Add t4im's homepos loader
1 parent f4f9e58 commit 80d4909

File tree

1 file changed

+65
-50
lines changed

1 file changed

+65
-50
lines changed
 

‎mods/sethome/init.lua

+65-50
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,80 @@
1+
2+
sethome = {}
3+
14
local homes_file = minetest.get_worldpath() .. "/homes"
25
local homepos = {}
36

47
local function loadhomes()
5-
local input = io.open(homes_file, "r")
6-
if input then
7-
repeat
8-
local x = input:read("*n")
9-
if x == nil then
10-
break
11-
end
12-
local y = input:read("*n")
13-
local z = input:read("*n")
14-
local name = input:read("*l")
15-
homepos[name:sub(2)] = {x = x, y = y, z = z}
16-
until input:read(0) == nil
17-
io.close(input)
18-
else
19-
homepos = {}
20-
end
8+
local input, err = io.open(homes_file, "r")
9+
if not input then
10+
return minetest.log("info", "Could not load player homes file: " .. err)
11+
end
12+
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)
16+
end
17+
input:close()
2118
end
2219

2320
loadhomes()
2421

25-
minetest.register_privilege("home", "Can use /sethome and /home")
22+
sethome.set = function(name, pos)
23+
local player = minetest.get_player_by_name(name)
24+
if not player or not pos then
25+
return false
26+
end
27+
28+
local data = {}
29+
local output, err = io.open(homes_file, "w")
30+
if output then
31+
homepos[name] = pos
32+
for i, v in pairs(homepos) do
33+
table.insert(data, string.format("%.1f %.1f %.1f %s\n", v.x, v.y, v.z, i))
34+
end
35+
output:write(table.concat(data))
36+
io.close(output)
37+
return true
38+
end
39+
minetest.log("action", "Unable to write to player homes file: " .. err)
40+
return false
41+
end
2642

27-
local changed = false
43+
sethome.get = function(name)
44+
return homepos[name]
45+
end
46+
47+
sethome.go = function(name)
48+
local player = minetest.get_player_by_name(name)
49+
if player and homepos[name] then
50+
player:setpos(homepos[name])
51+
return true
52+
end
53+
return false
54+
end
55+
56+
minetest.register_privilege("home", "Can use /sethome and /home")
2857

2958
minetest.register_chatcommand("home", {
30-
description = "Teleport you to your home point",
31-
privs = {home=true},
32-
func = function(name)
33-
local player = minetest.get_player_by_name(name)
34-
if player == nil then
35-
-- just a check to prevent the server crashing
36-
return false
37-
end
38-
if homepos[player:get_player_name()] then
39-
player:setpos(homepos[player:get_player_name()])
40-
minetest.chat_send_player(name, "Teleported to home!")
41-
else
42-
minetest.chat_send_player(name, "Set a home using /sethome")
43-
end
44-
end,
59+
description = "Teleport you to your home point",
60+
privs = {home = true},
61+
func = function(name)
62+
if sethome.go(name) then
63+
return true, "Teleported to home!"
64+
end
65+
return false, "Set a home using /sethome"
66+
end,
4567
})
4668

4769
minetest.register_chatcommand("sethome", {
48-
description = "Set your home point",
49-
privs = {home=true},
50-
func = function(name)
51-
local player = minetest.get_player_by_name(name)
52-
local pos = player:getpos()
53-
homepos[player:get_player_name()] = pos
54-
minetest.chat_send_player(name, "Home set!")
55-
changed = true
56-
if changed then
57-
local output = io.open(homes_file, "w")
58-
for i, v in pairs(homepos) do
59-
output:write(v.x.." "..v.y.." "..v.z.." "..i.."\n")
60-
end
61-
io.close(output)
62-
changed = false
63-
end
64-
end,
70+
description = "Set your home point",
71+
privs = {home = true},
72+
func = function(name)
73+
name = name or "" -- fallback to blank name if nil
74+
local player = minetest.get_player_by_name(name)
75+
if player and sethome.set(name, player:getpos()) then
76+
return true, "Home set!"
77+
end
78+
return false, "Player not found!"
79+
end,
6580
})

0 commit comments

Comments
 (0)
Please sign in to comment.