Sethome: Migrate sethome mod to player attributes.

Migrates settings safely and evacuates the `homes` file
entirely over time.
This commit is contained in:
Auke Kok 2017-02-20 14:40:37 -08:00 committed by paramat
parent da69fcdf91
commit 1c78fd346d

@ -5,9 +5,9 @@ local homes_file = minetest.get_worldpath() .. "/homes"
local homepos = {} local homepos = {}
local function loadhomes() local function loadhomes()
local input, err = io.open(homes_file, "r") local input = io.open(homes_file, "r")
if not input then if not input then
return minetest.log("info", "Could not load player homes file: " .. err) return -- no longer an error
end end
-- Iterate over all stored positions in the format "x y z player" for each line -- 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 if not player or not pos then
return false return false
end end
player:set_attribute("sethome:home", minetest.pos_to_string(pos))
-- remove `name` from the old storage file
local data = {} local data = {}
local output, err = io.open(homes_file, "w") local output = io.open(homes_file, "w")
if output then if output then
homepos[name] = pos homepos[name] = nil
for i, v in pairs(homepos) do for i, v in pairs(homepos) do
table.insert(data, string.format("%.1f %.1f %.1f %s\n", v.x, v.y, v.z, i)) table.insert(data, string.format("%.1f %.1f %.1f %s\n", v.x, v.y, v.z, i))
end end
@ -36,12 +38,18 @@ sethome.set = function(name, pos)
io.close(output) io.close(output)
return true return true
end end
minetest.log("action", "Unable to write to player homes file: " .. err) return true -- if the file doesn't exist - don't return an error.
return false
end end
sethome.get = function(name) 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 if pos then
return vector.new(pos) return vector.new(pos)
else else
@ -50,9 +58,10 @@ sethome.get = function(name)
end end
sethome.go = function(name) sethome.go = function(name)
local pos = sethome.get(name)
local player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
if player and homepos[name] then if player and pos then
player:setpos(homepos[name]) player:setpos(pos)
return true return true
end end
return false return false