mtimer-cd2025/system/chat_commands.lua
Dirk Sohler 43c633732e implement base functionality for chat commands
Chat commands alwys start with `/mtimer` and then the player defines
what action to perform and adds a payload.

    /mtimer help
    /mtimer show
    /mtimer set setting value

Issue: https://github.com/dsohler/mtimer/issues/1
2017-07-29 21:19:05 +02:00

64 lines
2.5 KiB
Lua

-- 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 = '<help>, <show>, <set setting value>',
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
})