Fix TNT mod crash when entities disappear during explosion (#2616)
authorsfan5 <sfan5@live.de>
Fri, 6 Mar 2020 20:51:19 +0000 (21:51 +0100)
committerGitHub <noreply@github.com>
Fri, 6 Mar 2020 20:51:19 +0000 (21:51 +0100)
mods/tnt/init.lua

index 82f0b9bea8df1cb72bf2080ed7d140ffa20a8b43..6565f852c65e96daab252cd6e06aa779765d8dbb 100644 (file)
@@ -163,9 +163,8 @@ local function entity_physics(pos, radius, drops)
 
                local damage = (4 / dist) * radius
                if obj:is_player() then
-                       -- currently the engine has no method to set
-                       -- player velocity. See #2960
-                       -- instead, we knock the player back 1.0 node, and slightly upwards
+                       -- we knock the player back 1.0 node, and slightly upwards
+                       -- TODO: switch to add_player_velocity() introduced in 5.1
                        local dir = vector.normalize(vector.subtract(obj_pos, pos))
                        local moveoff = vector.multiply(dir, dist + 1.0)
                        local newpos = vector.add(pos, moveoff)
@@ -174,31 +173,35 @@ local function entity_physics(pos, radius, drops)
 
                        obj:set_hp(obj:get_hp() - damage)
                else
-                       local do_damage = true
-                       local do_knockback = true
-                       local entity_drops = {}
                        local luaobj = obj:get_luaentity()
-                       local objdef = minetest.registered_entities[luaobj.name]
 
-                       if objdef and objdef.on_blast then
-                               do_damage, do_knockback, entity_drops = objdef.on_blast(luaobj, damage)
-                       end
+                       -- object might have disappeared somehow
+                       if luaobj then
+                               local do_damage = true
+                               local do_knockback = true
+                               local entity_drops = {}
+                               local objdef = minetest.registered_entities[luaobj.name]
 
-                       if do_knockback then
-                               local obj_vel = obj:get_velocity()
-                               obj:set_velocity(calc_velocity(pos, obj_pos,
-                                               obj_vel, radius * 10))
-                       end
-                       if do_damage then
-                               if not obj:get_armor_groups().immortal then
-                                       obj:punch(obj, 1.0, {
-                                               full_punch_interval = 1.0,
-                                               damage_groups = {fleshy = damage},
-                                       }, nil)
+                               if objdef and objdef.on_blast then
+                                       do_damage, do_knockback, entity_drops = objdef.on_blast(luaobj, damage)
+                               end
+
+                               if do_knockback then
+                                       local obj_vel = obj:get_velocity()
+                                       obj:set_velocity(calc_velocity(pos, obj_pos,
+                                                       obj_vel, radius * 10))
+                               end
+                               if do_damage then
+                                       if not obj:get_armor_groups().immortal then
+                                               obj:punch(obj, 1.0, {
+                                                       full_punch_interval = 1.0,
+                                                       damage_groups = {fleshy = damage},
+                                               }, nil)
+                                       end
+                               end
+                               for _, item in pairs(entity_drops) do
+                                       add_drop(drops, item)
                                end
-                       end
-                       for _, item in pairs(entity_drops) do
-                               add_drop(drops, item)
                        end
                end
        end