Rewrite sethome mod

This commit is contained in:
PenguinDad 2014-07-06 20:29:12 +02:00
parent 5e0c49345a
commit a717827dc8

View file

@ -1,9 +1,70 @@
local homes_file = minetest.get_worldpath() .. "/homes" local homes_file = minetest.get_worldpath() .. "/homes.txt"
local homepos = {} local oldhomes_file = minetest.get_worldpath() .. "/homes"
local function loadhomes() local input, err = io.open(homes_file, "r")
local input = io.open(homes_file, "r") if err then
if input then minetest.log("error", "[sethome] Failed to open homes file: " .. err)
else
homepos = minetest.deserialize(input:read("*a"))
if type(homepos) ~= "table" then
homepos = {}
end
io.close(input)
end
minetest.register_privilege("home", "Can use /sethome and /home")
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
return false
end
if homepos[name] then
player:setpos(homepos[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("sethome", {
description = "Set your home point",
privs = {home=true},
func = function(name)
local player = minetest.get_player_by_name(name)
local pos = vector.round(player:getpos())
homepos[name] = pos
local homedata = minetest.serialize(homepos)
if not homedata then
minetest.log("error", "[sethome] Failed to serialize home data!")
minetest.chat_send_player(name, "Something failed while serializing the home data.")
return false
end
local output, err = io.open(homes_file, "w")
if err then
minetest.log("error", "[sethome] Failed to open homes file: " .. err)
minetest.chat_send_player(name, "Could not save your new home position.")
return false
end
output:write(homedata)
io.close(output)
minetest.chat_send_player(name, "Home set at " .. minetest.pos_to_string(pos))
end
})
minetest.register_chatcommand("migrate_homes", {
description = "Migrate to new homes file format",
privs = {rollback=true}
func = function(name)
local input, err = io.open(oldhomes_file, "r")
if err then
minetest.log("error", "[sethome] Failed to open old homes file: " .. err)
return false
else
repeat repeat
local x = input:read("*n") local x = input:read("*n")
if x == nil then if x == nil then
@ -12,54 +73,23 @@ local function loadhomes()
local y = input:read("*n") local y = input:read("*n")
local z = input:read("*n") local z = input:read("*n")
local name = input:read("*l") local name = input:read("*l")
homepos[name:sub(2)] = {x = x, y = y, z = z} homepos[name:sub(2)] = vector.new(x, y, z)
until input:read(0) == nil until input:read(0) == nil
io.close(input) io.close(input)
else local homedata = minetest.serialize(homepos)
homepos = {} if not homedata then
end minetest.log("error", "[sethome] Failed to serialize old home data!")
end minetest.chat_send_player(name, "Something failed while serializing the old home data.")
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.env:get_player_by_name(name)
if player == nil then
-- just a check to prevent the server crashing
return false return false
end end
if homepos[player:get_player_name()] then local output, err = io.open(homes_file, "w")
player:setpos(homepos[player:get_player_name()]) if err then
minetest.chat_send_player(name, "Teleported to home!") minetest.log("error", "[sethome] Failed to open homes file: " .. err)
else minetest.chat_send_player(name, "Could not save to new homes files.")
minetest.chat_send_player(name, "Set a home using /sethome") return false
end
end,
})
minetest.register_chatcommand("sethome", {
description = "Set your home point",
privs = {home=true},
func = function(name)
local player = minetest.env: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 end
output:write()
io.close(output) io.close(output)
changed = false
end end
end, end
}) })