From b9377a0dbfe885cdedf8dd287718893a57c12b8a Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sat, 9 Mar 2019 08:25:00 +0100 Subject: [PATCH] Fix broken player init in mcl_hunger causing crash --- mods/PLAYER/mcl_hunger/api.lua | 25 +++++++++++++++++++------ mods/PLAYER/mcl_hunger/init.lua | 9 ++------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/mods/PLAYER/mcl_hunger/api.lua b/mods/PLAYER/mcl_hunger/api.lua index ad609e055c..46dc76fa2a 100644 --- a/mods/PLAYER/mcl_hunger/api.lua +++ b/mods/PLAYER/mcl_hunger/api.lua @@ -1,19 +1,32 @@ 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 function mcl_hunger.get_hunger(player) - local hunger = player:get_meta():get_string("mcl_hunger:hunger") or 20 - return tonumber(hunger) + local hunger = tonumber(player:get_meta():get_string("mcl_hunger:hunger")) or 20 + return hunger end function mcl_hunger.get_saturation(player) - local saturation = player:get_meta():get_string("mcl_hunger:saturation") or mcl_hunger.SATURATION_INIT - return tonumber(saturation) + local saturation = tonumber(player:get_meta():get_string("mcl_hunger:saturation")) or mcl_hunger.SATURATION_INIT + return saturation end function mcl_hunger.get_exhaustion(player) - local exhaustion = player:get_meta():get_string("mcl_hunger:exhaustion") or 0 - return tonumber(exhaustion) + local exhaustion = tonumber(player:get_meta():get_string("mcl_hunger:exhaustion")) or 0 + return exhaustion end function mcl_hunger.set_hunger(player, hunger, update_hudbars) diff --git a/mods/PLAYER/mcl_hunger/init.lua b/mods/PLAYER/mcl_hunger/init.lua index 0e64a19067..a90bdb73ec 100644 --- a/mods/PLAYER/mcl_hunger/init.lua +++ b/mods/PLAYER/mcl_hunger/init.lua @@ -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")) 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) local name = player:get_player_name() + mcl_hunger.init_player(player) init_hud(player) mcl_hunger.poison_damage[name] = 0 mcl_hunger.poison_hunger[name] = 0 @@ -193,6 +187,7 @@ end) else minetest.register_on_joinplayer(function(player) + mcl_hunger.init_player(player) mcl_hunger.last_eat[player:get_player_name()] = -1 end)