From 8dc6b0124e9de67d3035ebe1641afd7abd78ee62 Mon Sep 17 00:00:00 2001 From: Sfan5 Date: Wed, 16 Apr 2014 20:40:16 +0200 Subject: [PATCH] Add /sethome & /home --- mods/sethome/init.lua | 65 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 mods/sethome/init.lua diff --git a/mods/sethome/init.lua b/mods/sethome/init.lua new file mode 100644 index 00000000..20f77c01 --- /dev/null +++ b/mods/sethome/init.lua @@ -0,0 +1,65 @@ +local homes_file = minetest.get_worldpath() .. "/homes" +local homepos = {} + +local function loadhomes() + local input = io.open(homes_file, "r") + if input then + repeat + local x = input:read("*n") + if x == nil then + break + end + local y = input:read("*n") + local z = input:read("*n") + local name = input:read("*l") + homepos[name:sub(2)] = {x = x, y = y, z = z} + until input:read(0) == nil + io.close(input) + else + homepos = {} + end +end + +loadhomes() + +minetest.register_privilege("home", "Can use /sethome and /home") + +local changed = false + +minetest.register_chatcommand("home", { + description = "Teleport you to your home point", + privs = {home=true}, + func = function(name) + local player = minetest.env:get_player_by_name(name) + if player == nil then + -- 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, +}) + +minetest.register_chatcommand("sethome", { + description = "Set your home point", + privs = {home=true}, + func = function(name) + local player = minetest.env:get_player_by_name(name) + local pos = player:getpos() + homepos[player:get_player_name()] = pos + 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, +}) -- 2.25.1