icyessentials/sethome/init.lua

188 lines
4.3 KiB
Lua

-- sethome override for Icy Essentials with multi-home support
local maxhomes = tonumber(minetest.settings:get("sethome_count")) or 3
local bedhome = minetest.settings:get_bool("sethome_bed", true)
local homerespawn = minetest.settings:get_bool("sethome_respawn", false)
sethome = ess.register_module("home", "Homes")
sethome.set = function(name, pos, target)
local player = minetest.get_player_by_name(name)
if not player or not pos then
return false, "Player not found!"
end
if not target or target == "" then target = "home" end
local homes = ess.get_player_meta(name, "homes")
if not homes then homes = {} end
local count = 0
for i in pairs(homes) do
if i ~= "bed" then
count = count + 1
end
end
if count >= maxhomes and maxhomes ~= 0 and not ess.priv_match(name, "home.multiple") and target ~= "bed" then
return false, string.format("You don't have permission to set more than %d homes.", maxhomes)
end
homes[target:lower()] = pos
ess.set_player_meta(name, "homes", homes)
return true
end
sethome.get = function(name, target)
local player = minetest.get_player_by_name(name)
if not target or target == "" then target = "home" end
local homes = ess.get_player_meta(name, "homes")
if not homes then return false end
local targ = homes[target:lower()]
if not targ and target == "home" and homes["bed"] then
targ = homes["bed"]
target = "bed"
end
if not targ then
return nil
end
return targ, target
end
sethome.go = function(name,point,skipsave)
local pos, target = sethome.get(name,point)
local player = minetest.get_player_by_name(name)
if player and pos then
if not skipsave then
ess.save_player_pos(player)
end
player:set_pos(pos)
return true, target
end
return false
end
local function cmd_home(name, params, splitparams)
local target = splitparams[1]
if not target or target == "" then
target = "home"
end
local went, name = sethome.go(name, target)
if went then
return true, string.format("Teleported to %s!", name)
end
return false, string.format("Home \"%s\" does not exist.", target)
end
local function cmd_set(name, params, splitparams)
local target = splitparams[1]
if not target or target == "" then
target = "home"
end
local player = minetest.get_player_by_name(name)
local success, err = sethome.set(name, player:get_pos(), target)
if player and success then
return true, string.format("Home \"%s\" set!", target)
end
return false, err
end
local function cmd_del(name, params, splitparams)
local target = splitparams[1]
if not target or target == "" then target = "home" end
local homes = ess.get_player_meta(name, "homes")
if not homes then homes = {} end
homes[target:lower()] = nil
ess.set_player_meta(name, "homes", homes)
return true, string.format("Home \"%s\" deleted successfully.", target)
end
local function cmd_list(name, params, splitparams)
local target = splitparams[1]
if not target or target == "" then target = "home" end
local homes = ess.get_player_meta(name, "homes")
if not homes then homes = {} end
local names = {}
for i in pairs(homes) do
table.insert(names, i)
end
return true, "Homes: " .. table.concat( names, ", " )
end
sethome.register_chatcommand("home", {
description = "Teleport you to your home point",
params = "[<name>]",
privs = {
home = true,
["home.all"] = true,
},
func = cmd_home,
})
sethome.register_chatcommand("homes", {
description = "List your home points",
privs = {
home = true,
["home.all"] = true,
},
func = cmd_list,
})
sethome.register_chatcommand("sethome", {
description = "Set your home point",
params = "[<name>]",
privs = {
home = true,
["home.all"] = true,
["home.multiple"] = true,
},
func = cmd_set,
})
sethome.register_chatcommand("delhome", {
description = "Delete your home point",
params = "[<name>]",
privs = {
home = true,
["home.all"] = true,
},
func = cmd_del,
})
if bedhome and minetest.get_modpath("beds") ~= nil and beds ~= nil then
local rclick = beds.on_rightclick
beds.on_rightclick = function (pos,player)
local name = player:get_player_name()
rclick(pos,player)
local success = sethome.set(name, pos, "bed")
if success then
minetest.chat_send_player(name, "Bed spawnpoint set!")
end
end
end
if homerespawn then
minetest.register_on_respawnplayer(function(player)
return sethome.go(player:get_player_name(), nil, true)
end)
end