Add support for shared settings (#127)

* Add support for shared settings

* Optimize stored setting value getting

Co-authored-by: SX <50966843+S-S-X@users.noreply.github.com>

* Avoid false for default transfer value

In set_setting, to eventually transfer to another mod. Suggested by S-S-X in #127

---------

Co-authored-by: SX <50966843+S-S-X@users.noreply.github.com>
This commit is contained in:
Athozus 2024-02-01 10:18:55 +01:00 committed by GitHub
parent 721d882c26
commit 46012a2b28
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -396,24 +396,32 @@ function mail.extractMaillists(receivers_string, maillists_owner)
return receivers
end
function mail.get_setting_default_value(setting_name)
return mail.settings[setting_name].default
function mail.get_setting_default_value(key)
return mail.settings[key].default
end
function mail.get_setting(playername, setting_name)
function mail.get_setting(playername, key)
local entry = mail.get_storage_entry(playername)
if entry.settings[setting_name] ~= nil then
return entry.settings[setting_name]
else
return mail.get_setting_default_value(setting_name)
local value = (entry.settings[key] == nil
and {mail.get_setting_default_value(key)}
or {entry.settings[key]})[1]
if mail.settings[key].sync then -- in case this setting is shared with another mod
value = mail.settings[key].sync(playername, key) -- get new value
mail.set_setting(playername, key, value, false) -- update the setting in mail storage and don't transfer it again
end
return value
end
-- add or update a setting
function mail.set_setting(playername, key, value)
function mail.set_setting(playername, key, value, not_transfer)
local entry = mail.get_storage_entry(playername)
entry.settings[key] = value
mail.set_storage_entry(playername, entry)
if not not_transfer and mail.settings[key].transfer then -- in case this setting is shared with another mod
mail.settings[key].transfer(playername, key, value)
end
end
function mail.reset_settings(playername)