1
0
Fork 0

Merge pull request 'Fix crash on non existent xp-hud due to gamemode' (#2249) from fix-gamemode-xp into master

Reviewed-on: MineClone2/MineClone2#2249
This commit is contained in:
cora 2022-05-27 18:15:48 +00:00
commit c1114211fc
2 changed files with 49 additions and 24 deletions

View File

@ -156,33 +156,20 @@ function mcl_experience.throw_xp(pos, total_xp)
end end
end end
function mcl_experience.update(player) function mcl_experience.remove_hud(player)
local xp = mcl_experience.get_xp(player) if hud_bars[player] then
local cache = caches[player] player:hud_remove(hud_bars[player])
hud_bars[player] = nil
cache.level = xp_to_level(xp) end
if hud_levels[player] then
if not minetest.is_creative_enabled(player:get_player_name()) then player:hud_remove(hud_levels[player])
player:hud_change(hud_bars[player], "text", "mcl_experience_bar_background.png^[lowpart:" hud_levels[player] = nil
.. math.floor(math.floor(xp_to_bar(xp, cache.level) * 18) / 18 * 100)
.. ":mcl_experience_bar.png^[transformR270"
)
if cache.level == 0 then
player:hud_change(hud_levels[player], "text", "")
else
player:hud_change(hud_levels[player], "text", tostring(cache.level))
end
end end
end end
function mcl_experience.register_on_add_xp(func, priority) function mcl_experience.setup_hud(player)
table.insert(mcl_experience.on_add_xp, {func = func, priority = priority or 0}) if hud_bars[player] and hud_levels[player] then return end
end mcl_experience.remove_hud(player)
-- callbacks
minetest.register_on_joinplayer(function(player)
caches[player] = { caches[player] = {
last_time = get_time(), last_time = get_time(),
} }
@ -205,7 +192,39 @@ minetest.register_on_joinplayer(function(player)
z_index = 12, z_index = 12,
}) })
end end
end
function mcl_experience.update(player)
local xp = mcl_experience.get_xp(player)
local cache = caches[player]
cache.level = xp_to_level(xp)
if not minetest.is_creative_enabled(player:get_player_name()) then
if not hud_bars[player] then
mcl_experience.setup_hud(player)
end
player:hud_change(hud_bars[player], "text", "mcl_experience_bar_background.png^[lowpart:"
.. math.floor(math.floor(xp_to_bar(xp, cache.level) * 18) / 18 * 100)
.. ":mcl_experience_bar.png^[transformR270"
)
if cache.level == 0 then
player:hud_change(hud_levels[player], "text", "")
else
player:hud_change(hud_levels[player], "text", tostring(cache.level))
end
end
end
function mcl_experience.register_on_add_xp(func, priority)
table.insert(mcl_experience.on_add_xp, {func = func, priority = priority or 0})
end
-- callbacks
minetest.register_on_joinplayer(function(player)
mcl_experience.setup_hud(player)
mcl_experience.update(player) mcl_experience.update(player)
end) end)

View File

@ -206,6 +206,12 @@ local gamemodes = {
function mcl_inventory.player_set_gamemode(p,g) function mcl_inventory.player_set_gamemode(p,g)
local m = p:get_meta() local m = p:get_meta()
m:set_string("gamemode",g) m:set_string("gamemode",g)
if g == "survival" then
mcl_experience.setup_hud(p)
mcl_experience.update(p)
elseif g == "creative" then
mcl_experience.remove_hud(p)
end
set_inventory(p) set_inventory(p)
end end