Refactor set/get hunger functions

This commit is contained in:
Wuzzy 2017-05-20 21:23:02 +02:00
parent 26e13119cb
commit d8e1124ee4
2 changed files with 33 additions and 18 deletions

View File

@ -77,7 +77,6 @@ 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.get_hunger(user))
local hp = user:get_hp()
local pos = user:getpos()
@ -141,13 +140,15 @@ function mcl_hunger.item_eat(hunger_change, replace_with_item, poisen, heal, sou
mcl_hunger.saturate(name, saturation, false)
-- Add food points
local h = mcl_hunger.get_hunger(user)
if h < 20 and hunger_change then
h = h + hunger_change
if h > 20 then h = 20 end
mcl_hunger.set_hunger(user, h)
mcl_hunger.set_hunger(user, h, false)
end
hb.change_hudbar(user, "saturation", mcl_hunger.saturation[name], mcl_hunger.get_hunger(user))
hb.change_hudbar(user, "food", h)
hb.change_hudbar(user, "saturation", mcl_hunger.saturation[name], h)
end
-- Poison
if poisen then

View File

@ -54,35 +54,49 @@ 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"))
-- API START --
mcl_hunger.get_hunger = function(player)
local RAW_VALUE_FOOD = 1
local RAW_VALUE_SATURATION = 2
local RAW_VALUE_EXHAUSTION = 3
local get_player_value_raw = function(player, id, default)
local inv = player:get_inventory()
if not inv then return nil end
local hgp = inv:get_stack("hunger", 1):get_count()
if hgp == 0 then
hgp = 21
inv:set_stack("hunger", 1, ItemStack({name=":", count=hgp}))
local value = inv:get_stack("hunger", id):get_count()
if value == 0 then
inv:set_stack("hunger", id, ItemStack({name=":", count=default+1}))
return default
else
hgp = hgp
return value - 1
end
return hgp-1
end
local set_player_value_raw = function(player, id, value)
local inv = player:get_inventory()
inv:set_stack("hunger", id, ItemStack({name=":", count=value+1}))
return true
end
-- API START --
mcl_hunger.get_hunger = function(player)
return get_player_value_raw(player, RAW_VALUE_FOOD, 20)
end
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()
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=hunger+1}))
local ok = set_player_value_raw(player, RAW_VALUE_FOOD, hunger)
if not ok then
return false
end
if update_hudbars ~= false then
hb.change_hudbar(player, "food", hunger)
hb.change_hudbar(player, "saturation", nil, hunger)
end
return true
end
-- END OF API --
@ -90,7 +104,7 @@ end
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
local inv = player:get_inventory()
inv:set_size("hunger",1)
inv:set_size("hunger", 3)
mcl_hunger.exhaustion[name] = 0.0
mcl_hunger.saturation[name] = 5.0
mcl_hunger.poisonings[name] = 0