Fix broken player init in mcl_hunger causing crash

This commit is contained in:
Wuzzy 2019-03-09 08:25:00 +01:00
parent 6d6fc3f34c
commit b9377a0dbf
2 changed files with 21 additions and 13 deletions

View file

@ -1,19 +1,32 @@
mcl_hunger.registered_foods = {} mcl_hunger.registered_foods = {}
function mcl_hunger.init_player(player)
local meta = player:get_meta()
if meta:get_string("mcl_hunger:hunger") == "" then
meta:set_string("mcl_hunger:hunger", tostring(20))
end
if meta:get_string("mcl_hunger:saturation") == "" then
meta:set_string("mcl_hunger:saturation", tostring(mcl_hunger.SATURATION_INIT))
end
if meta:get_string("mcl_hunger:exhaustion") == "" then
meta:set_string("mcl_hunger:exhaustion", tostring(0))
end
end
if mcl_hunger.active then if mcl_hunger.active then
function mcl_hunger.get_hunger(player) function mcl_hunger.get_hunger(player)
local hunger = player:get_meta():get_string("mcl_hunger:hunger") or 20 local hunger = tonumber(player:get_meta():get_string("mcl_hunger:hunger")) or 20
return tonumber(hunger) return hunger
end end
function mcl_hunger.get_saturation(player) function mcl_hunger.get_saturation(player)
local saturation = player:get_meta():get_string("mcl_hunger:saturation") or mcl_hunger.SATURATION_INIT local saturation = tonumber(player:get_meta():get_string("mcl_hunger:saturation")) or mcl_hunger.SATURATION_INIT
return tonumber(saturation) return saturation
end end
function mcl_hunger.get_exhaustion(player) function mcl_hunger.get_exhaustion(player)
local exhaustion = player:get_meta():get_string("mcl_hunger:exhaustion") or 0 local exhaustion = tonumber(player:get_meta():get_string("mcl_hunger:exhaustion")) or 0
return tonumber(exhaustion) return exhaustion
end end
function mcl_hunger.set_hunger(player, hunger, update_hudbars) function mcl_hunger.set_hunger(player, hunger, update_hudbars)

View file

@ -96,15 +96,9 @@ if mcl_hunger.debug then
hb.register_hudbar("exhaustion", 0xFFFFFF, S("Exhaust."), { icon = "mcl_hunger_icon_exhaustion.png", bgicon = "mcl_hunger_bgicon_exhaustion.png", bar = "mcl_hunger_bar_exhaustion.png" }, 0, mcl_hunger.EXHAUST_LVL, false, S("%s: %d/%d")) hb.register_hudbar("exhaustion", 0xFFFFFF, S("Exhaust."), { icon = "mcl_hunger_icon_exhaustion.png", bgicon = "mcl_hunger_bgicon_exhaustion.png", bar = "mcl_hunger_bar_exhaustion.png" }, 0, mcl_hunger.EXHAUST_LVL, false, S("%s: %d/%d"))
end end
minetest.register_on_newplayer(function(player)
local name = player:get_player_name()
mcl_hunger.set_hunger(player, 20, false)
mcl_hunger.set_saturation(player, mcl_hunger.SATURATION_INIT, false)
mcl_hunger.set_exhaustion(player, 0, false)
end)
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
local name = player:get_player_name() local name = player:get_player_name()
mcl_hunger.init_player(player)
init_hud(player) init_hud(player)
mcl_hunger.poison_damage[name] = 0 mcl_hunger.poison_damage[name] = 0
mcl_hunger.poison_hunger[name] = 0 mcl_hunger.poison_hunger[name] = 0
@ -193,6 +187,7 @@ end)
else else
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
mcl_hunger.init_player(player)
mcl_hunger.last_eat[player:get_player_name()] = -1 mcl_hunger.last_eat[player:get_player_name()] = -1
end) end)