summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubenwardy <anjayward@gmail.com>2014-04-13 19:02:12 +0100
committerrubenwardy <anjayward@gmail.com>2014-04-13 19:02:12 +0100
commit105cfefa71b1dfd0aecb52c64dea51fe5bcac84a (patch)
treed9072a6f180d964270155d9ccfd1076ac040f038
parentc21b722e6d6d69336138ac04fed05339a2b4f092 (diff)
Save player data
-rw-r--r--init.lua26
1 files changed, 26 insertions, 0 deletions
diff --git a/init.lua b/init.lua
index c4bcc7d..1383520 100644
--- a/init.lua
+++ b/init.lua
@@ -2,6 +2,27 @@ diet = {
players = {}
}
+function diet.__init()
+ local file = io.open(minetest.get_worldpath().."/diet.txt", "r")
+ if file then
+ local table = minetest.deserialize(file:read("*all"))
+ if type(table) == "table" then
+ diet.players = table.players
+ return
+ end
+ end
+end
+
+function diet.save()
+ local file = io.open(minetest.get_worldpath().."/diet.txt", "w")
+ if file then
+ file:write(minetest.serialize({
+ players = diet.players
+ }))
+ file:close()
+ end
+end
+
function diet.item_eat(max)
return function(itemstack, user, pointed_thing)
-- Process player data
@@ -67,6 +88,8 @@ function diet.item_eat(max)
-- Register
diet.__register_eat(player,item,ftype)
+ diet.save()
+
-- Remove item
itemstack:take_item()
return itemstack
@@ -85,6 +108,7 @@ function diet.__player(name)
name = name,
eaten = {}
}
+ diet.save()
return diet.players[name]
end
@@ -95,3 +119,5 @@ function diet.__register_eat(player,food,type)
table.remove(player.eaten,1)
end
end
+
+diet.__init()