From 7c5554a0f6c998aeeb0ddfe0d5f060354a24c69d Mon Sep 17 00:00:00 2001 From: kay27 Date: Sat, 19 Feb 2022 03:03:23 +0400 Subject: [PATCH] Remove crash on creative to survival gamemode change --- mods/HUD/hudbars/init.lua | 4 +-- mods/HUD/mcl_experience/init.lua | 45 ++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/mods/HUD/hudbars/init.lua b/mods/HUD/hudbars/init.lua index 505ff403b..ae4dd1713 100644 --- a/mods/HUD/hudbars/init.lua +++ b/mods/HUD/hudbars/init.lua @@ -52,11 +52,11 @@ end -- Load default settings dofile(modpath.."/default_settings.lua") -if minetest.get_modpath("mcl_experience") and not minetest.is_creative_enabled("") then +--if minetest.get_modpath("mcl_experience") and not minetest.is_creative_enabled("") then -- reserve some space for experience bar: hb.settings.start_offset_left.y = hb.settings.start_offset_left.y - 20 hb.settings.start_offset_right.y = hb.settings.start_offset_right.y - 20 -end +--end local function player_exists(player) return player ~= nil and player:is_player() diff --git a/mods/HUD/mcl_experience/init.lua b/mods/HUD/mcl_experience/init.lua index 2b6d4d5bf..65f456f81 100644 --- a/mods/HUD/mcl_experience/init.lua +++ b/mods/HUD/mcl_experience/init.lua @@ -156,6 +156,27 @@ function mcl_experience.throw_xp(pos, total_xp) end end +local function init_hudbars(player) + if not minetest.is_creative_enabled(player:get_player_name()) then + hud_bars[player] = player:hud_add({ + hud_elem_type = "image", + position = {x = 0.5, y = 1}, + offset = {x = (-9 * 28) - 3, y = -(48 + 24 + 16 - 5)}, + scale = {x = 2.8, y = 3.0}, + alignment = {x = 1, y = 1}, + z_index = 11, + }) + + hud_levels[player] = player:hud_add({ + hud_elem_type = "text", + position = {x = 0.5, y = 1}, + number = 0x80FF20, + offset = {x = 0, y = -(48 + 24 + 24)}, + z_index = 12, + }) + end +end + function mcl_experience.update(player) if not mcl_util or not mcl_util.is_player(player) then return end local xp = mcl_experience.get_xp(player) @@ -164,6 +185,9 @@ function mcl_experience.update(player) cache.level = xp_to_level(xp) if not minetest.is_creative_enabled(player:get_player_name()) then + if not hud_bars[player] then + init_hudbars(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" @@ -187,26 +211,7 @@ minetest.register_on_joinplayer(function(player) caches[player] = { last_time = get_time(), } - - if not minetest.is_creative_enabled(player:get_player_name()) then - hud_bars[player] = player:hud_add({ - hud_elem_type = "image", - position = {x = 0.5, y = 1}, - offset = {x = (-9 * 28) - 3, y = -(48 + 24 + 16 - 5)}, - scale = {x = 2.8, y = 3.0}, - alignment = {x = 1, y = 1}, - z_index = 11, - }) - - hud_levels[player] = player:hud_add({ - hud_elem_type = "text", - position = {x = 0.5, y = 1}, - number = 0x80FF20, - offset = {x = 0, y = -(48 + 24 + 24)}, - z_index = 12, - }) - end - + init_hudbars(player) mcl_experience.update(player) end)