From 2fec9696b661512e79ae12bd26302d0922082c02 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Fri, 15 Nov 2024 18:19:41 -0600 Subject: [PATCH] Implement mcl_util.remove_entity() and convert projectile code to use it --- mods/CORE/mcl_util/init.lua | 9 +++++++++ mods/ENTITIES/mcl_mobs/init.lua | 15 +++++---------- mods/ENTITIES/mobs_mc/wither.lua | 3 +-- mods/ITEMS/mcl_bows/arrow.lua | 8 +++----- mods/ITEMS/mcl_bows/rocket.lua | 4 ++-- mods/ITEMS/mcl_fishing/init.lua | 3 +-- mods/ITEMS/vl_projectile/init.lua | 25 +++++++++---------------- 7 files changed, 30 insertions(+), 37 deletions(-) diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index fb4119605..fbb5c2669 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -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 diff --git a/mods/ENTITIES/mcl_mobs/init.lua b/mods/ENTITIES/mcl_mobs/init.lua index bc3dfc7a6..4c84ab2ad 100644 --- a/mods/ENTITIES/mcl_mobs/init.lua +++ b/mods/ENTITIES/mcl_mobs/init.lua @@ -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 diff --git a/mods/ENTITIES/mobs_mc/wither.lua b/mods/ENTITIES/mobs_mc/wither.lua index 53373f96f..3a1253e06 100644 --- a/mods/ENTITIES/mobs_mc/wither.lua +++ b/mods/ENTITIES/mobs_mc/wither.lua @@ -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 diff --git a/mods/ITEMS/mcl_bows/arrow.lua b/mods/ITEMS/mcl_bows/arrow.lua index dc48062d8..33c3c542c 100644 --- a/mods/ITEMS/mcl_bows/arrow.lua +++ b/mods/ITEMS/mcl_bows/arrow.lua @@ -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) diff --git a/mods/ITEMS/mcl_bows/rocket.lua b/mods/ITEMS/mcl_bows/rocket.lua index 31e5affbc..47d99f8f9 100644 --- a/mods/ITEMS/mcl_bows/rocket.lua +++ b/mods/ITEMS/mcl_bows/rocket.lua @@ -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 diff --git a/mods/ITEMS/mcl_fishing/init.lua b/mods/ITEMS/mcl_fishing/init.lua index b1309a102..66cfb1607 100644 --- a/mods/ITEMS/mcl_fishing/init.lua +++ b/mods/ITEMS/mcl_fishing/init.lua @@ -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 diff --git a/mods/ITEMS/vl_projectile/init.lua b/mods/ITEMS/vl_projectile/init.lua index 200141294..a392a31ab 100644 --- a/mods/ITEMS/vl_projectile/init.lua +++ b/mods/ITEMS/vl_projectile/init.lua @@ -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