Fix some bugs with poison bar displaying

This commit is contained in:
Wuzzy 2017-06-06 18:33:01 +02:00
parent f1b99dc117
commit 9f683542af
2 changed files with 29 additions and 11 deletions

View File

@ -73,8 +73,11 @@ function mcl_hunger.eat(hp_change, replace_with_item, itemstack, user, pointed_t
end end
-- Reset HUD bars after poisoning -- Reset HUD bars after poisoning
local function reset_bars(player) local function reset_bars_poison_damage(player)
hb.change_hudbar(player, "health", nil, nil, "hudbars_icon_health.png", nil, "hudbars_bar_health.png") hb.change_hudbar(player, "health", nil, nil, "hudbars_icon_health.png", nil, "hudbars_bar_health.png")
end
local function reset_bars_poison_hunger(player)
hb.change_hudbar(player, "hunger", nil, nil, "hbhunger_icon.png", nil, "hbhunger_bar.png") hb.change_hudbar(player, "hunger", nil, nil, "hbhunger_icon.png", nil, "hbhunger_bar.png")
if mcl_hunger.debug then if mcl_hunger.debug then
hb.change_hudbar(player, "exhaustion", nil, nil, nil, nil, "mcl_hunger_bar_exhaustion.png") hb.change_hudbar(player, "exhaustion", nil, nil, nil, nil, "mcl_hunger_bar_exhaustion.png")
@ -87,17 +90,26 @@ local function poisonp(tick, time, time_left, damage, exhaustion, player)
if not player:is_player() then if not player:is_player() then
return return
end end
local name = player:get_player_name()
-- Abort if poisonings have been stopped -- Abort if poisonings have been stopped
if mcl_hunger.poisonings[player:get_player_name()] == 0 then if mcl_hunger.poison_damage[name] == 0 and mcl_hunger.poison_hunger[name] == 0 then
return return
end end
time_left = time_left + tick time_left = time_left + tick
if time_left < time then if time_left < time then
minetest.after(tick, poisonp, tick, time, time_left, damage, exhaustion, player) minetest.after(tick, poisonp, tick, time, time_left, damage, exhaustion, player)
else else
mcl_hunger.poisonings[player:get_player_name()] = mcl_hunger.poisonings[player:get_player_name()] - 1 if damage > 0 then
if mcl_hunger.poisonings[player:get_player_name()] <= 0 then mcl_hunger.poison_damage[name] = mcl_hunger.poison_damage[name] - 1
reset_bars(player) end
if exhaustion > 0 then
mcl_hunger.poison_hunger [name] = mcl_hunger.poison_hunger[name] - 1
end
if mcl_hunger.poison_damage[name] <= 0 then
reset_bars_poison_damage(player)
end
if mcl_hunger.poison_hunger[name] <= 0 then
reset_bars_poison_hunger(player)
end end
end end
@ -105,14 +117,17 @@ local function poisonp(tick, time, time_left, damage, exhaustion, player)
if player:get_hp()-damage > 0 then if player:get_hp()-damage > 0 then
player:set_hp(player:get_hp()-damage) player:set_hp(player:get_hp()-damage)
end end
mcl_hunger.exhaust(player:get_player_name(), exhaustion)
mcl_hunger.exhaust(name, exhaustion)
end end
-- Immediately stop all poisonings for this player -- Immediately stop all poisonings for this player
function mcl_hunger.stop_poison(player) function mcl_hunger.stop_poison(player)
mcl_hunger.poisonings[player:get_player_name()] = 0 mcl_hunger.poison_damage[player:get_player_name()] = 0
reset_bars(player) mcl_hunger.poison_hunger[player:get_player_name()] = 0
reset_bars_poison_damage(player)
reset_bars_poison_hunger(player)
end end
local poisonrandomizer = PseudoRandom(os.time()) local poisonrandomizer = PseudoRandom(os.time())
@ -212,14 +227,15 @@ function mcl_hunger.item_eat(hunger_change, replace_with_item, poisontime, poiso
-- Set poison bars -- Set poison bars
if poison and poison > 0 then if poison and poison > 0 then
hb.change_hudbar(user, "health", nil, nil, "hbhunger_icon_health_poison.png", nil, "hbhunger_bar_health_poison.png") hb.change_hudbar(user, "health", nil, nil, "hbhunger_icon_health_poison.png", nil, "hbhunger_bar_health_poison.png")
mcl_hunger.poison_damage[name] = mcl_hunger.poison_damage[name] + 1
end end
if exhaust and exhaust > 0 then if exhaust and exhaust > 0 then
hb.change_hudbar(user, "hunger", nil, nil, "mcl_hunger_icon_foodpoison.png", nil, "mcl_hunger_bar_foodpoison.png") hb.change_hudbar(user, "hunger", nil, nil, "mcl_hunger_icon_foodpoison.png", nil, "mcl_hunger_bar_foodpoison.png")
if mcl_hunger.debug then if mcl_hunger.debug then
hb.change_hudbar(user, "exhaustion", nil, nil, nil, nil, "mcl_hunger_bar_foodpoison.png") hb.change_hudbar(user, "exhaustion", nil, nil, nil, nil, "mcl_hunger_bar_foodpoison.png")
end end
mcl_hunger.poison_hunger[name] = mcl_hunger.poison_hunger[name] + 1
end end
mcl_hunger.poisonings[name] = mcl_hunger.poisonings[name] + 1
poisonp(1, poisontime, 0, poison, exhaust, user) poisonp(1, poisontime, 0, poison, exhaust, user)
end end
end end

View File

@ -48,7 +48,8 @@ end
]] ]]
-- Count number of poisonings a player has at once -- Count number of poisonings a player has at once
mcl_hunger.poisonings = {} mcl_hunger.poison_damage = {} -- damaging poison
mcl_hunger.poison_hunger = {} -- food poisoning, increasing hunger
-- Cooldown timers for each player, to force a short delay between consuming 2 food items -- Cooldown timers for each player, to force a short delay between consuming 2 food items
mcl_hunger.last_eat = {} mcl_hunger.last_eat = {}
@ -139,7 +140,8 @@ 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()
init_hud(player) init_hud(player)
mcl_hunger.poisonings[name] = 0 mcl_hunger.poison_damage[name] = 0
mcl_hunger.poison_hunger[name] = 0
mcl_hunger.last_eat[name] = -1 mcl_hunger.last_eat[name] = -1
end) end)