forked from thunderdog1138/star_wars
Tidy sethome code, add global functions, round coords to 1 decimal
- Global functions sethome.set(name, pos) , sethome.get(name) and sethome.go(name) - Tidy: trim coords to one decimal place and write to table and output table in one go. - Add error checking - Add t4im's homepos loader
This commit is contained in:
parent
f4f9e58ef2
commit
80d49095f5
|
@ -1,65 +1,80 @@
|
||||||
|
|
||||||
|
sethome = {}
|
||||||
|
|
||||||
local homes_file = minetest.get_worldpath() .. "/homes"
|
local homes_file = minetest.get_worldpath() .. "/homes"
|
||||||
local homepos = {}
|
local homepos = {}
|
||||||
|
|
||||||
local function loadhomes()
|
local function loadhomes()
|
||||||
local input = io.open(homes_file, "r")
|
local input, err = io.open(homes_file, "r")
|
||||||
if input then
|
if not input then
|
||||||
repeat
|
return minetest.log("info", "Could not load player homes file: " .. err)
|
||||||
local x = input:read("*n")
|
|
||||||
if x == nil then
|
|
||||||
break
|
|
||||||
end
|
end
|
||||||
local y = input:read("*n")
|
|
||||||
local z = input:read("*n")
|
-- Iterate over all stored positions in the format "x y z player" for each line
|
||||||
local name = input:read("*l")
|
for pos, name in input:read("*a"):gmatch("(%S+ %S+ %S+)%s([%w_-]+)[\r\n]") do
|
||||||
homepos[name:sub(2)] = {x = x, y = y, z = z}
|
homepos[name] = minetest.string_to_pos(pos)
|
||||||
until input:read(0) == nil
|
|
||||||
io.close(input)
|
|
||||||
else
|
|
||||||
homepos = {}
|
|
||||||
end
|
end
|
||||||
|
input:close()
|
||||||
end
|
end
|
||||||
|
|
||||||
loadhomes()
|
loadhomes()
|
||||||
|
|
||||||
minetest.register_privilege("home", "Can use /sethome and /home")
|
sethome.set = function(name, pos)
|
||||||
|
local player = minetest.get_player_by_name(name)
|
||||||
|
if not player or not pos then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
local changed = false
|
local data = {}
|
||||||
|
local output, err = io.open(homes_file, "w")
|
||||||
|
if output then
|
||||||
|
homepos[name] = pos
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
sethome.get = function(name)
|
||||||
|
return homepos[name]
|
||||||
|
end
|
||||||
|
|
||||||
|
sethome.go = function(name)
|
||||||
|
local player = minetest.get_player_by_name(name)
|
||||||
|
if player and homepos[name] then
|
||||||
|
player:setpos(homepos[name])
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
minetest.register_privilege("home", "Can use /sethome and /home")
|
||||||
|
|
||||||
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.get_player_by_name(name)
|
if sethome.go(name) then
|
||||||
if player == nil then
|
return true, "Teleported to home!"
|
||||||
-- just a check to prevent the server crashing
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
if homepos[player:get_player_name()] then
|
|
||||||
player:setpos(homepos[player:get_player_name()])
|
|
||||||
minetest.chat_send_player(name, "Teleported to home!")
|
|
||||||
else
|
|
||||||
minetest.chat_send_player(name, "Set a home using /sethome")
|
|
||||||
end
|
end
|
||||||
|
return false, "Set a home using /sethome"
|
||||||
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)
|
||||||
|
name = name or "" -- fallback to blank name if nil
|
||||||
local player = minetest.get_player_by_name(name)
|
local player = minetest.get_player_by_name(name)
|
||||||
local pos = player:getpos()
|
if player and sethome.set(name, player:getpos()) then
|
||||||
homepos[player:get_player_name()] = pos
|
return true, "Home set!"
|
||||||
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
|
|
||||||
io.close(output)
|
|
||||||
changed = false
|
|
||||||
end
|
end
|
||||||
|
return false, "Player not found!"
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue