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 0cbb7611cb
commit 9c56868a06
7 changed files with 30 additions and 37 deletions

@ -731,3 +731,12 @@ function mcl_util.get_entity_id(entity)
return id
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

@ -434,14 +434,12 @@ function mcl_mobs.register_arrow(name, def)
minetest.add_item(self.lastpos, self.object:get_luaentity().name)
end
self._removed = true
self.object:remove();
mcl_util.remove_entity(self)
end,
on_collide_with_entity = function(self, pos, object)
if self.hit_player and object:is_player() then
self.hit_player(self, object)
self._removed = true
self.object:remove()
mcl_util.remove_entity(self)
return
end
@ -451,13 +449,11 @@ function mcl_mobs.register_arrow(name, def)
if self.hit_mob and entity.is_mob == true then
self.hit_mob(self, object)
self._removed = true
self.object:remove()
mcl_util.remove_entity(self)
return
elseif self.hit_object then
self.hit_object(self, object)
self._removed = true
self.object:remove()
mcl_util.remove_entity(self)
return
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
mcl_burning.extinguish(self.object)
self._removed = true
self.object:remove();
mcl_util.remove_entity(self)
return
end

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

@ -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
-- has collided with an entity
self._removed = true
self.object:remove()
mcl_util.remove_entity(self)
end
},
@ -186,8 +185,7 @@ local arrow_entity = {
end
if data.stuckin_player then
self._removed = true
self.object:remove()
mcl_util.remove_entity(self)
end
end,
}
@ -201,7 +199,7 @@ minetest.register_on_respawnplayer(function(player)
for _, obj in pairs(player:get_children()) do
local ent = obj:get_luaentity()
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)

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

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

@ -82,8 +82,7 @@ function mod.update_projectile(self, dtime)
self.timer = (self.timer or 0) + dtime
local maximum_flight_time = entity_vl_projectile.maximum_time or 300
if (self.timer or 0) > maximum_flight_time then
self.removed = true
self.object:remove()
mcl_util.remove_entity(self)
return
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 not projectile_def.sticks_in_players then return end
minetest.after(150, function()
self._removed = true
self.object:remove()
end)
minetest.after(150, function() mcl_util.remove_entity(self) end)
-- Handle blocking projectiles
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
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)
item:set_velocity(vector.zero())
item:set_yaw(self.object:get_yaw())
end
mcl_burning.extinguish(self.object)
self._removed = true
self.object:remove()
mcl_util.remove_entity(self)
end
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
if self._stucktimer > STUCK_TIMEOUT then
mcl_burning.extinguish(self.object)
self._removed = true
self.object:remove()
mcl_util.remove_entity(self)
return true
end
@ -314,7 +308,7 @@ local function stuck_on_step(self, dtime, entity_def, projectile_def)
end
end
mcl_burning.extinguish(self.object)
self.object:remove()
mcl_util.remove_entity(self)
return
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)
end
if not survive_collision then
self._removed = true
self.object:remove()
mcl_util.remove_entity(self)
end
-- 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)
end
if not survive_collision then
self._removed = true
self.object:remove()
minetest.log("removing projectile that collided with entity")
mcl_util.remove_entity(self)
end
return true