From 0e76781b9bdc22c1ff5b8bbdb0f6634c778772cc Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Sun, 14 Apr 2024 04:27:09 +0200 Subject: [PATCH] Added the option to remove and clear effects... ...with the /effect command. Also made the API for clearing effects more robust. --- mods/ITEMS/mcl_potions/commands.lua | 17 +++++++++++++++-- mods/ITEMS/mcl_potions/functions.lua | 17 ++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/mods/ITEMS/mcl_potions/commands.lua b/mods/ITEMS/mcl_potions/commands.lua index d1280c645..c977b8c1c 100644 --- a/mods/ITEMS/mcl_potions/commands.lua +++ b/mods/ITEMS/mcl_potions/commands.lua @@ -9,8 +9,8 @@ local S = minetest.get_translator(minetest.get_current_modname()) minetest.register_chatcommand("effect",{ - params = S("|heal|list [] [] [NOPART]"), - description = S("Add a status effect to yourself. Arguments: : 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 in seconds. (: amount of healing when the effect is \"heal\", passing a negative value subtracts health.) : 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. : 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."), + params = S("|heal|list|clear|remove [] [] [NOPART]"), + description = S("Add a status effect to yourself. Arguments: : 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 in seconds. (: amount of healing when the effect is \"heal\", passing a negative value subtracts health. : name of a status effect to be removed when using \"remove\" as the previous parameter.) : 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. : 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}, func = function(name, params) @@ -43,6 +43,19 @@ minetest.register_chatcommand("effect",{ return true, S("Player @1 harmed by @2 HP.", name, hp) 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 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 diff --git a/mods/ITEMS/mcl_potions/functions.lua b/mods/ITEMS/mcl_potions/functions.lua index 7d97df5ee..f63ca6e03 100644 --- a/mods/ITEMS/mcl_potions/functions.lua +++ b/mods/ITEMS/mcl_potions/functions.lua @@ -1385,12 +1385,18 @@ function mcl_potions._reset_player_effects(player, set_hud) return end + local removed_effects = {} for name, effect in pairs(registered_effects) do 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 mcl_potions._clear_cached_player_data(player) + for i=1, #removed_effects do + removed_effects[i](player) + end + if set_hud ~= false then potions_set_hud(player) end @@ -1535,7 +1541,16 @@ function mcl_potions.get_total_fatigue(object) end function mcl_potions.clear_effect(object, effect) - EF[effect][object] = nil + 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 + if def.after_end then def.after_end(object) end + end if not object:is_player() then return end potions_set_hud(object) end