cleaned up sethome

Cleaned up the code, set it to :write() only once not in the loop, removed redundant callbacks and removed the concentrations.
This commit is contained in:
pinkysnowman 2015-09-22 22:43:25 -04:00 committed by crazyginger72
parent 24578ca968
commit 1df9222aef

View file

@ -15,8 +15,6 @@ local function loadhomes()
homepos[name:sub(2)] = {x = x, y = y, z = z} homepos[name:sub(2)] = {x = x, y = y, z = z}
until input:read(0) == nil until input:read(0) == nil
io.close(input) io.close(input)
else
homepos = {}
end end
end end
@ -24,42 +22,33 @@ loadhomes()
minetest.register_privilege("home", "Can use /sethome and /home") minetest.register_privilege("home", "Can use /sethome and /home")
local changed = false
minetest.register_chatcommand("home", { minetest.register_chatcommand("home", {
description = "Teleport you to your home point", description = "Teleport you to your home point",
privs = {home=true}, privs = {home=true},
func = function(name) func = function(name)
local player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
if player == nil then if player and homepos[name] then
-- just a check to prevent the server crashing player:setpos(homepos[name])
return false return true, "Teleported to home!"
end
if homepos[player:get_player_name()] then
player:setpos(homepos[player:get_player_name()])
minetest.chat_send_player(name, "Teleported to home!")
else else
minetest.chat_send_player(name, "Set a home using /sethome") return false, "Set a home using /sethome"
end
end end
end,
}) })
minetest.register_chatcommand("sethome", { minetest.register_chatcommand("sethome", {
description = "Set your home point", description = "Set your home point",
privs = {home=true}, privs = {home=true},
func = function(name) func = function(name)
local player = minetest.get_player_by_name(name) local pos = minetest.get_player_by_name(name):getpos()
local pos = player:getpos()
homepos[player:get_player_name()] = pos
minetest.chat_send_player(name, "Home set!")
changed = true
if changed then
local output = io.open(homes_file, "w") local output = io.open(homes_file, "w")
homepos[name] = pos
local data = {}
for i, v in pairs(homepos) do for i, v in pairs(homepos) do
output:write(v.x.." "..v.y.." "..v.z.." "..i.."\n") table.insert(data,string.format("%.1f %.1f %.1f %s", v.x,v.y,v.z,i))
end end
output:write(table.concat(data),"\n")
io.close(output) io.close(output)
changed = false return true, "Home set!"
end end
end,
}) })