109 lines
2.3 KiB
Lua
Raw Permalink Normal View History

2016-01-17 16:54:48 +01:00
places = {}
places.pos = {}
places.places_file = minetest.get_worldpath() .. "/places"
2016-02-10 12:46:16 +01:00
places.show_places = false
2016-09-06 11:50:03 +02:00
2016-01-17 16:54:48 +01:00
function places.register_place(name, pos)
places.pos[name] = pos
end
function places.load_places()
local input = io.open(places.places_file, "r")
if input then
local str = input:read()
if str then
2016-09-06 11:50:03 +02:00
print("[info] places string : " .. str)
2016-01-17 16:54:48 +01:00
if minetest.deserialize(str) then
places.pos = minetest.deserialize(str)
end
else
2016-09-06 11:50:03 +02:00
print("[warning] places file is empty")
2016-01-17 16:54:48 +01:00
end
io.close(input)
else
2016-09-06 11:50:03 +02:00
print("[ERROR] couldnt find places file")
2016-01-17 16:54:48 +01:00
end
end
function places.save_places()
if places.pos then
local output = io.open(places.places_file, "w")
local str = minetest.serialize(places.pos)
output:write(str)
io.close(output)
end
end
2016-09-06 11:50:03 +02:00
minetest.register_chatcommand("add_place", {
2016-01-17 16:54:48 +01:00
params = "<name>",
2016-09-06 11:50:03 +02:00
description = "Add a place",
2016-01-17 19:17:25 +01:00
privs = {server=true},
2016-01-17 16:54:48 +01:00
func = function(name, text)
if places.pos[text] then
return true, "There already is a place named " .. text
end
local player = minetest.get_player_by_name(name)
2016-09-06 11:50:03 +02:00
if not(player) then
return true, "Error couldnt find player " .. name
end
places.pos[text] = player:getpos()
places.save_places()
return true, "Added a place named " .. text
end,
})
2017-01-13 11:01:27 +01:00
minetest.register_chatcommand("goto", {
2016-09-06 11:50:03 +02:00
params = "<name>",
description = "Goto a place",
privs = {},
func = function(name, text)
if not(places.pos[text]) then
return true, "There is no place named " .. text
end
local player = minetest.get_player_by_name(name)
if not(player) then
return true, "Error couldnt find player " .. name
2016-01-17 16:54:48 +01:00
end
2016-09-06 11:50:03 +02:00
player:setpos(places.pos[text])
return true, "Teleported to " .. text
2016-01-17 16:54:48 +01:00
end,
})
2017-01-13 11:01:27 +01:00
minetest.register_chatcommand("list_places", {
params = "",
description = "Shows a list of all places",
privs = {server=true},
func = function(name, text)
if not(places.pos) then
return false, "Error"
end
local my_places = {}
for name, pos in pairs(places.pos) do
table.insert(my_places, name)
end
return true, table.concat(my_places, ", ")
end,
})
2016-01-17 16:54:48 +01:00
minetest.register_on_joinplayer(function(player)
2016-02-10 12:46:16 +01:00
if not places.show_places then
return
end
2016-01-17 16:54:48 +01:00
if places.pos then
for k, v in pairs(places.pos) do
player:hud_add({
hud_elem_type = "waypoint",
name = k,
text = "",
number = 255,
world_pos = v
})
end
end
end)
places.load_places()