blob: 096e35e9edb3b2eb75cc8b554ce046f5e2034e56 (
plain)
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
|
datastorage={}
datastorage["!registered_players"]={}
datastorage.save_data = function(table_pointer)
local data = minetest.serialize( datastorage[table_pointer] )
local path = minetest.get_worldpath().."/datastorage_"..table_pointer..".data"
local file = io.open( path, "w" )
if( file ) then
file:write( data )
file:close()
return true
else return nil
end
end
datastorage.load_data = function(table_pointer)
local path = minetest.get_worldpath().."/datastorage_"..table_pointer..".data"
local file = io.open( path, "r" )
if( file ) then
local data = file:read("*all")
datastorage[table_pointer] = minetest.deserialize( data )
file:close()
return true
else return nil
end
end
datastorage.get_container = function (player, key)
local player_name = player:get_player_name()
local container = datastorage[player_name]
if container[key] == nil then
container[key] = {}
end
datastorage.save_data(player_name)
return container[key]
end
-- forced save of all player's data
datastorage.save_container = function (player)
local player_name = player:get_player_name()
datastorage.save_data(player_name)
end
-- Init
if datastorage.load_data("!registered_players") == nil then
datastorage["!registered_players"]={}
datastorage.save_data("!registered_players")
end
minetest.register_on_joinplayer(function(player)
local player_name = player:get_player_name()
local registered = nil
for __,tab in ipairs(datastorage["!registered_players"]) do
if tab["player_name"] == player_name then registered = true break end
end
if registered == nil then
local new={}
new["player_name"]=player_name
table.insert(datastorage["!registered_players"],new)
datastorage[player_name]={}
datastorage.save_data("!registered_players")
datastorage.save_data(player_name)
else
datastorage.load_data(player_name)
end
end
)
minetest.register_on_leaveplayer(function(player)
local player_name = player:get_player_name()
datastorage.save_data(player_name)
datastorage[player_name] = nil
end
)
minetest.register_on_shutdown(function()
for __,tab in ipairs(datastorage["!registered_players"]) do
if datastorage[tab["player_name"]] == nil then break end
datastorage.save_data(tab["player_name"])
end
end
)
|