From 8f53074b58158a387ba0867a39063336394fad34 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Mon, 8 Jul 2024 21:40:11 -0500 Subject: [PATCH] Reorder functions to prevent crash --- mods/ITEMS/mcl_composters/init.lua | 70 +++++++++++++++--------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/mods/ITEMS/mcl_composters/init.lua b/mods/ITEMS/mcl_composters/init.lua index 7d4d135bd..28883dc48 100644 --- a/mods/ITEMS/mcl_composters/init.lua +++ b/mods/ITEMS/mcl_composters/init.lua @@ -48,6 +48,41 @@ local vector_offset = vector.offset local is_protected = minetest.is_protected local record_protection_violation = minetest.record_protection_violation +--- Math and node swap during compost progression +---@param pos Vector Position of the node +---@param node node +---@param chance integer Value of "compostability" group of inserted item +local function composter_progress_chance(pos, node, chance) + -- calculate leveling up chance + local rand = math.random(0,100) + if chance >= rand then + -- get current compost level + local level = registered_nodes[node.name]["_mcl_compost_level"] + -- spawn green particles above new layer + mcl_bone_meal.add_bone_meal_particle(vector_offset(pos, 0, level/8, 0)) + -- update composter block + if level < 7 then + level = level + 1 + else + level = "ready" + end + swap_node(pos, {name = "mcl_composters:composter_" .. level}) + minetest.sound_play({name="default_grass_footstep", gain=0.4}, { + pos = pos, + gain= 0.4, + max_hear_distance = 16, + }, true) + -- a full composter becomes ready for harvest after one second + -- the block will get updated by the node timer callback set in node reg def + if level == 7 then + local timer = get_node_timer(pos) + if not timer:is_started() then + timer:start(1) + end + end + end +end + --- Fill the composter when rightclicked. -- -- `on_rightclick` handler for composter blocks of all fill levels except @@ -86,41 +121,6 @@ local function composter_add_item(pos, node, player, itemstack, pointed_thing) return itemstack end ---- Math and node swap during compost progression ----@param pos Vector Position of the node ----@param node node ----@param chance integer Value of "compostability" group of inserted item -local function composter_progress_chance(pos, node, chance) - -- calculate leveling up chance - local rand = math.random(0,100) - if chance >= rand then - -- get current compost level - local level = registered_nodes[node.name]["_mcl_compost_level"] - -- spawn green particles above new layer - mcl_bone_meal.add_bone_meal_particle(vector_offset(pos, 0, level/8, 0)) - -- update composter block - if level < 7 then - level = level + 1 - else - level = "ready" - end - swap_node(pos, {name = "mcl_composters:composter_" .. level}) - minetest.sound_play({name="default_grass_footstep", gain=0.4}, { - pos = pos, - gain= 0.4, - max_hear_distance = 16, - }, true) - -- a full composter becomes ready for harvest after one second - -- the block will get updated by the node timer callback set in node reg def - if level == 7 then - local timer = get_node_timer(pos) - if not timer:is_started() then - timer:start(1) - end - end - end -end - --- Update a full composter block to ready for harvesting. -- -- `on_timer` handler. The timer is set in function 'composter_add_item'