Reorder functions to prevent crash

This commit is contained in:
teknomunk 2024-07-08 21:40:11 -05:00
parent 70e8ba9a89
commit 8f53074b58

View file

@ -48,6 +48,41 @@ local vector_offset = vector.offset
local is_protected = minetest.is_protected local is_protected = minetest.is_protected
local record_protection_violation = minetest.record_protection_violation 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. --- Fill the composter when rightclicked.
-- --
-- `on_rightclick` handler for composter blocks of all fill levels except -- `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 return itemstack
end 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. --- Update a full composter block to ready for harvesting.
-- --
-- `on_timer` handler. The timer is set in function 'composter_add_item' -- `on_timer` handler. The timer is set in function 'composter_add_item'