From bc343769ee8d3753c5af6b63ec81930f3f9eccf4 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Sun, 9 Jun 2024 08:38:47 -0500 Subject: [PATCH 01/13] Add guard to prevent crash when converting old mobs and the minetest fails to create the new entity --- mods/ENTITIES/mcl_mobs/init.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/init.lua b/mods/ENTITIES/mcl_mobs/init.lua index 44eae7b13..752eb7b4b 100644 --- a/mods/ENTITIES/mcl_mobs/init.lua +++ b/mods/ENTITIES/mcl_mobs/init.lua @@ -346,8 +346,10 @@ function mcl_mobs.register_conversion(old_name, new_name) minetest.register_entity(old_name, { on_activate = function(self, staticdata, dtime) local obj = minetest.add_entity(self.object:get_pos(), new_name, staticdata) - local hook = (obj:get_luaentity() or {})._on_after_convert - if hook then hook(obj) end + if obj then + local hook = (obj:get_luaentity() or {})._on_after_convert + if hook then hook(obj) end + end self.object:remove() end, _convert_to = new_name, From a88951ac6af3f8caaa82539bf47805b5ec5360bb Mon Sep 17 00:00:00 2001 From: teknomunk Date: Sun, 9 Jun 2024 14:11:04 -0500 Subject: [PATCH 02/13] More safety checks --- mods/ENTITIES/mcl_mobs/init.lua | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/init.lua b/mods/ENTITIES/mcl_mobs/init.lua index 752eb7b4b..4183ce42e 100644 --- a/mods/ENTITIES/mcl_mobs/init.lua +++ b/mods/ENTITIES/mcl_mobs/init.lua @@ -345,12 +345,19 @@ end -- END mcl_mobs.register_mob function function mcl_mobs.register_conversion(old_name, new_name) minetest.register_entity(old_name, { on_activate = function(self, staticdata, dtime) - local obj = minetest.add_entity(self.object:get_pos(), new_name, staticdata) - if obj then - local hook = (obj:get_luaentity() or {})._on_after_convert - if hook then hook(obj) end - end - self.object:remove() + local old_object = self.object + if not old_object then return end + + local pos = old_object:get_pos() + old_object:remove() -- Always remove the object if we got this far + + if not pos then return end + + local new_object = minetest.add_entity(pos, new_name, staticdata) + if not new_object then return end + + local hook = (obj:get_luaentity() or {})._on_after_convert + if hook then hook(obj) end end, _convert_to = new_name, }) From 9b50dd6565e7fc9a81dc2c01f6e271e78f4208b9 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Sun, 9 Jun 2024 14:13:36 -0500 Subject: [PATCH 03/13] Update to use new_object instead of obj --- mods/ENTITIES/mcl_mobs/init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/init.lua b/mods/ENTITIES/mcl_mobs/init.lua index 4183ce42e..7443fe553 100644 --- a/mods/ENTITIES/mcl_mobs/init.lua +++ b/mods/ENTITIES/mcl_mobs/init.lua @@ -356,8 +356,8 @@ function mcl_mobs.register_conversion(old_name, new_name) local new_object = minetest.add_entity(pos, new_name, staticdata) if not new_object then return end - local hook = (obj:get_luaentity() or {})._on_after_convert - if hook then hook(obj) end + local hook = (new_object:get_luaentity() or {})._on_after_convert + if hook then hook(new_object) end end, _convert_to = new_name, }) From 1942384fe513c7515cdd480cffdcb95ab3d3fc7d Mon Sep 17 00:00:00 2001 From: teknomunk Date: Sun, 9 Jun 2024 14:28:44 -0500 Subject: [PATCH 04/13] Move object remove after position check --- mods/ENTITIES/mcl_mobs/init.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/init.lua b/mods/ENTITIES/mcl_mobs/init.lua index 7443fe553..9462e03b3 100644 --- a/mods/ENTITIES/mcl_mobs/init.lua +++ b/mods/ENTITIES/mcl_mobs/init.lua @@ -349,9 +349,8 @@ function mcl_mobs.register_conversion(old_name, new_name) if not old_object then return end local pos = old_object:get_pos() - old_object:remove() -- Always remove the object if we got this far - if not pos then return end + old_object:remove() local new_object = minetest.add_entity(pos, new_name, staticdata) if not new_object then return end From d64ee18f756755d9429d8a635c473205e1b9a119 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Wed, 12 Jun 2024 06:26:07 -0500 Subject: [PATCH 05/13] Strip some fields from the mob's staticdata on conversion --- mods/ENTITIES/mcl_mobs/init.lua | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mods/ENTITIES/mcl_mobs/init.lua b/mods/ENTITIES/mcl_mobs/init.lua index 9462e03b3..ec38307eb 100644 --- a/mods/ENTITIES/mcl_mobs/init.lua +++ b/mods/ENTITIES/mcl_mobs/init.lua @@ -342,9 +342,18 @@ function mcl_mobs.register_mob(name, def) minetest.register_entity(name, setmetatable(final_def,mcl_mobs.mob_class_meta)) end -- END mcl_mobs.register_mob function + +local STRIP_FIELDS = { "mesh", "base_size", "textures" } function mcl_mobs.register_conversion(old_name, new_name) minetest.register_entity(old_name, { on_activate = function(self, staticdata, dtime) + -- Strip select fields from the staticdata to prevent conversion issues + local unpacked_staticdata = minetest.deserialize(staticdata) + for i = 1,#STRIP_FIELDS do + unpacked_staticdata[STRIP_FIELDS[i]] = nil + end + staticdata = minetest.serialize(staticdata) + local old_object = self.object if not old_object then return end From 41032ec999823bc0a6e715dd7908fa23f27f7a4b Mon Sep 17 00:00:00 2001 From: teknomunk Date: Wed, 12 Jun 2024 06:31:06 -0500 Subject: [PATCH 06/13] Use correct variable name --- mods/ENTITIES/mcl_mobs/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mods/ENTITIES/mcl_mobs/init.lua b/mods/ENTITIES/mcl_mobs/init.lua index ec38307eb..834ae426a 100644 --- a/mods/ENTITIES/mcl_mobs/init.lua +++ b/mods/ENTITIES/mcl_mobs/init.lua @@ -352,7 +352,7 @@ function mcl_mobs.register_conversion(old_name, new_name) for i = 1,#STRIP_FIELDS do unpacked_staticdata[STRIP_FIELDS[i]] = nil end - staticdata = minetest.serialize(staticdata) + staticdata = minetest.serialize(unpacked_staticdata) local old_object = self.object if not old_object then return end From 4935f5fdda2bf2c0870ab7b88f315bc10c065571 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Wed, 12 Jun 2024 06:33:19 -0500 Subject: [PATCH 07/13] Add debug logging --- mods/ENTITIES/mcl_mobs/init.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/mods/ENTITIES/mcl_mobs/init.lua b/mods/ENTITIES/mcl_mobs/init.lua index 834ae426a..083006fae 100644 --- a/mods/ENTITIES/mcl_mobs/init.lua +++ b/mods/ENTITIES/mcl_mobs/init.lua @@ -352,6 +352,7 @@ function mcl_mobs.register_conversion(old_name, new_name) for i = 1,#STRIP_FIELDS do unpacked_staticdata[STRIP_FIELDS[i]] = nil end + minetest.log("action", "Converting "..old_name.." to "..new_name.." staticdata="..dump(unpacked_staticdata)) staticdata = minetest.serialize(unpacked_staticdata) local old_object = self.object From 15fa925aae2d06c65cebf5f1439d7cd92ac1fe58 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Wed, 12 Jun 2024 06:36:59 -0500 Subject: [PATCH 08/13] More fields to strip --- mods/ENTITIES/mcl_mobs/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mods/ENTITIES/mcl_mobs/init.lua b/mods/ENTITIES/mcl_mobs/init.lua index 083006fae..8d1d624c9 100644 --- a/mods/ENTITIES/mcl_mobs/init.lua +++ b/mods/ENTITIES/mcl_mobs/init.lua @@ -343,7 +343,7 @@ function mcl_mobs.register_mob(name, def) end -- END mcl_mobs.register_mob function -local STRIP_FIELDS = { "mesh", "base_size", "textures" } +local STRIP_FIELDS = { "mesh", "base_size", "textures", "base_mesh", "base_texture" } function mcl_mobs.register_conversion(old_name, new_name) minetest.register_entity(old_name, { on_activate = function(self, staticdata, dtime) From 7e0afd7e21b15fa3b2665f3e13f37905316466cd Mon Sep 17 00:00:00 2001 From: teknomunk Date: Wed, 12 Jun 2024 06:42:37 -0500 Subject: [PATCH 09/13] Remove debug logging --- mods/ENTITIES/mcl_mobs/init.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/mods/ENTITIES/mcl_mobs/init.lua b/mods/ENTITIES/mcl_mobs/init.lua index 8d1d624c9..dcd1bed4d 100644 --- a/mods/ENTITIES/mcl_mobs/init.lua +++ b/mods/ENTITIES/mcl_mobs/init.lua @@ -352,7 +352,6 @@ function mcl_mobs.register_conversion(old_name, new_name) for i = 1,#STRIP_FIELDS do unpacked_staticdata[STRIP_FIELDS[i]] = nil end - minetest.log("action", "Converting "..old_name.." to "..new_name.." staticdata="..dump(unpacked_staticdata)) staticdata = minetest.serialize(unpacked_staticdata) local old_object = self.object From 75a767a0abd2e83719ca048af8c75ac499c0c59f Mon Sep 17 00:00:00 2001 From: teknomunk Date: Fri, 14 Jun 2024 18:43:10 -0500 Subject: [PATCH 10/13] Mob spawner conversion --- mods/ITEMS/mcl_mobspawners/init.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mods/ITEMS/mcl_mobspawners/init.lua b/mods/ITEMS/mcl_mobspawners/init.lua index f32558629..b500872ec 100644 --- a/mods/ITEMS/mcl_mobspawners/init.lua +++ b/mods/ITEMS/mcl_mobspawners/init.lua @@ -83,6 +83,13 @@ local function respawn_doll(pos) local mob = meta:get_string("Mob") local doll if mob and mob ~= "" then + -- Handle conversion of mob spawners + local convert_to = (minetest.registered_entities[mob] or {})._convert_to + if convert_to then + mob = convert_to + meta:set_string("Mob", mob) + end + doll = find_doll(pos) if not doll then doll = spawn_doll(pos) @@ -367,7 +374,6 @@ doll_def.on_activate = function(self, staticdata, dtime_s) self.object:set_velocity({x=0, y=0, z=0}) self.object:set_acceleration({x=0, y=0, z=0}) self.object:set_armor_groups({immortal=1}) - end doll_def.on_step = function(self, dtime) From 0a2336ad82055bcd36f7809e05e2b611adce8ff8 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Fri, 14 Jun 2024 18:50:05 -0500 Subject: [PATCH 11/13] Handle conversion of mobs that were incorrectly converted --- mods/ENTITIES/mcl_mobs/api.lua | 5 +++++ mods/ENTITIES/mcl_mobs/init.lua | 11 +++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index 873d05bf2..8064c5511 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -149,6 +149,11 @@ function mob_class:mob_activate(staticdata, def, dtime) local tmp = minetest.deserialize(staticdata) + -- Patch incorrectly converted mobs + if tmp.base_mesh ~= minetest.registered_entities[self.name].mesh then + mcl_mobs.strip_staticdata(tmp) + end + if tmp then for _,stat in pairs(tmp) do self[_] = stat diff --git a/mods/ENTITIES/mcl_mobs/init.lua b/mods/ENTITIES/mcl_mobs/init.lua index dcd1bed4d..f9828c7d0 100644 --- a/mods/ENTITIES/mcl_mobs/init.lua +++ b/mods/ENTITIES/mcl_mobs/init.lua @@ -344,14 +344,17 @@ end -- END mcl_mobs.register_mob function local STRIP_FIELDS = { "mesh", "base_size", "textures", "base_mesh", "base_texture" } +function mcl_mobs.strip_staticdata(staticdata) + -- Strip select fields from the staticdata to prevent conversion issues + for i = 1,#STRIP_FIELDS do + unpacked_staticdata[STRIP_FIELDS[i]] = nil + end +end function mcl_mobs.register_conversion(old_name, new_name) minetest.register_entity(old_name, { on_activate = function(self, staticdata, dtime) - -- Strip select fields from the staticdata to prevent conversion issues local unpacked_staticdata = minetest.deserialize(staticdata) - for i = 1,#STRIP_FIELDS do - unpacked_staticdata[STRIP_FIELDS[i]] = nil - end + mcl_mobs.strip_staticdata(unpacked_staticdata) staticdata = minetest.serialize(unpacked_staticdata) local old_object = self.object From 304550d90cc79ba202c6aa78f561c431a0b10203 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Fri, 14 Jun 2024 18:53:03 -0500 Subject: [PATCH 12/13] Fix parameter name --- mods/ENTITIES/mcl_mobs/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mods/ENTITIES/mcl_mobs/init.lua b/mods/ENTITIES/mcl_mobs/init.lua index f9828c7d0..9518e9658 100644 --- a/mods/ENTITIES/mcl_mobs/init.lua +++ b/mods/ENTITIES/mcl_mobs/init.lua @@ -344,7 +344,7 @@ end -- END mcl_mobs.register_mob function local STRIP_FIELDS = { "mesh", "base_size", "textures", "base_mesh", "base_texture" } -function mcl_mobs.strip_staticdata(staticdata) +function mcl_mobs.strip_staticdata(unpacked_staticdata) -- Strip select fields from the staticdata to prevent conversion issues for i = 1,#STRIP_FIELDS do unpacked_staticdata[STRIP_FIELDS[i]] = nil From 3928e12634a7832e8f9b2112432704bdf23a0b58 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Fri, 14 Jun 2024 19:49:45 -0500 Subject: [PATCH 13/13] Fix two more crashes, stray space --- mods/ENTITIES/mcl_mobs/api.lua | 10 +++++----- mods/ENTITIES/mcl_mobs/init.lua | 7 ++++++- mods/ITEMS/mcl_mobspawners/init.lua | 2 -- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index 8064c5511..1e49e8b4f 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -149,12 +149,12 @@ function mob_class:mob_activate(staticdata, def, dtime) local tmp = minetest.deserialize(staticdata) - -- Patch incorrectly converted mobs - if tmp.base_mesh ~= minetest.registered_entities[self.name].mesh then - mcl_mobs.strip_staticdata(tmp) - end - if tmp then + -- Patch incorrectly converted mobs + if tmp.base_mesh ~= minetest.registered_entities[self.name].mesh then + mcl_mobs.strip_staticdata(tmp) + end + for _,stat in pairs(tmp) do self[_] = stat end diff --git a/mods/ENTITIES/mcl_mobs/init.lua b/mods/ENTITIES/mcl_mobs/init.lua index 9518e9658..81b5fdfe7 100644 --- a/mods/ENTITIES/mcl_mobs/init.lua +++ b/mods/ENTITIES/mcl_mobs/init.lua @@ -592,7 +592,12 @@ function mcl_mobs.register_egg(mob, desc, background_color, overlay_color, addeg --minetest.log("min light: " .. mob_light_lvl[1]) --minetest.log("max light: " .. mob_light_lvl[2]) - mcl_mobspawners.setup_spawner(pointed_thing.under, itemstack:get_name(), mob_light_lvl[1], mob_light_lvl[2]) + -- Handle egg conversion + local mob_name = itemstack:get_name() + local convert_to = (minetest.registered_entities[mob_name] or {})._convert_to + if convert_to then mob_name = convert_to end + + mcl_mobspawners.setup_spawner(pointed_thing.under, mob_name, mob_light_lvl[1], mob_light_lvl[2]) if not minetest.is_creative_enabled(name) then itemstack:take_item() end diff --git a/mods/ITEMS/mcl_mobspawners/init.lua b/mods/ITEMS/mcl_mobspawners/init.lua index b500872ec..83c03804e 100644 --- a/mods/ITEMS/mcl_mobspawners/init.lua +++ b/mods/ITEMS/mcl_mobspawners/init.lua @@ -135,7 +135,6 @@ function mcl_mobspawners.setup_spawner(pos, Mob, MinLight, MaxLight, MaxMobsInAr end set_doll_properties(doll, Mob) - -- Start spawning very soon local t = minetest.get_node_timer(pos) t:start(2) @@ -172,7 +171,6 @@ local function spawn_mobs(pos, elapsed) local count = 0 local ent - local timer = minetest.get_node_timer(pos) -- spawn mob if player detected and in range