Implement mcl_util.remove_entity() and convert projectile code to use it

This commit is contained in:
teknomunk 2024-11-15 18:19:41 -06:00
parent 27f0f8b52b
commit 2fec9696b6
7 changed files with 30 additions and 37 deletions

View File

@ -731,3 +731,12 @@ function mcl_util.get_entity_id(entity)
return id return id
end end
end end
function mcl_util.remove_entity(luaentity)
if luaentity._removed then return end
luaentity._removed = true
local hook = luaentity._on_remove
if hook then hook(luaentity) end
luaentity.object:remove()
end

View File

@ -434,14 +434,12 @@ function mcl_mobs.register_arrow(name, def)
minetest.add_item(self.lastpos, self.object:get_luaentity().name) minetest.add_item(self.lastpos, self.object:get_luaentity().name)
end end
self._removed = true mcl_util.remove_entity(self)
self.object:remove();
end, end,
on_collide_with_entity = function(self, pos, object) on_collide_with_entity = function(self, pos, object)
if self.hit_player and object:is_player() then if self.hit_player and object:is_player() then
self.hit_player(self, object) self.hit_player(self, object)
self._removed = true mcl_util.remove_entity(self)
self.object:remove()
return return
end end
@ -451,13 +449,11 @@ function mcl_mobs.register_arrow(name, def)
if self.hit_mob and entity.is_mob == true then if self.hit_mob and entity.is_mob == true then
self.hit_mob(self, object) self.hit_mob(self, object)
self._removed = true mcl_util.remove_entity(self)
self.object:remove()
return return
elseif self.hit_object then elseif self.hit_object then
self.hit_object(self, object) self.hit_object(self, object)
self._removed = true mcl_util.remove_entity(self)
self.object:remove()
return return
end end
end end
@ -481,8 +477,7 @@ function mcl_mobs.register_arrow(name, def)
if self.switch == 0 or self.timer > self._lifetime or not within_limits(pos, 0) then if self.switch == 0 or self.timer > self._lifetime or not within_limits(pos, 0) then
mcl_burning.extinguish(self.object) mcl_burning.extinguish(self.object)
self._removed = true mcl_util.remove_entity(self)
self.object:remove();
return return
end end

View File

@ -201,8 +201,7 @@ mcl_mobs.register_mob("mobs_mc:wither", {
self._death_timer = self._death_timer + self.health - self._health_old self._death_timer = self._death_timer + self.health - self._health_old
if self.health == self._health_old then self._death_timer = self._death_timer + dtime end if self.health == self._health_old then self._death_timer = self._death_timer + dtime end
if self._death_timer > 100 then if self._death_timer > 100 then
self._removed = true mcl_util.remove_entity(self)
self.object:remove()
return false return false
end end
self._health_old = self.health self._health_old = self.health

View File

@ -138,8 +138,7 @@ local arrow_entity = {
-- Because arrows are flagged to survive collisions to allow sticking into blocks, manually remove it now that it -- Because arrows are flagged to survive collisions to allow sticking into blocks, manually remove it now that it
-- has collided with an entity -- has collided with an entity
self._removed = true mcl_util.remove_entity(self)
self.object:remove()
end end
}, },
@ -186,8 +185,7 @@ local arrow_entity = {
end end
if data.stuckin_player then if data.stuckin_player then
self._removed = true mcl_util.remove_entity(self)
self.object:remove()
end end
end, end,
} }
@ -201,7 +199,7 @@ minetest.register_on_respawnplayer(function(player)
for _, obj in pairs(player:get_children()) do for _, obj in pairs(player:get_children()) do
local ent = obj:get_luaentity() local ent = obj:get_luaentity()
if ent and ent.name and string.find(ent.name, "mcl_bows:arrow_entity") then if ent and ent.name and string.find(ent.name, "mcl_bows:arrow_entity") then
obj:remove() mcl_util.remove_entity(self)
end end
end end
end) end)

View File

@ -241,7 +241,7 @@ minetest.register_craftitem("mcl_bows:rocket", {
local eploded_particle = particle_explosion(pos) local eploded_particle = particle_explosion(pos)
damage_explosion(self, eploded_particle * 17, pos) damage_explosion(self, eploded_particle * 17, pos)
mcl_burning.extinguish(self.object) mcl_burning.extinguish(self.object)
self.object:remove() mcl_util.remove_entity(self)
end end
end, end,
}) })
@ -268,7 +268,7 @@ rocket_entity.on_step = function(self, dtime)
local eploded_particle = particle_explosion(self) local eploded_particle = particle_explosion(self)
damage_explosion(self, eploded_particle * 17) damage_explosion(self, eploded_particle * 17)
mcl_burning.extinguish(self.object) mcl_burning.extinguish(self.object)
self.object:remove() mcl_util.remove_entity(self)
return return
end end

