From 914e3c6c2a79c643370640ccca0381f0046677e5 Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Mon, 22 Apr 2024 02:12:54 +0200 Subject: [PATCH] Effects persist on loads for mobs too --- mods/ENTITIES/mcl_mobs/api.lua | 13 +++++++--- mods/ENTITIES/mcl_mobs/init.lua | 1 + mods/ITEMS/mcl_potions/functions.lua | 37 +++++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index 8ebdddcac..37db6b46e 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -96,18 +96,20 @@ function mob_class:get_staticdata() local tmp = {} - for _,stat in pairs(self) do + for tag, stat in pairs(self) do local t = type(stat) if t ~= "function" and t ~= "nil" and t ~= "userdata" - and _ ~= "_cmi_components" then - tmp[_] = self[_] + and tag ~= "_cmi_components" then + tmp[tag] = self[tag] end end + tmp._mcl_potions = self._mcl_potions + return minetest.serialize(tmp) end @@ -306,7 +308,10 @@ function mob_class:mob_activate(staticdata, def, dtime) self._run_armor_init = true end - + if not self._mcl_potions then + self._mcl_potions = {} + end + mcl_potions._load_entity_effects(self) if def.after_activate then diff --git a/mods/ENTITIES/mcl_mobs/init.lua b/mods/ENTITIES/mcl_mobs/init.lua index 2b18a6775..3a2e18872 100644 --- a/mods/ENTITIES/mcl_mobs/init.lua +++ b/mods/ENTITIES/mcl_mobs/init.lua @@ -325,6 +325,7 @@ function mcl_mobs.register_mob(name, def) attack_exception = def.attack_exception or function(p) return false end, _spawner = def._spawner, + _mcl_potions = {}, } if minetest.get_modpath("doc_identifier") ~= nil then diff --git a/mods/ITEMS/mcl_potions/functions.lua b/mods/ITEMS/mcl_potions/functions.lua index fa524996b..17323ad7c 100644 --- a/mods/ITEMS/mcl_potions/functions.lua +++ b/mods/ITEMS/mcl_potions/functions.lua @@ -1339,8 +1339,13 @@ minetest.register_globalstep(function(dtime) if effect.after_end then effect.after_end(object) end if object:is_player() then meta = object:get_meta() - meta:set_string("mcl_potions:"..name, minetest.serialize(EF[name][object])) + meta:set_string("mcl_potions:_EF_"..name, "") potions_set_hud(object) + else + local ent = object:get_luaentity() + if ent then + ent._mcl_potions["_EF_"..name] = nil + end end elseif object:is_player() then if vals.dur == math.huge then @@ -1351,6 +1356,11 @@ minetest.register_globalstep(function(dtime) object:hud_change(icon_ids[object:get_player_name()][vals.hud_index].timestamp, "text", math.floor(dur/60)..string.format(":%02d",math.floor(dur % 60))) end + else + local ent = object:get_luaentity() + if ent then + ent._mcl_potions["_EF_"..name] = EF[name][object] + end end end end @@ -1503,9 +1513,28 @@ function mcl_potions._load_player_effects(player) -- new API effects + on_load for loaded legacy effects for name, effect in pairs(registered_effects) do local loaded = minetest.deserialize(meta:get_string("mcl_potions:_EF_"..name)) - if loaded then EF[name][player] = loaded end - if EF[name][player] and effect.on_load then - effect.on_load(player, EF[name][player].factor) + if loaded then + EF[name][player] = loaded + if effect.on_load then + effect.on_load(player, EF[name][player].factor) + end + end + end +end + +function mcl_potions._load_entity_effects(entity) + if not entity or not entity._mcl_potions or entity._mcl_potions == {} then + return + end + local object = entity.object + if not object or not object:get_pos() then return end + for name, effect in pairs(registered_effects) do + local loaded = entity._mcl_potions["_EF_"..name] + if loaded then + EF[name][object] = loaded + if effect.on_load then + effect.on_load(object, EF[name][object].factor) + end end end end