From 1df9222aef94486bbf4f5827b0afe6cd450482a2 Mon Sep 17 00:00:00 2001 From: pinkysnowman Date: Tue, 22 Sep 2015 22:43:25 -0400 Subject: [PATCH] cleaned up sethome Cleaned up the code, set it to :write() only once not in the loop, removed redundant callbacks and removed the concentrations. --- mods/sethome/init.lua | 91 +++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 51 deletions(-) diff --git a/mods/sethome/init.lua b/mods/sethome/init.lua index 590086b4..0d0d3639 100644 --- a/mods/sethome/init.lua +++ b/mods/sethome/init.lua @@ -2,64 +2,53 @@ local homes_file = minetest.get_worldpath() .. "/homes" local homepos = {} local function loadhomes() - local input = io.open(homes_file, "r") - if input then + local input = io.open(homes_file, "r") + if input then repeat - local x = input:read("*n") - if x == nil then - break - end - local y = input:read("*n") - local z = input:read("*n") - local name = input:read("*l") - homepos[name:sub(2)] = {x = x, y = y, z = z} - until input:read(0) == nil - io.close(input) - else - homepos = {} - end + local x = input:read("*n") + if x == nil then + break + end + local y = input:read("*n") + local z = input:read("*n") + local name = input:read("*l") + homepos[name:sub(2)] = {x = x, y = y, z = z} + until input:read(0) == nil + io.close(input) + end end loadhomes() minetest.register_privilege("home", "Can use /sethome and /home") -local changed = false - -minetest.register_chatcommand("home", { - description = "Teleport you to your home point", - privs = {home=true}, - func = function(name) - local player = minetest.get_player_by_name(name) - if player == nil then - -- just a check to prevent the server crashing - return false - end - if homepos[player:get_player_name()] then - player:setpos(homepos[player:get_player_name()]) - minetest.chat_send_player(name, "Teleported to home!") - else - minetest.chat_send_player(name, "Set a home using /sethome") - end - end, +minetest.register_chatcommand("home", { + description = "Teleport you to your home point", + privs = {home=true}, + func = function(name) + local player = minetest.get_player_by_name(name) + if player and homepos[name] then + player:setpos(homepos[name]) + return true, "Teleported to home!" + else + return false, "Set a home using /sethome" + end + end }) minetest.register_chatcommand("sethome", { - description = "Set your home point", - privs = {home=true}, - func = function(name) - local player = minetest.get_player_by_name(name) - 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") - for i, v in pairs(homepos) do - output:write(v.x.." "..v.y.." "..v.z.." "..i.."\n") - end - io.close(output) - changed = false - end - end, -}) + description = "Set your home point", + privs = {home=true}, + func = function(name) + local pos = minetest.get_player_by_name(name):getpos() + local output = io.open(homes_file, "w") + homepos[name] = pos + local data = {} + for i, v in pairs(homepos) do + table.insert(data,string.format("%.1f %.1f %.1f %s", v.x,v.y,v.z,i)) + end + output:write(table.concat(data),"\n") + io.close(output) + return true, "Home set!" + end +}) \ No newline at end of file