View File

@ -331,8 +331,7 @@ vl_projectile.register("mcl_fishing:flying_bobber_entity", {
on_collide_with_solid = function(self, pos, node) on_collide_with_solid = function(self, pos, node)
local player = self._owner local player = self._owner
self._remove = true mcl_util.remove_entity(self)
self.object:remove()
-- Make sure the player field is valid for when we create the floating bobber -- Make sure the player field is valid for when we create the floating bobber
if not player then return end if not player then return end

View File

@ -82,8 +82,7 @@ function mod.update_projectile(self, dtime)
self.timer = (self.timer or 0) + dtime self.timer = (self.timer or 0) + dtime
local maximum_flight_time = entity_vl_projectile.maximum_time or 300 local maximum_flight_time = entity_vl_projectile.maximum_time or 300
if (self.timer or 0) > maximum_flight_time then if (self.timer or 0) > maximum_flight_time then
self.removed = true mcl_util.remove_entity(self)
self.object:remove()
return return
end end
@ -155,10 +154,7 @@ local function handle_player_sticking(self, entity_def, projectile_def, entity)
if self._in_player or self._blocked then return end if self._in_player or self._blocked then return end
if not projectile_def.sticks_in_players then return end if not projectile_def.sticks_in_players then return end
minetest.after(150, function() minetest.after(150, function() mcl_util.remove_entity(self) end)
self._removed = true
self.object:remove()
end)
-- Handle blocking projectiles -- Handle blocking projectiles
if mcl_shields.is_blocking(entity) then if mcl_shields.is_blocking(entity) then
@ -256,15 +252,14 @@ function mod.replace_with_item_drop(self, pos, projectile_def)
item = projectile_def.item item = projectile_def.item
end end
if self._collectable and not minetest.is_creative_enabled("") then if item and self._collectable and not minetest.is_creative_enabled("") then
local item = minetest.add_item(pos, item) local item = minetest.add_item(pos, item)
item:set_velocity(vector.zero()) item:set_velocity(vector.zero())
item:set_yaw(self.object:get_yaw()) item:set_yaw(self.object:get_yaw())
end end
mcl_burning.extinguish(self.object) mcl_burning.extinguish(self.object)
self._removed = true mcl_util.remove_entity(self)
self.object:remove()
end end
local function stuck_on_step(self, dtime, entity_def, projectile_def) local function stuck_on_step(self, dtime, entity_def, projectile_def)
@ -275,8 +270,7 @@ local function stuck_on_step(self, dtime, entity_def, projectile_def)
self._stucktimer = (self._stucktimer or 0) + dtime self._stucktimer = (self._stucktimer or 0) + dtime
if self._stucktimer > STUCK_TIMEOUT then if self._stucktimer > STUCK_TIMEOUT then
mcl_burning.extinguish(self.object) mcl_burning.extinguish(self.object)
self._removed = true mcl_util.remove_entity(self)
self.object:remove()
return true return true
end end
@ -314,7 +308,7 @@ local function stuck_on_step(self, dtime, entity_def, projectile_def)
end end
end end
mcl_burning.extinguish(self.object) mcl_burning.extinguish(self.object)
self.object:remove() mcl_util.remove_entity(self)
return return
end end
@ -441,8 +435,7 @@ function mod.collides_with_solids(self, dtime, entity_def, projectile_def)
survive_collision = survive_collision(self, entity_def, projectile_def, "node", node, node_def) survive_collision = survive_collision(self, entity_def, projectile_def, "node", node, node_def)
end end
if not survive_collision then if not survive_collision then
self._removed = true mcl_util.remove_entity(self)
self.object:remove()
end end
-- Done with behaviors -- Done with behaviors
@ -532,8 +525,8 @@ local function handle_entity_collision(self, entity_def, projectile_def, object)
survive_collision = survive_collision(self, entity_def, projectile_def, "entity", object) survive_collision = survive_collision(self, entity_def, projectile_def, "entity", object)
end end
if not survive_collision then if not survive_collision then
self._removed = true minetest.log("removing projectile that collided with entity")
self.object:remove() mcl_util.remove_entity(self)
end end
return true return true