mirror of
https://github.com/luanti-org/minetest_game.git
synced 2025-05-21 06:43:17 -04:00
Rewrite sethome mod
This commit is contained in:
parent
5e0c49345a
commit
a717827dc8
1 changed files with 85 additions and 55 deletions
|
@ -1,65 +1,95 @@
|
||||||
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)
|
||||||
repeat
|
else
|
||||||
local x = input:read("*n")
|
homepos = minetest.deserialize(input:read("*a"))
|
||||||
if x == nil then
|
if type(homepos) ~= "table" then
|
||||||
break
|
homepos = {}
|
||||||
end
|
end
|
||||||
local y = input:read("*n")
|
io.close(input)
|
||||||
local z = input:read("*n")
|
|
||||||
local name = input:read("*l")
|
|
||||||
homepos[name:sub(2)] = {x = x, y = y, z = z}
|
|
||||||
until input:read(0) == nil
|
|
||||||
io.close(input)
|
|
||||||
else
|
|
||||||
homepos = {}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
loadhomes()
|
|
||||||
|
|
||||||
minetest.register_privilege("home", "Can use /sethome and /home")
|
minetest.register_privilege("home", "Can use /sethome and /home")
|
||||||
|
|
||||||
local changed = false
|
|
||||||
|
|
||||||
minetest.register_chatcommand("home", {
|
minetest.register_chatcommand("home", {
|
||||||
description = "Teleport you to your home point",
|
description = "Teleport you to your home point",
|
||||||
privs = {home=true},
|
privs = {home=true},
|
||||||
func = function(name)
|
func = function(name)
|
||||||
local player = minetest.env:get_player_by_name(name)
|
local player = minetest.get_player_by_name(name)
|
||||||
if player == nil then
|
if player == nil then
|
||||||
-- just a check to prevent the server crashing
|
return false
|
||||||
return false
|
end
|
||||||
end
|
if homepos[name] then
|
||||||
if homepos[player:get_player_name()] then
|
player:setpos(homepos[name])
|
||||||
player:setpos(homepos[player:get_player_name()])
|
minetest.chat_send_player(name, "Teleported to home!")
|
||||||
minetest.chat_send_player(name, "Teleported to home!")
|
else
|
||||||
else
|
minetest.chat_send_player(name, "Set a home using /sethome")
|
||||||
minetest.chat_send_player(name, "Set a home using /sethome")
|
end
|
||||||
end
|
end
|
||||||
end,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
minetest.register_chatcommand("sethome", {
|
minetest.register_chatcommand("sethome", {
|
||||||
description = "Set your home point",
|
description = "Set your home point",
|
||||||
privs = {home=true},
|
privs = {home=true},
|
||||||
func = function(name)
|
func = function(name)
|
||||||
local player = minetest.env:get_player_by_name(name)
|
local player = minetest.get_player_by_name(name)
|
||||||
local pos = player:getpos()
|
local pos = vector.round(player:getpos())
|
||||||
homepos[player:get_player_name()] = pos
|
homepos[name] = pos
|
||||||
minetest.chat_send_player(name, "Home set!")
|
local homedata = minetest.serialize(homepos)
|
||||||
changed = true
|
if not homedata then
|
||||||
if changed then
|
minetest.log("error", "[sethome] Failed to serialize home data!")
|
||||||
local output = io.open(homes_file, "w")
|
minetest.chat_send_player(name, "Something failed while serializing the home data.")
|
||||||
for i, v in pairs(homepos) do
|
return false
|
||||||
output:write(v.x.." "..v.y.." "..v.z.." "..i.."\n")
|
end
|
||||||
end
|
local output, err = io.open(homes_file, "w")
|
||||||
io.close(output)
|
if err then
|
||||||
changed = false
|
minetest.log("error", "[sethome] Failed to open homes file: " .. err)
|
||||||
end
|
minetest.chat_send_player(name, "Could not save your new home position.")
|
||||||
end,
|
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
|
||||||
|
local x = input:read("*n")
|
||||||
|
if x == nil then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
local y = input:read("*n")
|
||||||
|
local z = input:read("*n")
|
||||||
|
local name = input:read("*l")
|
||||||
|
homepos[name:sub(2)] = vector.new(x, y, z)
|
||||||
|
until input:read(0) == nil
|
||||||
|
io.close(input)
|
||||||
|
local homedata = minetest.serialize(homepos)
|
||||||
|
if not homedata then
|
||||||
|
minetest.log("error", "[sethome] Failed to serialize old home data!")
|
||||||
|
minetest.chat_send_player(name, "Something failed while serializing the old 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 to new homes files.")
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
output:write()
|
||||||
|
io.close(output)
|
||||||
|
end
|
||||||
|
end
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue