Added the option to remove and clear effects...

...with the /effect command.
Also made the API for clearing effects more robust.
This commit is contained in:
the-real-herowl 2024-04-14 04:27:09 +02:00
parent dcbc9d2398
commit 75e5ca8d49
2 changed files with 31 additions and 3 deletions

View file

@ -9,8 +9,8 @@ local S = minetest.get_translator(minetest.get_current_modname())
minetest.register_chatcommand("effect",{ minetest.register_chatcommand("effect",{
params = S("<effect>|heal|list <duration|heal-amount> [<level>] [<factor>] [NOPART]"), params = S("<effect>|heal|list|clear|remove <duration|heal-amount|effect> [<level>] [<factor>] [NOPART]"),
description = S("Add a status effect to yourself. Arguments: <effect>: name of status effect. Passing \"list\" as effect name lists available effects. Passing \"heal\" as effect name heals (or harms) by amount designed by the next parameter. <duration>: duration in seconds. (<heal-amount>: amount of healing when the effect is \"heal\", passing a negative value subtracts health.) <level>: effect power determinant, bigger level results in more powerful effect for effects that depend on the level (no changes for other effects), defaults to 1, pass F to use low-level factor instead. <factor>: effect strength modifier, can mean different things depending on the effect, no changes for effects that do not depend on level/factor. NOPART at the end means no particles will be shown for this effect."), description = S("Add a status effect to yourself. Arguments: <effect>: name of status effect. Passing \"list\" as effect name lists available effects. Passing \"heal\" as effect name heals (or harms) by amount designed by the next parameter. Passing \"clear\" as effect name removes all effects. Passing \"remove\" as effect name removes the effect named by the next parameter. <duration>: duration in seconds. (<heal-amount>: amount of healing when the effect is \"heal\", passing a negative value subtracts health. <effect>: name of a status effect to be removed when using \"remove\" as the previous parameter.) <level>: effect power determinant, bigger level results in more powerful effect for effects that depend on the level (no changes for other effects), defaults to 1, pass F to use low-level factor instead. <factor>: effect strength modifier, can mean different things depending on the effect, no changes for effects that do not depend on level/factor. NOPART at the end means no particles will be shown for this effect."),
privs = {server = true}, privs = {server = true},
func = function(name, params) func = function(name, params)
@ -43,6 +43,19 @@ minetest.register_chatcommand("effect",{
return true, S("Player @1 harmed by @2 HP.", name, hp) return true, S("Player @1 harmed by @2 HP.", name, hp)
end end
end end
elseif P[1] == "clear" then
mcl_potions._reset_player_effects(minetest.get_player_by_name(name))
return true, S("Effects cleared for player @1", name)
elseif P[1] == "remove" then
if not P[2] then
return false, S("Missing effect parameter!")
end
if mcl_potions.registered_effects[P[2]] then
mcl_potions.clear_effect(minetest.get_player_by_name(name), P[2])
return true, S("Removed effect @1 from player @2", P[2], name)
else
return false, S("@1 is not an available status effect.", P[2])
end
elseif not tonumber(P[2]) then elseif not tonumber(P[2]) then
return false, S("Missing or invalid duration parameter!") return false, S("Missing or invalid duration parameter!")
elseif P[3] and not tonumber(P[3]) and P[3] ~= "F" and P[3] ~= "NOPART" then elseif P[3] and not tonumber(P[3]) and P[3] ~= "F" and P[3] ~= "NOPART" then

View file

@ -1385,12 +1385,18 @@ function mcl_potions._reset_player_effects(player, set_hud)
return return
end end
local removed_effects = {}
for name, effect in pairs(registered_effects) do for name, effect in pairs(registered_effects) do
if EF[name][player] and effect.on_end then effect.on_end(player) end if EF[name][player] and effect.on_end then effect.on_end(player) end
if effect.after_end then table.insert(removed_effects, effect.after_end) end
end end
mcl_potions._clear_cached_player_data(player) mcl_potions._clear_cached_player_data(player)
for i=1, #removed_effects do
removed_effects[i](player)
end
if set_hud ~= false then if set_hud ~= false then
potions_set_hud(player) potions_set_hud(player)
end end
@ -1535,7 +1541,16 @@ function mcl_potions.get_total_fatigue(object)
end end
function mcl_potions.clear_effect(object, effect) function mcl_potions.clear_effect(object, effect)
if not EF[effect] then
minetest.log("warning", "[mcl_potions] Tried to remove an effect that is not registered: " .. dump(effect))
return false
end
local def = registered_effects[effect]
if EF[effect][object] then
if def.on_end then def.on_end(object) end
EF[effect][object] = nil EF[effect][object] = nil
if def.after_end then def.after_end(object) end
end
if not object:is_player() then return end if not object:is_player() then return end
potions_set_hud(object) potions_set_hud(object)
end end