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 return receivers
end end
function mail.get_setting_default_value(setting_name) function mail.get_setting_default_value(key)
return mail.settings[setting_name].default return mail.settings[key].default
end end
function mail.get_setting(playername, setting_name) function mail.get_setting(playername, key)
local entry = mail.get_storage_entry(playername) local entry = mail.get_storage_entry(playername)
if entry.settings[setting_name] ~= nil then local value = (entry.settings[key] == nil
return entry.settings[setting_name] and {mail.get_setting_default_value(key)}
else or {entry.settings[key]})[1]
return mail.get_setting_default_value(setting_name)
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 end
return value
end end
-- add or update a setting -- 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) local entry = mail.get_storage_entry(playername)
entry.settings[key] = value entry.settings[key] = value
mail.set_storage_entry(playername, entry) 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 end
function mail.reset_settings(playername) function mail.reset_settings(playername)