From 4fccddf784616effe5fdc98bd05a404fa8b84db4 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Fri, 8 Mar 2019 15:55:57 +0000 Subject: [PATCH] Added /chelp --- control.lua | 1 + expcore/commands.lua | 2 +- locale/en/commands-local.cfg | 5 ++- modules/commands/commands-local.cfg | 5 ++- modules/commands/help.lua | 60 +++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 modules/commands/help.lua diff --git a/control.lua b/control.lua index 160eabee..2724d18b 100644 --- a/control.lua +++ b/control.lua @@ -23,6 +23,7 @@ local files = { 'modules.commands.teleport', 'modules.commands.cheat-mode', 'modules.commands.interface', + 'modules.commands.help', } -- Loads all files in array above and logs progress diff --git a/expcore/commands.lua b/expcore/commands.lua index d8acbb01..4679b437 100644 --- a/expcore/commands.lua +++ b/expcore/commands.lua @@ -303,7 +303,7 @@ function Commands.search(keyword,allowed_player) matches[name] = { name=name, help=description, - description=description, + description='', aliases={} } end diff --git a/locale/en/commands-local.cfg b/locale/en/commands-local.cfg index 3e5abe44..20b325c8 100644 --- a/locale/en/commands-local.cfg +++ b/locale/en/commands-local.cfg @@ -2,4 +2,7 @@ kill-already-dead=You are already dead. admin-chat-format=[Admin Chat] [color=__3__]__1__: __2__ tp-no-position-found=No position to teleport to was found, please try again later. -tp-to-self=Player can not be teleported to themselves. \ No newline at end of file +tp-to-self=Player can not be teleported to themselves. +chelp-title=Help results for __1__ page __2__ of __3__ +chelp-format=/__1__ __2__ - __3__ +chelp-out-of-range=__1__ is an invalid page number. \ No newline at end of file diff --git a/modules/commands/commands-local.cfg b/modules/commands/commands-local.cfg index 3e5abe44..20b325c8 100644 --- a/modules/commands/commands-local.cfg +++ b/modules/commands/commands-local.cfg @@ -2,4 +2,7 @@ kill-already-dead=You are already dead. admin-chat-format=[Admin Chat] [color=__3__]__1__: __2__ tp-no-position-found=No position to teleport to was found, please try again later. -tp-to-self=Player can not be teleported to themselves. \ No newline at end of file +tp-to-self=Player can not be teleported to themselves. +chelp-title=Help results for __1__ page __2__ of __3__ +chelp-format=/__1__ __2__ - __3__ +chelp-out-of-range=__1__ is an invalid page number. \ No newline at end of file diff --git a/modules/commands/help.lua b/modules/commands/help.lua new file mode 100644 index 00000000..9483dcb6 --- /dev/null +++ b/modules/commands/help.lua @@ -0,0 +1,60 @@ +local Commands = require 'expcore.commands' +local Global = require 'utils.global' +require 'expcore.common_parse' + +local results_per_page = 5 + +local search_cache = {} +Global.register(search_cache,function(tbl) + search_cache = tbl +end) + +Commands.new_command('chelp','Searches for a keyword in all commands you are allowed to use.') +:add_param('keyword',false) -- the keyword that will be looked for +:add_param('page',true,'integer') -- the keyword that will be looked for +:add_defaults{page=1} +:register(function(player,keyword,page,raw) + -- gets a value for pages, might have result in cache + local pages + if search_cache[player.index] and search_cache[player.index].keyword == keyword:lower() then + pages = search_cache[player.index].pages + else + pages = {{}} + local current_page = 1 + local page_count = 0 + -- loops other all commands returned by search, includes game commands + for _,command_data in pairs(Commands.search(keyword,player)) do + -- if the number of results if greater than the number already added then it moves onto a new page + if page_count > results_per_page then + page_count = 0 + current_page = current_page + 1 + table.insert(pages,{}) + end + -- adds the new command to the page + page_count = page_count + 1 + table.insert(pages[current_page],{ + 'exp-commands.chelp-format', + command_data.name, + command_data.description, + command_data.help, + command_data.aliases:concat(', ') + }) + end + -- adds the result to the cache + search_cache[player.index] = { + keyword=keyword:lower(), + pages=pages + } + end + -- print the requested page + Commands.print{'exp-commands.chelp-title',keyword,page,#pages} + if pages[page] then + for _,command in pairs(pages[page]) do + Commands.print(command) + end + else + Commands.print{'exp-commands.chelp-out-of-range',page} + end + -- blocks command complete message + return Commands.success +end) \ No newline at end of file