diff --git a/README.md b/README.md index 0b8fd17..397704d 100644 --- a/README.md +++ b/README.md @@ -85,14 +85,29 @@ mtimer_position = bl mtimer_timezone_offset = 0 mtimer_visible = true +mtimer_hud_element_offset_x = 0 +mtimer_hud_element_offset_y = 0 + mtimer_host_time_format = {24h}:{min} ({isodate}) mtimer_ingame_time_format = {24h}:{min} mtimer_real_time_format = {24h}:{min} ({isodate}) mtimer_session_duration_format = {hours}:{minutes} mtimer_session_start_time_format = {isodate}T{isotime} +mtimer_timer_format = S('Current Date: @1', '{rd}'){_n}S('Ingame Time: @1', '{it}'){_n}S('Session Start: @1', '{st}'){_n}S('Session Duration: @1', '{sd}') + +mtimer_custom_timer_running_format = d: {days}, h: {hours}, m: {minutes}, s: {seconds} +mtimer_custom_timer_stopped_format = S('The timer is stopped') +mtimer_custom_timer_finished_format = S('The timer has finished') + +mtimer_custom_timer_value_days = 0 +mtimer_custom_timer_value_hours = 0 +mtimer_custom_timer_value_minutes = 0 +mtimer_custom_timer_value_seconds = 0 + +mtimer_custom_timer_mode = countdown ``` -The timer format and the HUD element’s offset can’t be set because those values need special formatting that can’t be done using Minetest’s settings API right now. Namely a serialized table for the offset and a string with newlines +The `mtimer_timer_format` value allows a special replacement variable. All occurrences of a literal `{_n}` will be replaced with a newline. This is done because configuration values cannot have a newline. All `S()` values are translated. Unfortunately currently you cannot set translatable string in configuration options. ## Known Issues diff --git a/system/load_configuration.lua b/system/load_configuration.lua index 90fd379..63f6d8f 100644 --- a/system/load_configuration.lua +++ b/system/load_configuration.lua @@ -26,31 +26,52 @@ local worldconfig = Settings(worldpath..DIR_DELIM..'_mtimer.conf') -- 2. `_mtimer.conf` in the loaded world’s directory -- 3. Provided default value when calling the function -- +-- If the value `return_only` is set then the function does only return the +-- determined value instead of writing the meta table entry. +-- +-- If `replace` is a key-value table then occurrences of the key in a special +-- variable in the configuration value are replaced. +-- +-- replace = { +-- foobar = 'My Foobar Value' +-- } +-- +-- This example searches for all variables `{_foobar}` and replaces them with +-- `'My Foobar Value'`. Note the underscore. The underscore prevents confusion +-- with timer-related variables. +-- -- @param key_name The unprefixed name of the key to get -- @param default_value What to return when the configuration option is missing --- @param changeable If the option is changeable via the config files +-- @param return_only Only return the configuration value and do nothing +-- @param replace A replacement table as described -- @return string Either the configuration option’s value or an empty string -local set = function (key_name, default_value, changeable) +local set = function (key_name, default_value, return_only, replace) local meta_key = 'mtimer:'..key_name local config_option = 'mtimer_'..key_name local value = default_value - if changeable ~= false then - local global_setting = minetest.settings:get(config_option) - local world_setting = worldconfig:get(config_option) - value = world_setting or global_setting or default_value or '' - end + -- Get the setting from one of the possible locations + local global_setting = minetest.settings:get(config_option) + local world_setting = worldconfig:get(config_option) - mtimer.meta[key_name] = { - key = meta_key, - default = tostring(value) - } + -- Define value + value = world_setting or global_setting or default_value or '' + if type(replace) == 'table' then value=value:gsub('{_([^}]*)}',replace) end + + -- Return or store value + if return_only == true then return tostring(value) end + mtimer.meta[key_name] = { key = meta_key, default = tostring(value) } end +-- Set HUD element offset table using the custom values +set('hud_element_offset', minetest.serialize({ + x = set('hud_element_offset_x', 0, true), + y = set('hud_element_offset_y', 0, true) +})) + -- Display settings set('color', '#ffffff') -set('hud_element_offset', minetest.serialize({ x = 0, y = 0 }), false) set('hud_element_size', 1) set('position', 'bl') set('timezone_offset', 0) @@ -81,16 +102,21 @@ set('session_start_time_format', '{isodate}T{isotime}') -- and the stored `start_timestamp`. Here the `target_message` -- is ignored and will never be shown. set('custom_timer_settings', minetest.serialize({ - values = { days = 0, hours = 0, minutes = 0, seconds = 0 }, + values = { + days = tonumber(set('custom_timer_value_days', 0, true)), + hours = tonumber(set('custom_timer_value_hours', 0, true)), + minutes = tonumber(set('custom_timer_value_minutes', 0, true)), + seconds = tonumber(set('custom_timer_value_seconds', 0, true)) + }, start_timestamp = 0, format = { - running = 'd: {days}, h: {hours}, m: {minutes}, s: {seconds}', - stopped = S('The timer is stopped'), - finished = S('The timer has finished') + running = set('custom_timer_running_format', 'd: {days}, h: {hours}, m: {minutes}, s: {seconds}', true), + stopped = set('custom_timer_stopped_format', S('The timer is stopped'), true), + finished = set('custom_timer_finished_format', S('The timer has finished'), true) }, - timer_mode = 'countdown', + timer_mode = set('custom_timer_mode', 'countdown', true), running = false -}), false) +})) -- Timer display format (the HUD element’s content) @@ -99,4 +125,4 @@ set('timer_format', table.concat({ S('Ingame Time: @1', '{it}'), S('Session Start: @1', '{st}'), S('Session Duration: @1', '{sd}') -}, '\n'), false) +}, '{_n}'), false, { n = '\n' })