1
0
mirror of https://github.com/sys4-fr/server-nalc.git synced 2024-11-16 23:40:31 +01:00

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
This commit is contained in:
LeMagnesium 2015-02-06 14:46:53 +01:00
parent 82b6bc809b
commit 218882457b
3 changed files with 72 additions and 65 deletions

View File

@ -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"} ["nether"] = minetest.get_worldpath() .. "/netherhomes"}
local homepos = {["real"] = {}, ["nether"] = {}} home.homepos = {["real"] = {}, ["nether"] = {}}
local timers = {} home.timers = {}
local HOME_INTERVAL = 30*60 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() local function loadhomes()
for key,_ in pairs(homes_file) do for key,_ in pairs(home.homes_file) do
local input = io.open(homes_file[key], "r") local input = io.open(home.homes_file[key], "r")
if input then if input then
repeat repeat
local x = input:read("*n") local x = input:read("*n")
@ -16,11 +69,11 @@ local function loadhomes()
local y = input:read("*n") local y = input:read("*n")
local z = input:read("*n") local z = input:read("*n")
local name = input:read("*l") 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 until input:read(0) == nil
io.close(input) io.close(input)
else else
homepos[key] = {} home.homepos[key] = {}
end end
end end
end end
@ -34,56 +87,11 @@ local changed = false
minetest.register_chatcommand("home", { minetest.register_chatcommand("home", {
description = "Teleport you to your home point", description = "Teleport you to your home point",
privs = {home=true}, privs = {home=true},
func = function(name) func = home.tohome,
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,
}) })
minetest.register_chatcommand("sethome", { minetest.register_chatcommand("sethome", {
description = "Set your home point", description = "Set your home point",
privs = {home=true}, privs = {home=true},
func = function(name) func = home.sethome,
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,
}) })

View File

@ -1,3 +1,4 @@
sethome
creative? creative?
intllib? intllib?
datastorage? datastorage?

View File

@ -6,6 +6,9 @@ else
S = function(s) return s end S = function(s) return s end
end end
local timers = {}
local HOME_INTERVAL = 30*60
minetest.register_privilege("creative", { minetest.register_privilege("creative", {
description = "Can use the creative inventory", description = "Can use the creative inventory",
give_to_singleplayer = false, give_to_singleplayer = false,
@ -44,15 +47,9 @@ unified_inventory.register_button("home_gui_set", {
image = "ui_sethome_icon.png", image = "ui_sethome_icon.png",
tooltip = S("Set home position"), tooltip = S("Set home position"),
action = function(player) action = function(player)
local player_name = player:get_player_name() minetest.sound_play("dingdong",
unified_inventory.set_home(player, player:getpos())
local home = unified_inventory.home_pos[player_name]
if home ~= nil then
minetest.sound_play("dingdong",
{to_player=player_name, gain = 1.0}) {to_player=player_name, gain = 1.0})
minetest.chat_send_player(player_name, home.sethome(player:get_player_name())
S("Home position set to: %s"):format(minetest.pos_to_string(home)))
end
end, end,
}) })
@ -61,9 +58,10 @@ unified_inventory.register_button("home_gui_go", {
image = "ui_gohome_icon.png", image = "ui_gohome_icon.png",
tooltip = S("Go home"), tooltip = S("Go home"),
action = function(player) 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}) {to_player=player:get_player_name(), gain = 1.0})
unified_inventory.go_home(player) end
end, end,
}) })