diff --git a/init.lua b/init.lua index 28858a5..c9868a1 100644 --- a/init.lua +++ b/init.lua @@ -1,7 +1,10 @@ --- Load default configuration andf functions +-- Load default configuration local modpath = minetest.get_modpath(minetest.get_current_modname())..DIR_DELIM local config = dofile(modpath..'system'..DIR_DELIM..'get_options.lua') + +-- Load functions dofile(modpath..'system'..DIR_DELIM..'timer_function.lua') +dofile(modpath..'system'..DIR_DELIM..'chat_commands.lua') -- Set custom player attribute if not set diff --git a/system/chat_commands.lua b/system/chat_commands.lua new file mode 100644 index 0000000..9a8f3be --- /dev/null +++ b/system/chat_commands.lua @@ -0,0 +1,63 @@ +-- 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) +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 + + +-- 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 + + +-- 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 == 'show' then show_configuration(player) end + if action == 'help' then show_help(player) end + if action == 'set' and payload ~= nil then set(player, payload) end + end +})