Merge pull request '[5.6] safely handle entities involving set_hp' (#2526) from iliekprogrammar/MineClone2:safe_entity_handling into master

Reviewed-on: https://git.minetest.land/MineClone2/MineClone2/pulls/2526
Reviewed-by: cora <cora@noreply.git.minetest.land>
This commit is contained in:
cora 2022-08-07 21:05:11 +00:00
commit 7b92bb89ce
7 changed files with 13 additions and 2 deletions

@ -337,7 +337,6 @@ local function trace_explode(pos, strength, raydirs, radius, info, direct, sourc
if not obj:is_player() then if not obj:is_player() then
return return
end end
mcl_util.deal_damage(obj, damage, {type = "explosion", direct = direct, source = source}) mcl_util.deal_damage(obj, damage, {type = "explosion", direct = direct, source = source})
obj:add_velocity(vector.multiply(punch_dir, impact * 20)) obj:add_velocity(vector.multiply(punch_dir, impact * 20))

@ -202,6 +202,8 @@ end
function boat.on_step(self, dtime, moveresult) function boat.on_step(self, dtime, moveresult)
mcl_burning.tick(self.object, dtime, self) mcl_burning.tick(self.object, dtime, self)
-- mcl_burning.tick may remove object immediately
if not self.object:get_pos() then return end
self._v = get_v(self.object:get_velocity()) * get_sign(self._v) self._v = get_v(self.object:get_velocity()) * get_sign(self._v)
local v_factor = 1 local v_factor = 1

@ -3618,6 +3618,8 @@ local mob_step = function(self, dtime)
check_aggro(self,dtime) check_aggro(self,dtime)
if not self.fire_resistant then if not self.fire_resistant then
mcl_burning.tick(self.object, dtime, self) mcl_burning.tick(self.object, dtime, self)
-- mcl_burning.tick may remove object immediately
if not self.object:get_pos() then return end
end end
local pos = self.object:get_pos() local pos = self.object:get_pos()

@ -120,9 +120,10 @@ function lightning.strike(pos)
if not pos then if not pos then
return false return false
end end
local objects = get_objects_inside_radius(pos2, 3.5)
if lightning.on_strike_functions then if lightning.on_strike_functions then
for _, func in pairs(lightning.on_strike_functions) do for _, func in pairs(lightning.on_strike_functions) do
-- allow on_strike callbacks to destroy entities by re-obtaining objects for each callback
local objects = get_objects_inside_radius(pos2, 3.5)
func(pos, pos2, objects) func(pos, pos2, objects)
end end
end end
@ -174,6 +175,7 @@ lightning.register_on_strike(function(pos, pos2, objects)
elseif lua and lua.name == "mobs_mc:creeper" then elseif lua and lua.name == "mobs_mc:creeper" then
mcl_util.replace_mob(obj, "mobs_mc:creeper_charged") mcl_util.replace_mob(obj, "mobs_mc:creeper_charged")
else else
-- WARNING: unsafe entity handling. object may be removed immediately
mcl_util.deal_damage(obj, 5, { type = "lightning_bolt" }) mcl_util.deal_damage(obj, 5, { type = "lightning_bolt" })
end end
end end

@ -90,6 +90,8 @@ mcl_damage.register_modifier(function(obj, damage, reason)
if thorns_damage > 0 and reason.type ~= "thorns" and reason.source ~= obj then if thorns_damage > 0 and reason.type ~= "thorns" and reason.source ~= obj then
mcl_util.deal_damage(reason.source, thorns_damage, {type = "thorns", direct = obj}) mcl_util.deal_damage(reason.source, thorns_damage, {type = "thorns", direct = obj})
-- mcl_util.deal_damage may remove object immediately
if not reason.source:get_pos() then return end
local thorns_item = thorns_pieces[math.random(#thorns_pieces)] local thorns_item = thorns_pieces[math.random(#thorns_pieces)]

@ -115,6 +115,8 @@ end
function ARROW_ENTITY.on_step(self, dtime) function ARROW_ENTITY.on_step(self, dtime)
mcl_burning.tick(self.object, dtime, self) mcl_burning.tick(self.object, dtime, self)
-- mcl_burning.tick may remove object immediately
if not self.object:get_pos() then return end
self._time_in_air = self._time_in_air + .001 self._time_in_air = self._time_in_air + .001

@ -313,6 +313,8 @@ end
function ARROW_ENTITY.on_step(self, dtime) function ARROW_ENTITY.on_step(self, dtime)
mcl_burning.tick(self.object, dtime, self) mcl_burning.tick(self.object, dtime, self)
-- mcl_burning.tick may remove object immediately
if not self.object:get_pos() then return end
self._time_in_air = self._time_in_air + .001 self._time_in_air = self._time_in_air + .001