From 1c78fd346db7cd5bab4569d07ee2cb74e8e88047 Mon Sep 17 00:00:00 2001 From: Auke Kok Date: Mon, 20 Feb 2017 14:40:37 -0800 Subject: [PATCH] Sethome: Migrate sethome mod to player attributes. Migrates settings safely and evacuates the `homes` file entirely over time. --- mods/sethome/init.lua | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/mods/sethome/init.lua b/mods/sethome/init.lua index e0fc453d..13a33e57 100644 --- a/mods/sethome/init.lua +++ b/mods/sethome/init.lua @@ -5,9 +5,9 @@ local homes_file = minetest.get_worldpath() .. "/homes" local homepos = {} local function loadhomes() - local input, err = io.open(homes_file, "r") + local input = io.open(homes_file, "r") if not input then - return minetest.log("info", "Could not load player homes file: " .. err) + return -- no longer an error end -- Iterate over all stored positions in the format "x y z player" for each line @@ -24,11 +24,13 @@ sethome.set = function(name, pos) if not player or not pos then return false end + player:set_attribute("sethome:home", minetest.pos_to_string(pos)) + -- remove `name` from the old storage file local data = {} - local output, err = io.open(homes_file, "w") + local output = io.open(homes_file, "w") if output then - homepos[name] = pos + homepos[name] = nil for i, v in pairs(homepos) do table.insert(data, string.format("%.1f %.1f %.1f %s\n", v.x, v.y, v.z, i)) end @@ -36,12 +38,18 @@ sethome.set = function(name, pos) io.close(output) return true end - minetest.log("action", "Unable to write to player homes file: " .. err) - return false + return true -- if the file doesn't exist - don't return an error. end sethome.get = function(name) - local pos = homepos[name] + local player = minetest.get_player_by_name(name) + local pos = minetest.string_to_pos(player:get_attribute("sethome:home")) + if pos then + return pos + end + + -- fetch old entry from storage table + pos = homepos[name] if pos then return vector.new(pos) else @@ -50,9 +58,10 @@ sethome.get = function(name) end sethome.go = function(name) + local pos = sethome.get(name) local player = minetest.get_player_by_name(name) - if player and homepos[name] then - player:setpos(homepos[name]) + if player and pos then + player:setpos(pos) return true end return false