1
0
mirror of https://github.com/sys4-fr/server-nalc.git synced 2025-02-04 14:20:16 +01:00

Added sethome handlers using minetest.[de]serialize minetest.serialize and minetest.deserialize provide a faster way to write and read input, it turnes useless the repeat .. until statement, and then, make the read faster.

This commit is contained in:
LeMagnesium 2015-02-14 15:20:00 +01:00
parent 6f2f57a913
commit 63a6c26de6

View File

@ -30,9 +30,10 @@ home.sethome = function(name)
home.homepos[p_status][player:get_player_name()] = pos home.homepos[p_status][player:get_player_name()] = pos
minetest.chat_send_player(name, "Home set!") minetest.chat_send_player(name, "Home set!")
local output = io.open(home.homes_file[p_status], "w") local output = io.open(home.homes_file[p_status], "w")
for i, v in pairs(home.homepos[p_status]) do --for i, v in pairs(home.homepos[p_status]) do
output:write(v.x.." "..v.y.." "..v.z.." "..i.."\n") -- output:write(v.x.." "..v.y.." "..v.z.." "..i.."\n")
end --end
output:write(minetest.serialize(home.homepos[p_status]))
io.close(output) io.close(output)
return true return true
end end
@ -74,6 +75,11 @@ local function loadhomes()
for key,_ in pairs(home.homes_file) do for key,_ in pairs(home.homes_file) do
local input = io.open(home.homes_file[key], "r") local input = io.open(home.homes_file[key], "r")
if input then if input then
-- Old format handler
local line = input:read()
input:seek("set",0)
if line == nil then return end
if not line:match("return {") then
repeat repeat
local x = input:read("*n") local x = input:read("*n")
if x == nil then if x == nil then
@ -84,6 +90,9 @@ local function loadhomes()
local name = input:read("*l") local name = input:read("*l")
home.homepos[key][name:sub(2)] = {x = x, y = y, z = z} home.homepos[key][name:sub(2)] = {x = x, y = y, z = z}
until input:read(0) == nil until input:read(0) == nil
else
home.homepos[key] = minetest.deserialize(input:read())
end
io.close(input) io.close(input)
else else
home.homepos[key] = {} home.homepos[key] = {}
@ -106,3 +115,5 @@ minetest.register_chatcommand("sethome", {
privs = {home=true}, privs = {home=true},
func = home.sethome, func = home.sethome,
}) })
minetest.log("action","[sethome] Loaded.")