Skip to content

Commit

Permalink
Bugfixes. Preserve timers and param2 (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Izzyb1234 committed Jun 23, 2022
1 parent 1646439 commit b523656
Showing 1 changed file with 51 additions and 5 deletions.
56 changes: 51 additions & 5 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ local function is_entity_valid(entity)
return entity and (entity.obj:is_player() or (entity.obj:get_luaentity() and entity.obj:get_luaentity().name) or false)
end

-- Check whether a node was registered by the wield_light mod
local function is_wieldlight_node(pos_vec)
local name = string.sub(minetest.get_node(pos_vec).name, 1, #mod_name)
return name == mod_name
end

-- Get the projected position of an entity based on its velocity, rounded to the nearest block
local function entity_pos(obj, offset)
local velocity
Expand Down Expand Up @@ -174,19 +180,48 @@ local function update_entity(entity)
end
end
end
minetest.get_node_timer(pos):start(cleanup_interval)
end
if active_lights[pos_str] then
if is_wieldlight_node(pos) then
minetest.get_node_timer(pos):start(cleanup_interval)
end
end
entity.update = false
end


-- Save the original nodes timer if it has one
local function save_timer(pos_vec)
local timer = minetest.get_node_timer(pos_vec)
if timer:is_started() then
local meta = minetest.get_meta(pos_vec)
meta:set_float("saved_timer_timeout", timer:get_timeout())
meta:set_float("saved_timer_elapsed", timer:get_elapsed())
end
end

-- Restore the original nodes timer if it had one
local function restore_timer(pos_vec)
local meta = minetest.get_meta(pos_vec)
local timeout = meta:get_float("saved_timer_timeout")
if timeout > 0 then
local elapsed = meta:get_float("saved_timer_elapsed")
local timer = minetest.get_node_timer(pos_vec)
timer:set(timeout, elapsed)
meta:set_string("saved_timer_timeout","")
meta:set_string("saved_timer_elapsed","")
end
end

-- Replace a lighting node with its original counterpart
local function reset_lighting_node(pos)
local existing_node = minetest.get_node(pos)
local lighting_node = wielded_light.get_lighting_node(existing_node.name)
if not lighting_node then
return
end
minetest.swap_node(pos, { name = lighting_node.node })
minetest.swap_node(pos, { name = lighting_node.node,param2 = existing_node.param2 })
restore_timer(pos)
end

-- Will be run once the node timer expires
Expand All @@ -209,6 +244,7 @@ local function cleanup_timer_callback(pos, elapsed)
end
end


-- Recalculate the total light level for a given position and update the light level there
local function recalc_light(pos)
-- If not in active lights list we can't do anything
Expand Down Expand Up @@ -244,7 +280,8 @@ local function recalc_light(pos)
max_light = math.min(max_light, minetest.LIGHT_MAX)

-- Get the current light level in this position
local name = minetest.get_node(pos_vec).name
local existing_node = minetest.get_node(pos_vec)
local name = existing_node.name
local old_value = wielded_light.level_of_lighting_node(name) or 0

-- If the light level has changed, set the coresponding light node and initiate the cleanup timer
Expand All @@ -256,8 +293,13 @@ local function recalc_light(pos)
node_name = lighting_nodes[name].node
end
if node_name then
if not is_wieldlight_node(pos_vec) then
save_timer(pos_vec)
end

minetest.swap_node(pos_vec, {
name = lightable_nodes[node_name][max_light]
name = lightable_nodes[node_name][max_light],
param2 = existing_node.param2
})
minetest.get_node_timer(pos_vec):start(cleanup_interval)
else
Expand Down Expand Up @@ -370,6 +412,10 @@ function wielded_light.register_lightable_node(node_name, property_overrides, cu
new_definition.mod_origin = mod_name
new_definition.groups = new_definition.groups or {}
new_definition.groups.not_in_creative_inventory = 1
-- Make sure original node is dropped if a lit node is dug
if not new_definition.drop then
new_definition.drop = node_name
end

-- Allow any properties to be overridden on registration
for prop, val in pairs(property_overrides) do
Expand Down Expand Up @@ -425,7 +471,7 @@ function wielded_light.get_light_position(pos)
{x=0, y=0, z=0},
{x=0, y=1, z=0}, {x=0, y=-1, z=0},
{x=1, y=0, z=0}, {x=-1, y=0, z=0},
{x=0, y=0, z=1}, {x=0, y=0, z=1},
{x=0, y=0, z=1}, {x=0, y=0, z=-1},
}
for _, around in ipairs(around_vector) do
local light_pos = vector.add(pos, around)
Expand Down

0 comments on commit b523656

Please sign in to comment.