From aa927fe26a2ecb8f07d067642cbaeab1070238c8 Mon Sep 17 00:00:00 2001 From: FaceDeer Date: Sun, 9 Jun 2019 23:53:09 -0600 Subject: [PATCH] protect against extremely large builder periods causing entities to be added out of range --- util.lua | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/util.lua b/util.lua index 8316fd7..327c7e7 100644 --- a/util.lua +++ b/util.lua @@ -391,32 +391,45 @@ digtron.is_soft_material = function(target) return false end +-- If someone sets very large offsets or intervals for the offset markers they might be added too far +-- away. safe_add_entity causes these attempts to be ignored rather than crashing the game. +-- returns the entity if successful, nil otherwise +function safe_add_entity(pos, name) + success, ret = pcall(minetest.add_entity, pos, name) + if success then return ret else return nil end +end + digtron.show_offset_markers = function(pos, offset, period) local buildpos = digtron.find_new_pos(pos, minetest.get_node(pos).param2) local x_pos = math.floor((buildpos.x+offset)/period)*period - offset - minetest.add_entity({x=x_pos, y=buildpos.y, z=buildpos.z}, "digtron:marker") + safe_add_entity({x=x_pos, y=buildpos.y, z=buildpos.z}, "digtron:marker") if x_pos >= buildpos.x then - minetest.add_entity({x=x_pos - period, y=buildpos.y, z=buildpos.z}, "digtron:marker") + safe_add_entity({x=x_pos - period, y=buildpos.y, z=buildpos.z}, "digtron:marker") end if x_pos <= buildpos.x then - minetest.add_entity({x=x_pos + period, y=buildpos.y, z=buildpos.z}, "digtron:marker") + safe_add_entity({x=x_pos + period, y=buildpos.y, z=buildpos.z}, "digtron:marker") end local y_pos = math.floor((buildpos.y+offset)/period)*period - offset - minetest.add_entity({x=buildpos.x, y=y_pos, z=buildpos.z}, "digtron:marker_vertical") + safe_add_entity({x=buildpos.x, y=y_pos, z=buildpos.z}, "digtron:marker_vertical") if y_pos >= buildpos.y then - minetest.add_entity({x=buildpos.x, y=y_pos - period, z=buildpos.z}, "digtron:marker_vertical") + safe_add_entity({x=buildpos.x, y=y_pos - period, z=buildpos.z}, "digtron:marker_vertical") end if y_pos <= buildpos.y then - minetest.add_entity({x=buildpos.x, y=y_pos + period, z=buildpos.z}, "digtron:marker_vertical") + safe_add_entity({x=buildpos.x, y=y_pos + period, z=buildpos.z}, "digtron:marker_vertical") end local z_pos = math.floor((buildpos.z+offset)/period)*period - offset - minetest.add_entity({x=buildpos.x, y=buildpos.y, z=z_pos}, "digtron:marker"):setyaw(1.5708) + + local entity = safe_add_entity({x=buildpos.x, y=buildpos.y, z=z_pos}, "digtron:marker") + if entity ~= nil then entity:setyaw(1.5708) end + if z_pos >= buildpos.z then - minetest.add_entity({x=buildpos.x, y=buildpos.y, z=z_pos - period}, "digtron:marker"):setyaw(1.5708) + local entity = safe_add_entity({x=buildpos.x, y=buildpos.y, z=z_pos - period}, "digtron:marker") + if entity ~= nil then entity:setyaw(1.5708) end end if z_pos <= buildpos.z then - minetest.add_entity({x=buildpos.x, y=buildpos.y, z=z_pos + period}, "digtron:marker"):setyaw(1.5708) + local entity = safe_add_entity({x=buildpos.x, y=buildpos.y, z=z_pos + period}, "digtron:marker") + if entity ~= nil then entity:setyaw(1.5708) end end end \ No newline at end of file