2019-09-10 19:09:51 +02:00
|
|
|
-- sethome/init.lua
|
2016-07-03 18:40:43 +02:00
|
|
|
|
|
|
|
sethome = {}
|
|
|
|
|
2019-09-10 19:09:51 +02:00
|
|
|
-- Load support for MT game translation.
|
|
|
|
local S = minetest.get_translator("sethome")
|
|
|
|
|
|
|
|
|
2014-04-16 20:40:16 +02:00
|
|
|
local homes_file = minetest.get_worldpath() .. "/homes"
|
|
|
|
local homepos = {}
|
|
|
|
|
|
|
|
local function loadhomes()
|
2017-02-20 23:40:37 +01:00
|
|
|
local input = io.open(homes_file, "r")
|
2016-07-03 18:40:43 +02:00
|
|
|
if not input then
|
2017-02-20 23:40:37 +01:00
|
|
|
return -- no longer an error
|
2016-07-03 18:40:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Iterate over all stored positions in the format "x y z player" for each line
|
|
|
|
for pos, name in input:read("*a"):gmatch("(%S+ %S+ %S+)%s([%w_-]+)[\r\n]") do
|
|
|
|
homepos[name] = minetest.string_to_pos(pos)
|
|
|
|
end
|
|
|
|
input:close()
|
2014-04-16 20:40:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
loadhomes()
|
|
|
|
|
2016-07-03 18:40:43 +02:00
|
|
|
sethome.set = function(name, pos)
|
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
if not player or not pos then
|
|
|
|
return false
|
|
|
|
end
|
2020-07-26 11:07:15 +02:00
|
|
|
local player_meta = player:get_meta()
|
|
|
|
player_meta:set_string("sethome:home", minetest.pos_to_string(pos))
|
2016-07-03 18:40:43 +02:00
|
|
|
|
2017-02-20 23:40:37 +01:00
|
|
|
-- remove `name` from the old storage file
|
2020-06-10 14:50:49 +02:00
|
|
|
if not homepos[name] then
|
|
|
|
return true
|
|
|
|
end
|
2016-07-03 18:40:43 +02:00
|
|
|
local data = {}
|
2017-02-20 23:40:37 +01:00
|
|
|
local output = io.open(homes_file, "w")
|
2016-07-03 18:40:43 +02:00
|
|
|
if output then
|
2017-02-20 23:40:37 +01:00
|
|
|
homepos[name] = nil
|
2016-07-03 18:40:43 +02:00
|
|
|
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
|
2017-02-20 23:40:37 +01:00
|
|
|
return true -- if the file doesn't exist - don't return an error.
|
2016-07-03 18:40:43 +02:00
|
|
|
end
|
2014-04-16 20:40:16 +02:00
|
|
|
|
2016-07-03 18:40:43 +02:00
|
|
|
sethome.get = function(name)
|
2017-02-20 23:40:37 +01:00
|
|
|
local player = minetest.get_player_by_name(name)
|
2020-07-26 11:07:15 +02:00
|
|
|
local player_meta = player:get_meta()
|
|
|
|
local pos = minetest.string_to_pos(player_meta:get_string("sethome:home"))
|
2017-02-20 23:40:37 +01:00
|
|
|
if pos then
|
|
|
|
return pos
|
|
|
|
end
|
|
|
|
|
|
|
|
-- fetch old entry from storage table
|
|
|
|
pos = homepos[name]
|
2016-11-05 03:17:05 +01:00
|
|
|
if pos then
|
2016-11-20 04:24:37 +01:00
|
|
|
return vector.new(pos)
|
2016-11-05 03:17:05 +01:00
|
|
|
else
|
2016-11-20 04:24:37 +01:00
|
|
|
return nil
|
2016-11-05 03:17:05 +01:00
|
|
|
end
|
2016-07-03 18:40:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
sethome.go = function(name)
|
2017-02-20 23:40:37 +01:00
|
|
|
local pos = sethome.get(name)
|
2016-07-03 18:40:43 +02:00
|
|
|
local player = minetest.get_player_by_name(name)
|
2017-02-20 23:40:37 +01:00
|
|
|
if player and pos then
|
2017-12-16 06:10:35 +01:00
|
|
|
player:set_pos(pos)
|
2016-07-03 18:40:43 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2016-11-06 00:49:25 +01:00
|
|
|
minetest.register_privilege("home", {
|
2019-09-10 19:09:51 +02:00
|
|
|
description = S("Can use /sethome and /home"),
|
2016-11-06 00:49:25 +01:00
|
|
|
give_to_singleplayer = false
|
|
|
|
})
|
2014-04-16 20:40:16 +02:00
|
|
|
|
|
|
|
minetest.register_chatcommand("home", {
|
2019-09-10 19:09:51 +02:00
|
|
|
description = S("Teleport you to your home point"),
|
2016-07-03 18:40:43 +02:00
|
|
|
privs = {home = true},
|
|
|
|
func = function(name)
|
|
|
|
if sethome.go(name) then
|
2019-09-10 19:09:51 +02:00
|
|
|
return true, S("Teleported to home!")
|
2016-07-03 18:40:43 +02:00
|
|
|
end
|
2019-09-10 19:09:51 +02:00
|
|
|
return false, S("Set a home using /sethome")
|
2016-07-03 18:40:43 +02:00
|
|
|
end,
|
2014-04-16 20:40:16 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_chatcommand("sethome", {
|
2019-09-10 19:09:51 +02:00
|
|
|
description = S("Set your home point"),
|
2016-07-03 18:40:43 +02:00
|
|
|
privs = {home = true},
|
|
|
|
func = function(name)
|
|
|
|
name = name or "" -- fallback to blank name if nil
|
|
|
|
local player = minetest.get_player_by_name(name)
|
2018-07-01 21:44:03 +02:00
|
|
|
if player and sethome.set(name, player:get_pos()) then
|
2019-09-10 19:09:51 +02:00
|
|
|
return true, S("Home set!")
|
2016-07-03 18:40:43 +02:00
|
|
|
end
|
2019-09-10 19:09:51 +02:00
|
|
|
return false, S("Player not found!")
|
2016-07-03 18:40:43 +02:00
|
|
|
end,
|
2014-04-16 20:40:16 +02:00
|
|
|
})
|