From 38ad237d92d4c650542a68ff05024c452a017100 Mon Sep 17 00:00:00 2001 From: kay27 Date: Mon, 26 Oct 2020 10:24:38 +0400 Subject: [PATCH] Fix multithreaded addition of XP --- mods/HUD/mcl_experience/init.lua | 36 ++++++++++++++++---------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/mods/HUD/mcl_experience/init.lua b/mods/HUD/mcl_experience/init.lua index 7483538f7..7ffa20bdf 100644 --- a/mods/HUD/mcl_experience/init.lua +++ b/mods/HUD/mcl_experience/init.lua @@ -238,31 +238,31 @@ function mcl_experience.add_experience(player, experience) local old_bar, old_xp, old_level = temp_pool.bar, temp_pool.xp, temp_pool.level temp_pool.xp = math.min(math.max(temp_pool.xp + experience, 0), max_xp) - if (temp_pool.xp >= temp_pool.xp_next_level) or (temp_pool.xp < old_xp) then + + if (temp_pool.xp < temp_pool.xp_next_level) and (temp_pool.xp >= old_xp) then + temp_pool.bar = temp_pool.bar + temp_pool.bar_step * experience + else temp_pool.level = mcl_experience.xp_to_level(temp_pool.xp) temp_pool.bar, temp_pool.bar_step, temp_pool.xp_next_level = mcl_experience.xp_to_bar(temp_pool.xp, temp_pool.level) - if old_level ~= temp_pool.level then - if minetest.get_us_time()/1000000 - temp_pool.last_time > 0.04 then - if experience > 0 then - minetest.sound_play("level_up",{gain=0.2,to_player = name}) - end - temp_pool.last_time = minetest.get_us_time()/1000000 - end - hud_manager.change_hud({player = player, hud_name = "xp_level", element = "text", data = tostring(temp_pool.level)}) - end - else - if minetest.get_us_time()/1000000 - temp_pool.last_time > 0.01 then - temp_pool.last_time = minetest.get_us_time()/1000000 - if experience > 0 then - minetest.sound_play("experience",{gain=0.1,to_player = name,pitch=math.random(75,99)/100}) - end - end - temp_pool.bar = temp_pool.bar + temp_pool.bar_step * experience end if old_bar ~= temp_pool.bar then hud_manager.change_hud({player = player, hud_name = "experience_bar", element = "number", data = math.floor(temp_pool.bar)}) end + + if experience > 0 and minetest.get_us_time()/1000000 - temp_pool.last_time > 0.01 then + if old_level ~= temp_pool.level then + minetest.sound_play("level_up",{gain=0.2,to_player = name}) + temp_pool.last_time = minetest.get_us_time()/1000000 + 0.2 + else + minetest.sound_play("experience",{gain=0.1,to_player = name,pitch=math.random(75,99)/100}) + temp_pool.last_time = minetest.get_us_time()/1000000 + end + end + + if old_level ~= temp_pool.level then + hud_manager.change_hud({player = player, hud_name = "xp_level", element = "text", data = tostring(temp_pool.level)}) + end end --reset player level