Effects persist on loads for mobs too

This commit is contained in:
the-real-herowl 2024-04-22 02:12:54 +02:00
parent a4eaaad1a9
commit 914e3c6c2a
3 changed files with 43 additions and 8 deletions

View File

@ -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

View File

@ -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

View File

@ -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,11 +1513,30 @@ 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
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
-- Returns true if object has given effect