-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.lua
94 lines (77 loc) · 3.37 KB
/
functions.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
-- back functions
local S = back.intllib
local position = {}
position.position_file = {["f"] = minetest.get_worldpath() .. "/back_positions"}
position.position_back = {}
-- ---------------------------------------------------------------------------------------------------------------------
-- Teleportation a la derniere position connue
-- ---------------------------------------------------------------------------------------------------------------------
back.back = function(name)
minetest.chat_send_player(name, S("Back to your previous position"))
local player = minetest.get_player_by_name(name)
if player == nil then
return false
end
-- Verif si position presente pour le joueur
if position.position_back[name] then
else
minetest.chat_send_player(name, S("No saved position..."))
return false
end
-- Sauvegarde de la derniere position connue avant modif du fichier "back_positions"
local p = position.position_back[player:get_player_name()]
-- Sauvegarde de la position courante avant retour
back.save_position(name)
-- Tp vers position "p"
player:setpos(p)
return true
end
-- ---------------------------------------------------------------------------------------------------------------------
-- Sauvegarde de la position courante
-- ---------------------------------------------------------------------------------------------------------------------
function back.save_position(name)
local player = minetest.get_player_by_name(name)
local pos = player:getpos()
minetest.log("action",S("[Back] Backup the current position ->").." "..name.." - "..minetest.serialize(pos))
if position.position_back[player:get_player_name()] ~= pos then
position.position_back[player:get_player_name()] = pos
local output = io.open(position.position_file["f"], "w")
output:write(minetest.serialize(position.position_back))
io.close(output)
back.reload_positions()
return true
else
return false
end
end
-- ---------------------------------------------------------------------------------------------------------------------
-- Reload du fichier "back_positions"
-- ---------------------------------------------------------------------------------------------------------------------
function back.reload_positions()
for key,_ in pairs(position.position_file) do
local input = io.open(position.position_file[key], "r")
if input then
local line = input:read()
input:seek("set",0)
if line == nil then return end
if not line:match("return {") 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")
position.position_back[name:sub(2)] = {x = x, y = y, z = z}
until input:read(0) == nil
else
position.position_back = minetest.deserialize(input:read())
end
io.close(input)
else
position.position_back = {}
end
end
end
-- ---------------------------------------------------------------------------------------------------------------------