-- Colorize a string in the given color -- -- Colorizes a string in a given hexadecimal color coded color. The string will -- be returned including a reset color escape (assumably white text color) -- -- @param color The hexadecimal value for the color you want to use -- @param string The string that has to be colorized -- @return string The colorized string local colorize = function (color, string) local c = minetest.get_color_escape_sequence('#'..string.upper(color)) local r = minetest.get_color_escape_sequence('#FFFFFF') return c..string..r end -- Validate a new value -- -- 1. Check if setting is valid at all -- 2. If `show` has to be set, validate for 'true' or 'false' string -- 3. Validate against a simple regex -- 4. If all of the above was not `nil`, return `true` -- -- @param setting The setting the value is for -- @param new_value The new value that has to be validated -- @return bool If the new value for the setting is valid or not local validate = function (setting, new_value) local valid_values = { runtime = '^[^%c]+$', offset = '^%-?[0-9]+,%-?[0-9]+$', ingame_time = '^[^%c]+$', current_time = '^[^%c]+$', font_color = '^[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]$', start = '^[^%c]+$', show = '^$', -- unmatchable, see below locale = '^$', -- currently unsupported and not settable by user position = '^%-?[0-9]+,%-?[0-9]+$', format = '^[^%c]+$', alignment = '^%-?[0-9]+,%-?[0-9]+$' } if valid_values[setting] == nil then return false end -- Check if `new_value` for setting `show` is either 'true' or 'false' and -- return true in both cases (because either 'true' or 'false' are valid). if setting == 'show' and new_value == 'true' then return true end if setting == 'show' and new_value == 'false' then return true end if new_value:match(valid_values[setting]) == nil then return false end return true end -- Show help to the player -- -- @param player The player to show the help to local show_help = function (player) local help = colorize('729fcf', 'Mtimer Help')..'\n' local name = player:get_player_name() help = help..' '..colorize('73d216', '/mtimer help')..' = ' help = help..'Show this messaget\n' help = help..' '..colorize('73d216', '/mtimer show')..' = ' help = help..'List the current configuration\n' help = help..' '..colorize('73d216', '/mtimer set setting value')..' = ' help = help..'Set '..colorize('73d216', 'setting')..' to ' help = help..colorize('73d216', 'value') minetest.chat_send_player(name, help) end -- Show the configuration to the player -- -- Gathers all settable variables and prints them to the player -- -- @param player The player object of the player to print to local show_configuration = function (player) local name = player:get_player_name() local result_string = colorize('729fcf', 'MTimer Configuration')..'\n' local font_color = player:get_attribute('mtimer:font_color') local configuration = { runtime = player:get_attribute('mtimer:runtime'), offset = player:get_attribute('mtimer:offset'), ingame_time = player:get_attribute('mtimer:ingame_time'), current_time = player:get_attribute('mtimer:current_time'), font_color = colorize(font_color, font_color), start = player:get_attribute('mtimer:start'), show = player:get_attribute('mtimer:show'), locale = player:get_attribute('mtimer:locale'), position = player:get_attribute('mtimer:position'), format = player:get_attribute('mtimer:format'), alignment = player:get_attribute('mtimer:alignment') } for setting,value in pairs(configuration) do local s = colorize('73d216', setting) result_string = result_string..' '..s..' = '..value..'\n' end minetest.chat_send_player(name, result_string) end local set = function (player, payload) local name = player:get_player_name() local setting = payload:match('[%a_]+') local new_value = payload:gsub('^[%a_]+ ', '') local old_value = player:get_attribute('mtimer:'..setting) local status = '' if old_value == nil then minetest.chat_send_player(name, colorize('ef2929', 'Unknown setting!')) show_configuration(player) return end if validate(setting, new_value) == false then minetest.chat_send_player(name, colorize('ef2929', 'Invalid value!')) show_configuration(player) return end status = status..colorize('729fcf', '[MTimer]')..' ' status = status..'Setting '..colorize('73d216', setting)..' to ' status = status..colorize('75507b', new_value) print('[MTimer] '..name..' sets `'..setting..'` to `'..new_value..'`') minetest.chat_send_player(name, status) player:set_attribute('mtimer:'..setting, new_value) mtimer_update(player) end -- Register the chat command that will be used to interact with MTimer. -- -- The chat command has three actions: -- -- help Show help to the user via the regular chat -- show Show the current configuration to the user -- set Set the given setting to the given value minetest.register_chatcommand('mtimer', { params = ', , ', description = 'Manage MTimer display', func = function(name, parameters) local player = minetest.get_player_by_name(name) local action = parameters:match('%a+') local payload = parameters:gsub('^%a+ ', '') if action == 'help' then show_help(player) end if action == 'show' then show_configuration(player) end if action == 'set' and payload ~= nil then set(player, payload) end end })