From 218882457bdfb546f1abd8fd73b97afa13676aae Mon Sep 17 00:00:00 2001 From: LeMagnesium Date: Fri, 6 Feb 2015 14:46:53 +0100 Subject: [PATCH] Merged GUI/console homes' handlers - Added timer to unified_inventory's GUI go_home by 'merging' it - Merged GUI/console sethome by addind all sethome's functions and variables in home[], unified_inventory calls home.sethome and home.tohome - Added sethome in unified_inventory's mandatory dependencies --- minetestforfun_game/mods/sethome/init.lua | 118 ++++++++++++---------- mods/unified_inventory/depends.txt | 1 + mods/unified_inventory/register.lua | 18 ++-- 3 files changed, 72 insertions(+), 65 deletions(-) diff --git a/minetestforfun_game/mods/sethome/init.lua b/minetestforfun_game/mods/sethome/init.lua index 11f97542..ac6f83b8 100644 --- a/minetestforfun_game/mods/sethome/init.lua +++ b/minetestforfun_game/mods/sethome/init.lua @@ -1,12 +1,65 @@ -local homes_file = {["real"] = minetest.get_worldpath() .. "/realhomes", +home = {} + +home.homes_file = {["real"] = minetest.get_worldpath() .. "/realhomes", ["nether"] = minetest.get_worldpath() .. "/netherhomes"} -local homepos = {["real"] = {}, ["nether"] = {}} -local timers = {} -local HOME_INTERVAL = 30*60 +home.homepos = {["real"] = {}, ["nether"] = {}} +home.timers = {} +home.HOME_INTERVAL = 30*60 + +home.sethome = function(name) + local player = minetest.env:get_player_by_name(name) + local pos = player:getpos() + local p_status = "real" + if pos.y < -19600 then + p_status = "nether" + end + home.homepos[p_status][player:get_player_name()] = pos + minetest.chat_send_player(name, "Home set!") + changed = true + if changed then + local output = io.open(home.homes_file[p_status], "w") + for i, v in pairs(home.homepos[p_status]) do + output:write(v.x.." "..v.y.." "..v.z.." "..i.."\n") + end + io.close(output) + changed = false + end +end + +home.tohome = 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 + local p_status = "real" + if player:getpos().y < -19600 then + p_status = "nether" + end + if home.homepos[p_status][name] then + if home.timers[name] ~= nil then + local timer_player = os.difftime(os.time(),home.timers[name]) + if timer_player < home.HOME_INTERVAL then -- less than x minutes + minetest.chat_send_player(name, "Please retry later, you used home last time less than ".. home.HOME_INTERVAL .." seconds ago.") + minetest.chat_send_player(name, "Retry in: ".. home.HOME_INTERVAL-timer_player .." seconds.") + minetest.log("action","Player ".. name .." tried to teleport home within forbidden interval.") + return false + end + end + player:setpos(home.homepos[p_status][player:get_player_name()]) + minetest.chat_send_player(name, "Teleported to home!") + minetest.log("action","Player ".. name .." teleported to home. Last teleportation allowed in ".. home.HOME_INTERVAL .." seconds.") + home.timers[name] = os.time() + return true + else + minetest.chat_send_player(name, "Set a home using /sethome") + return false + end +end local function loadhomes() - for key,_ in pairs(homes_file) do - local input = io.open(homes_file[key], "r") + for key,_ in pairs(home.homes_file) do + local input = io.open(home.homes_file[key], "r") if input then repeat local x = input:read("*n") @@ -16,11 +69,11 @@ local function loadhomes() local y = input:read("*n") local z = input:read("*n") local name = input:read("*l") - homepos[key][name:sub(2)] = {x = x, y = y, z = z} + home.homepos[key][name:sub(2)] = {x = x, y = y, z = z} until input:read(0) == nil io.close(input) else - homepos[key] = {} + home.homepos[key] = {} end end end @@ -34,56 +87,11 @@ 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 - local p_status = "real" - if player:getpos().y < -19600 then - p_status = "nether" - end - if homepos[p_status][name] then - if timers[name] ~= nil then - local timer_player = os.difftime(os.time(),timers[name]) - if timer_player < HOME_INTERVAL then -- less than x minutes - minetest.chat_send_player(name, "Please retry later, you used home last time less than ".. HOME_INTERVAL .." seconds ago.") - minetest.chat_send_player(name, "Retry in: ".. HOME_INTERVAL-timer_player .." seconds.") - minetest.log("action","Player ".. name .." tried to teleport home within forbidden interval.") - return - end - end - player:setpos(homepos[p_status][player:get_player_name()]) - minetest.chat_send_player(name, "Teleported to home!") - minetest.log("action","Player ".. name .." teleported to home. Last teleportation allowed in ".. HOME_INTERVAL .." seconds.") - timers[name] = os.time() - else - minetest.chat_send_player(name, "Set a home using /sethome") - end - end, + func = home.tohome, }) 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() - local p_status = "real" - if pos.y < -19600 then - p_status = "nether" - end - homepos[p_status][player:get_player_name()] = pos - minetest.chat_send_player(name, "Home set!") - changed = true - if changed then - local output = io.open(homes_file[p_status], "w") - for i, v in pairs(homepos[p_status]) do - output:write(v.x.." "..v.y.." "..v.z.." "..i.."\n") - end - io.close(output) - changed = false - end - end, + func = home.sethome, }) diff --git a/mods/unified_inventory/depends.txt b/mods/unified_inventory/depends.txt index a1ea5568..2b728903 100755 --- a/mods/unified_inventory/depends.txt +++ b/mods/unified_inventory/depends.txt @@ -1,3 +1,4 @@ +sethome creative? intllib? datastorage? diff --git a/mods/unified_inventory/register.lua b/mods/unified_inventory/register.lua index 1b7e0677..47969ece 100755 --- a/mods/unified_inventory/register.lua +++ b/mods/unified_inventory/register.lua @@ -6,6 +6,9 @@ else S = function(s) return s end end +local timers = {} +local HOME_INTERVAL = 30*60 + minetest.register_privilege("creative", { description = "Can use the creative inventory", give_to_singleplayer = false, @@ -44,15 +47,9 @@ unified_inventory.register_button("home_gui_set", { image = "ui_sethome_icon.png", tooltip = S("Set home position"), action = function(player) - local player_name = player:get_player_name() - unified_inventory.set_home(player, player:getpos()) - local home = unified_inventory.home_pos[player_name] - if home ~= nil then - minetest.sound_play("dingdong", + minetest.sound_play("dingdong", {to_player=player_name, gain = 1.0}) - minetest.chat_send_player(player_name, - S("Home position set to: %s"):format(minetest.pos_to_string(home))) - end + home.sethome(player:get_player_name()) end, }) @@ -61,9 +58,10 @@ unified_inventory.register_button("home_gui_go", { image = "ui_gohome_icon.png", tooltip = S("Go home"), action = function(player) - minetest.sound_play("teleport", + if home.tohome(player:get_player_name()) == true then + minetest.sound_play("teleport", {to_player=player:get_player_name(), gain = 1.0}) - unified_inventory.go_home(player) + end end, })