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

View File

@ -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