From 7fe1be2c18be119c164a4ed0abf9ab113e79c603 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Mon, 3 Apr 2023 00:41:39 +0100 Subject: [PATCH] Add error handling to sunlight checking to prevent crashing and improve diagnosis. --- mods/CORE/mcl_util/init.lua | 18 +++++++++++++ mods/ENTITIES/mcl_mobs/physics.lua | 2 +- mods/ENVIRONMENT/mcl_raids/init.lua | 2 +- mods/ENVIRONMENT/mcl_zombie_sieges/init.lua | 2 +- .../REDSTONE/mesecons_solarpanel/init.lua | 26 +++++-------------- 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index 601a46c9f..2b3e0918a 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -64,6 +64,24 @@ function mcl_util.check_dtime_timer(self, dtime, timer_name, threshold) return false end +-- Minetest 5.3.0 or less can only measure the light level. This came in at 5.4 +-- This function has been known to fail in multiple places so the error handling is added increase safety and improve +-- debugging. See: +-- https://git.minetest.land/MineClone2/MineClone2/issues/1392 +function mcl_util.get_natural_light (pos, time) + local status, retVal = pcall(minetest.get_natural_light, pos, time) + if status then + return retVal + else + minetest.log("warning", "Failed to get natural light at pos: " .. dump(pos) .. ", time: " .. dump(time)) + if (pos) then + local node = minetest.get_node(pos) + minetest.log("warning", "Node at pos: " .. dump(node.name)) + end + end + return 0 +end + function mcl_util.file_exists(name) if type(name) ~= "string" then return end local f = io.open(name) diff --git a/mods/ENTITIES/mcl_mobs/physics.lua b/mods/ENTITIES/mcl_mobs/physics.lua index 956eb992c..4ef72eed7 100644 --- a/mods/ENTITIES/mcl_mobs/physics.lua +++ b/mods/ENTITIES/mcl_mobs/physics.lua @@ -643,7 +643,7 @@ function mob_class:do_env_damage() --minetest.log("warning", "Pos is ignored: " .. dump(pos)) end - local sunlight = minetest.get_natural_light(pos, self.time_of_day) + local sunlight = mcl_util.get_natural_light(pos, self.time_of_day) if self.light_damage ~= 0 and (sunlight or 0) > 12 then if self:deal_light_damage(pos, self.light_damage) then diff --git a/mods/ENVIRONMENT/mcl_raids/init.lua b/mods/ENVIRONMENT/mcl_raids/init.lua index 8c996f0ce..8e7c644ae 100644 --- a/mods/ENVIRONMENT/mcl_raids/init.lua +++ b/mods/ENVIRONMENT/mcl_raids/init.lua @@ -240,7 +240,7 @@ end local function start_firework_rocket(pos) local p = get_point_on_circle(pos,math.random(32,64),32) local n = minetest.get_node(p) - local l = minetest.get_natural_light(pos,0.5) + local l = mcl_util.get_natural_light(pos,0.5) if n.name ~= "air" or l <= minetest.LIGHT_MAX then return end local o = minetest.add_entity(p,"mcl_bows:rocket_entity") o:get_luaentity()._harmless = true diff --git a/mods/ENVIRONMENT/mcl_zombie_sieges/init.lua b/mods/ENVIRONMENT/mcl_zombie_sieges/init.lua index c2a53d79a..fefb17fa5 100644 --- a/mods/ENVIRONMENT/mcl_zombie_sieges/init.lua +++ b/mods/ENVIRONMENT/mcl_zombie_sieges/init.lua @@ -1,7 +1,7 @@ local zombie_siege_enabled = minetest.settings:get_bool("mcl_raids_zombie_siege", false) local function check_spawn_pos(pos) - return minetest.get_natural_light(pos) < 7 + return mcl_util.get_natural_light(pos) < 7 end local function spawn_zombies(self) diff --git a/mods/ITEMS/REDSTONE/mesecons_solarpanel/init.lua b/mods/ITEMS/REDSTONE/mesecons_solarpanel/init.lua index 129c28eea..7780cbfee 100644 --- a/mods/ITEMS/REDSTONE/mesecons_solarpanel/init.lua +++ b/mods/ITEMS/REDSTONE/mesecons_solarpanel/init.lua @@ -35,29 +35,17 @@ local function path_to_sunlight_exists(position, light_level) end local function sunlight_visible(position) - local light_level - -- Minetest 5.4.0+ can measure the daylight level at a position - if nil ~= minetest.get_natural_light then - light_level = minetest.get_natural_light( - position, - nil - ) - if light_level >= 12 then - return true - end - else -- Minetest 5.3.0 or less can only measure the light level + local light_level = mcl_util.get_natural_light (position) + if light_level >= 12 then + --minetest.log("Light is greater than 12") + return true + else local time = minetest.get_timeofday() * 24000 -- only check light level during day if time > 6000 and time < 18000 then - light_level = minetest.get_node_light( - position, - nil - ) + light_level = minetest.get_node_light(position, nil) if light_level >= 12 then - return path_to_sunlight_exists( - position, - 12 - ) + return path_to_sunlight_exists(position, 12) end end end