From ad03d535d669ade16d3fc7b6e208c6330e4f3d91 Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Thu, 21 Mar 2024 01:27:11 +0100 Subject: [PATCH] Added mcl_luck API Luck and Bad Luck effects use the mcl_luck API now The API is unused for now --- mods/ITEMS/mcl_potions/functions.lua | 14 ++++++++---- mods/PLAYER/mcl_luck/init.lua | 33 ++++++++++++++++++++++++++++ mods/PLAYER/mcl_luck/mod.conf | 3 +++ 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 mods/PLAYER/mcl_luck/init.lua create mode 100644 mods/PLAYER/mcl_luck/mod.conf diff --git a/mods/ITEMS/mcl_potions/functions.lua b/mods/ITEMS/mcl_potions/functions.lua index db50c7058..50cde6bae 100644 --- a/mods/ITEMS/mcl_potions/functions.lua +++ b/mods/ITEMS/mcl_potions/functions.lua @@ -610,10 +610,13 @@ mcl_potions.register_effect({ return (not object:is_player()) end, on_start = function(object, factor) --- mcl_luck.add_luck_modifier(object, "mcl_potions:luck", factor) + mcl_luck.apply_luck_modifier(object:get_player_name(), "mcl_potions:luck", factor) + end, + on_load = function(object, factor) + mcl_luck.apply_luck_modifier(object:get_player_name(), "mcl_potions:luck", factor) end, on_end = function(object) --- mcl_luck.remove_luck_modifier(object, "mcl_potions:luck") + mcl_luck.remove_luck_modifier(object:get_player_name(), "mcl_potions:luck") end, uses_factor = true, }) @@ -626,10 +629,13 @@ mcl_potions.register_effect({ return (not object:is_player()) end, on_start = function(object, factor) --- mcl_luck.add_luck_modifier(object, "mcl_potions:bad_luck", -factor) + mcl_luck.apply_luck_modifier(object:get_player_name(), "mcl_potions:bad_luck", -factor) + end, + on_load = function(object, factor) + mcl_luck.apply_luck_modifier(object:get_player_name(), "mcl_potions:bad_luck", -factor) end, on_end = function(object) --- mcl_luck.remove_luck_modifier(object, "mcl_potions:bad_luck") + mcl_luck.remove_luck_modifier(object:get_player_name(), "mcl_potions:bad_luck") end, uses_factor = true, }) diff --git a/mods/PLAYER/mcl_luck/init.lua b/mods/PLAYER/mcl_luck/init.lua new file mode 100644 index 000000000..275cea52f --- /dev/null +++ b/mods/PLAYER/mcl_luck/init.lua @@ -0,0 +1,33 @@ +mcl_luck = {} + +-- table indexed by player name +-- each entry for each player contains list of modifiers applied to the player +-- modifiers are listed by their name (defined when applying them) +-- all modifiers are dynamic (they are removed when the player leaves game and on server shutdown) +local applied_luck = {} + +function mcl_luck.apply_luck_modifier(player_name, modifier_name, amount) + applied_luck[player_name][modifier_name] = amount +end + +function mcl_luck.remove_luck_modifier(player_name, modifier_name) + applied_luck[player_name][modifier_name] = nil +end + +function mcl_luck.get_luck(player_name) + local luck = 0 + for _, amount in pairs(applied_luck[player_name]) do + luck = luck + amount + end + return luck +end + +minetest.register_on_joinplayer(function(player) + local player_name = player:get_player_name() + applied_luck[player_name] = {} +end) + +minetest.register_on_leaveplayer(function(player) + local player_name = player:get_player_name() + applied_luck[player_name] = nil +end) diff --git a/mods/PLAYER/mcl_luck/mod.conf b/mods/PLAYER/mcl_luck/mod.conf new file mode 100644 index 000000000..395c888bb --- /dev/null +++ b/mods/PLAYER/mcl_luck/mod.conf @@ -0,0 +1,3 @@ +name = mcl_luck +author = Herowl +description = An API for handling luck, it can be polled by random events.