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
This commit is contained in:
Dirk Sohler 2017-07-29 21:19:05 +02:00
parent d65c1f11ba
commit 43c633732e
2 changed files with 67 additions and 1 deletions

@ -1,7 +1,10 @@
-- Load default configuration andf functions -- Load default configuration
local modpath = minetest.get_modpath(minetest.get_current_modname())..DIR_DELIM local modpath = minetest.get_modpath(minetest.get_current_modname())..DIR_DELIM
local config = dofile(modpath..'system'..DIR_DELIM..'get_options.lua') 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..'timer_function.lua')
dofile(modpath..'system'..DIR_DELIM..'chat_commands.lua')
-- Set custom player attribute if not set -- Set custom player attribute if not set

63
system/chat_commands.lua Normal file

@ -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 = '<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
})