From 26e13119cb84eb224d4ad50807e1bb286f6784ea Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sat, 20 May 2017 20:59:30 +0200 Subject: [PATCH] Get rid of those weird hunger_raw functions --- mods/PLAYER/mcl_hunger/hunger.lua | 13 +----- mods/PLAYER/mcl_hunger/init.lua | 77 ++++++++----------------------- 2 files changed, 21 insertions(+), 69 deletions(-) diff --git a/mods/PLAYER/mcl_hunger/hunger.lua b/mods/PLAYER/mcl_hunger/hunger.lua index e5aa7e206..29b853243 100644 --- a/mods/PLAYER/mcl_hunger/hunger.lua +++ b/mods/PLAYER/mcl_hunger/hunger.lua @@ -1,11 +1,3 @@ --- Keep these for backwards compatibility -function mcl_hunger.save_hunger(player) - mcl_hunger.set_hunger_raw(player) -end -function mcl_hunger.load_hunger(player) - mcl_hunger.get_hunger_raw(player) -end - -- wrapper for minetest.item_eat (this way we make sure other mods can't break this one) local org_eat = core.do_item_eat core.do_item_eat = function(hp_change, replace_with_item, itemstack, user, pointed_thing) @@ -85,7 +77,7 @@ function mcl_hunger.item_eat(hunger_change, replace_with_item, poisen, heal, sou local itemname = itemstack:get_name() if itemstack:take_item() ~= nil and user ~= nil then local name = user:get_player_name() - local h = tonumber(mcl_hunger.hunger[name]) + local h = tonumber(mcl_hunger.get_hunger(user)) local hp = user:get_hp() local pos = user:getpos() @@ -152,8 +144,7 @@ function mcl_hunger.item_eat(hunger_change, replace_with_item, poisen, heal, sou if h < 20 and hunger_change then h = h + hunger_change if h > 20 then h = 20 end - mcl_hunger.hunger[name] = h - mcl_hunger.set_hunger_raw(user) + mcl_hunger.set_hunger(user, h) end hb.change_hudbar(user, "saturation", mcl_hunger.saturation[name], mcl_hunger.get_hunger(user)) diff --git a/mods/PLAYER/mcl_hunger/init.lua b/mods/PLAYER/mcl_hunger/init.lua index c41c774b4..675828b12 100644 --- a/mods/PLAYER/mcl_hunger/init.lua +++ b/mods/PLAYER/mcl_hunger/init.lua @@ -10,10 +10,6 @@ if minetest.setting_getbool("enable_damage") then mcl_hunger = {} mcl_hunger.food = {} --- HUD statbar values -mcl_hunger.hunger = {} -mcl_hunger.hunger_out = {} - -- Count number of poisonings a player has at once mcl_hunger.poisonings = {} @@ -46,8 +42,8 @@ if set then end local function custom_hud(player) - hb.init_hudbar(player, "food", mcl_hunger.get_hunger_raw(player)) - hb.init_hudbar(player, "saturation", mcl_hunger.saturation[player:get_player_name()], mcl_hunger.get_hunger_raw(player)) + hb.init_hudbar(player, "food", mcl_hunger.get_hunger(player)) + hb.init_hudbar(player, "saturation", mcl_hunger.saturation[player:get_player_name()], mcl_hunger.get_hunger(player)) hb.init_hudbar(player, "exhaustion", mcl_hunger.exhaustion[player:get_player_name()]) end @@ -58,37 +54,8 @@ hb.register_hudbar("food", 0xFFFFFF, S("Food"), { icon = "hbhunger_icon.png", bg hb.register_hudbar("saturation", 0xFFFFFF, S("Saturation"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 5, 20, false, S("%s: %.1f/%d")) hb.register_hudbar("exhaustion", 0xFFFFFF, S("Exhaustion"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 0, 4, false, S("%s: %.3f/%d")) --- update hud elemtens if value has changed -local function update_hud(player) - local name = player:get_player_name() - --hunger - local h_out = tonumber(mcl_hunger.hunger_out[name]) - local h = tonumber(mcl_hunger.hunger[name]) - if h_out ~= h then - mcl_hunger.hunger_out[name] = h - hb.change_hudbar(player, "food", h) - hb.change_hudbar(player, "saturation", nil, h) - end -end - -- API START -- mcl_hunger.get_hunger = function(player) - local name = player:get_player_name() - return mcl_hunger.hunger[name] -end - -mcl_hunger.set_hunger = function(player, hunger) - local name = player:get_player_name() - mcl_hunger.hunger[name] = hunger - mcl_hunger.set_hunger_raw(player) - update_hud(player) -end - --- END OF API -- - --- For internal use only. Don't use the “raw” functions outside of mcl_hunger! - -mcl_hunger.get_hunger_raw = function(player) local inv = player:get_inventory() if not inv then return nil end local hgp = inv:get_stack("hunger", 1):get_count() @@ -101,38 +68,40 @@ mcl_hunger.get_hunger_raw = function(player) return hgp-1 end -mcl_hunger.set_hunger_raw = function(player) +mcl_hunger.set_hunger = function(player, hunger, update_hudbars) + local name = player:get_player_name() local inv = player:get_inventory() local name = player:get_player_name() - local value = mcl_hunger.hunger[name] - if not inv or not value then return nil end - if value > 20 then value = 20 end - if value < 0 then value = 0 end + if not inv or not hunger then return nil end + if hunger > 20 then hunger = 20 end + if hunger < 0 then hunger = 0 end - inv:set_stack("hunger", 1, ItemStack({name=":", count=value+1})) + inv:set_stack("hunger", 1, ItemStack({name=":", count=hunger+1})) - return true + + if update_hudbars ~= false then + hb.change_hudbar(player, "food", hunger) + hb.change_hudbar(player, "saturation", nil, hunger) + end end +-- END OF API -- + minetest.register_on_joinplayer(function(player) local name = player:get_player_name() local inv = player:get_inventory() inv:set_size("hunger",1) - mcl_hunger.hunger[name] = mcl_hunger.get_hunger_raw(player) - mcl_hunger.hunger_out[name] = mcl_hunger.hunger[name] mcl_hunger.exhaustion[name] = 0.0 mcl_hunger.saturation[name] = 5.0 mcl_hunger.poisonings[name] = 0 custom_hud(player) - mcl_hunger.set_hunger_raw(player) end) minetest.register_on_respawnplayer(function(player) -- reset hunger (and save) local name = player:get_player_name() local h = 20 - mcl_hunger.hunger[name] = h - mcl_hunger.set_hunger_raw(player) + mcl_hunger.set_hunger(player, h, false) mcl_hunger.exhaustion[name] = 0.0 mcl_hunger.saturation[name] = 5.0 hb.change_hudbar(player, "exhaustion", mcl_hunger.exhaustion[name]) @@ -151,10 +120,9 @@ function mcl_hunger.exhaust(playername, increase) mcl_hunger.saturation[playername] = math.max(mcl_hunger.saturation[playername] - 1.0, 0.0) satuchanged = true elseif mcl_hunger.saturation[playername] < 0.0001 then - h = mcl_hunger.get_hunger_raw(player) + h = mcl_hunger.get_hunger(player) h = math.max(h-1, 0) - mcl_hunger.hunger[playername] = h - mcl_hunger.set_hunger_raw(player) + mcl_hunger.set_hunger(player, h) satuchanged = true end if satuchanged then @@ -183,7 +151,7 @@ minetest.register_globalstep(function(dtime) for _,player in ipairs(minetest.get_connected_players()) do local name = player:get_player_name() - local h = tonumber(mcl_hunger.hunger[name]) + local h = tonumber(mcl_hunger.get_hunger(player)) local hp = player:get_hp() if timer > 0.5 then -- Quick heal (every 0.5s) @@ -217,13 +185,6 @@ minetest.register_globalstep(function(dtime) mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_JUMP) end - -- Reduce hunter if 0 saturation - if timerMult == 0 and h > 0 and mcl_hunger.saturation[name] < 0.0001 then - end - - -- update all hud elements - update_hud(player) - end end end