-- =================================================================== ocsethome = {} local disable_extra_homes = minetest.setting_getbool ("disable_extra_homes") -- =================================================================== local max_homes = ocutil.numsetzero ("maxhomes") if max_homes == 0 then max_homes = ocutil.numsetzero ("max_homes") end if max_homes < 1 or max_homes > 8 then max_homes = 8 end ocutil.log ("max_homes is " .. max_homes) -- =================================================================== local privdesc if disable_extra_homes then privdesc = "For /sethome and /home" else privdesc = "For /sethome /sethome2 /home /home2 etc." end minetest.register_privilege ("home", { description = privdesc , give_to_singleplayer = false , }) -- =================================================================== ocsethome.sethome = function (name, d) local ds = ocsethome.disable_set if ds ~= nil and ds then return end local player = minetest.env:get_player_by_name (name) local pos = player:getpos() d = "" .. d if d == "" then d = "1" end player:get_meta():set_string("home" .. d, minetest.pos_to_string(pos)) -- Note: "ocsethome" calls "unified_inventory". However, "unified_ -- inventory" shouldn't be listed in the "ocsethome" "depends.txt" -- file (not even as an optional dependency. Such an entry would -- break things. if d == "1" and _G ["unified_inventory"] ~= nil and unified_inventory ~= nil and unified_inventory.set_home ~= nil then ocsethome.disable_set = true unified_inventory.set_home (player, pos) ocsethome.disable_set = false end local cmd = "/home" .. d if d == "1" then cmd = "/home or /home1" end minetest.chat_send_player (name, "Home #" .. d .." is set. Use " .. cmd .. " to go there.") end -- =================================================================== local register_cmd_sethome = function (txt_digit) if txt_digit == nil then txt_digit = "" end local num_digit = txt_digit if num_digit == "" then num_digit = "1" end local param_sethome = { params = "" , description = "Set home location #" .. num_digit , privs = { home=true } , func = function (name, param) local use_digit = num_digit if tonumber (use_digit) > max_homes then minetest.chat_send_player (name, "Error: You can only have homes numbered 1 to " .. max_homes) return end if param ~= nil and param ~= "" then local player = minetest.env:get_player_by_name (name) param = tonumber (param) if param == nil then minetest.chat_send_player (name, "Usage: /sethome# or /sethome #") return end if param < 1 or param > max_homes then minetest.chat_send_player (name, "Error: You can only have homes numbered 1 to " .. max_homes) return end if txt_digit ~= "" then minetest.chat_send_player (name, "Error: /sethome# # doesn't make sense") return end use_digit = param end ocsethome.sethome (name, use_digit) end } minetest.register_chatcommand ("sethome" .. txt_digit, param_sethome) end -- =================================================================== register_cmd_sethome ("" ) register_cmd_sethome ("1") if not disable_extra_homes then register_cmd_sethome ("2") register_cmd_sethome ("3") register_cmd_sethome ("4") register_cmd_sethome ("5") register_cmd_sethome ("6") register_cmd_sethome ("7") register_cmd_sethome ("8") end -- =================================================================== local savedir = minetest.get_worldpath() .. "/sethome/" -- ^ deprecated, but required for backward-compatible load_deprecated_home local load_deprecated_home = function (name, d) local player = minetest.env:get_player_by_name (name) d = "" .. d if d == "" then d = "1" end if tonumber (d) > max_homes then return nil end local fname = savedir .. name .. "_home" .. d local file = io.open (fname, "r") if not file then return nil end local line = file:read ("*line") file:close() local pos = minetest.string_to_pos (string.sub (line, 1, string.find (line, ")"))) if not pos or type (pos) ~= "table" then return nil end return pos end -- =================================================================== ocsethome.home = function (name, d) local player = minetest.env:get_player_by_name (name) d = "" .. d if d == "" then d = "1" end if tonumber (d) > max_homes then minetest.chat_send_player (name, "Error: You can only have homes numbered 1 to " .. max_homes) return end local home_id = "home" .. d local tmp_str = player:get_meta():get_string(home_id) local pos = nil if tmp_str == nil or tmp_str == "" then local file_pos = load_deprecated_home(name, d) if file_pos == nil then local cmd = "/home" .. d if d == "1" then cmd = "/sethome or /sethome1" end minetest.chat_send_player (name, "You haven't set that location. Use " .. cmd .. " to do so.") return else pos = file_pos -- Now it is to convert it to player metadata since -- load_deprecated_home always returns a valid pos -- (or nil, caught in case above): player:get_meta():set_string("home" .. d, minetest.pos_to_string(pos)) end else pos = minetest.string_to_pos(tmp_str) end if not pos or type (pos) ~= "table" then minetest.chat_send_player (name, "Error in " .. home_id .. " data. Contact the server owner.") return end player:setpos (pos) minetest.chat_send_player (name, "Home sweet home #" .. d) end -- =================================================================== local register_cmd_home = function (txt_digit) if txt_digit == nil then txt_digit = "" end local num_digit = txt_digit if num_digit == "" then num_digit = "1" end local param_home = { params = "" , description = "Teleport to home #" .. num_digit , privs = { home=true } , func = function (name, param) local use_digit = num_digit if param ~= nil and param ~= "" then local player = minetest.env:get_player_by_name (name) param = tonumber (param) if param == nil then minetest.chat_send_player (name, "Usage: /home# or /home #") return end if param < 1 or param > max_homes then minetest.chat_send_player (name, "Error: You can only have homes numbered 1 to " .. max_homes) return end if txt_digit ~= "" then minetest.chat_send_player (name, "Error: /home# # doesn't make sense") return end use_digit = param end ocsethome.home (name, use_digit) end } minetest.register_chatcommand ("home" .. txt_digit, param_home) end -- =================================================================== register_cmd_home ("" ) register_cmd_home ("1") if not disable_extra_homes then register_cmd_home ("2") register_cmd_home ("3") register_cmd_home ("4") register_cmd_home ("5") register_cmd_home ("6") register_cmd_home ("7") register_cmd_home ("8") end -- =================================================================== -- Wrap it up. minetest.log ("action", "mod ocsethome loaded") -- =================================================================== -- End of module.