Skip to content
Permalink
Browse files

Sethome: Migrate sethome mod to player attributes.

Migrates settings safely and evacuates the `homes` file
entirely over time.
  • Loading branch information...
sofar authored and paramat committed Feb 20, 2017
1 parent da69fcd commit 1c78fd346db7cd5bab4569d07ee2cb74e8e88047
Showing with 18 additions and 9 deletions.
  1. +18 −9 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,24 +24,32 @@ 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
output:write(table.concat(data))
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

0 comments on commit 1c78fd3

Please sign in to comment.
You can’t perform that action at this time.