From 08038272b6e632b3ee7ed6afd532efef7e448b78 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Mon, 4 Mar 2019 19:01:34 +0000 Subject: [PATCH] Added /tag and /tag-clear --- control.lua | 3 ++- expcore/commands.lua | 9 +++++---- expcore/common_parse.lua | 2 +- expcore/locale/en.cfg | 4 +++- locale/en/expcore.cfg | 4 +++- modules/commands/me.lua | 1 - modules/commands/tag.lua | 27 +++++++++++++++++++++++++++ 7 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 modules/commands/tag.lua diff --git a/control.lua b/control.lua index 2709a4e2..64bff3f4 100644 --- a/control.lua +++ b/control.lua @@ -18,7 +18,8 @@ local files = { 'modules.test', 'modules.commands.me', 'modules.commands.kill', - 'modules.commands.admin-chat' + 'modules.commands.admin-chat', + 'modules.commands.tag', } -- Loads all files in array above and logs progress diff --git a/expcore/commands.lua b/expcore/commands.lua index 5947bb71..ff717788 100644 --- a/expcore/commands.lua +++ b/expcore/commands.lua @@ -506,7 +506,7 @@ end function Commands.internal_error(success,command_name,error_message) if not success then Commands.error('Internal Error, Please contact an admin','utility/cannot_build') - log('[ERROR] command/'..command_name..' :: '..error_message) + log{'expcore-commands.command-error-log-format',command_name,error_message} end return not success end @@ -613,8 +613,7 @@ function Commands.run_command(command_event) -- used below as the reject function local parse_fail = function(error_message) error_message = error_message or '' - Commands.error('Invalid Param "'..param_name..'"; '..error_message) - return + return Commands.error{'expcore-commands.invalid-param',param_name,error_message} end -- input: string, player: LuaPlayer, reject: function, ... extra args local success,param_parsed = pcall(parse_callback,raw_params[index],player,parse_fail,unpack(param_data.parse_args)) @@ -629,7 +628,9 @@ function Commands.run_command(command_event) end elseif param_parsed == nil or param_parsed == Commands.defines.error or param_parsed == parse_fail then -- no value was returned or error was returned, if nil then give generic error - if not param_parsed == Commands.defines.error then Commands.error('Invalid Param "'..param_name..'"; please make sure it is the correct type') end + if not param_parsed == Commands.defines.error then + Commands.error{'expcore-commands.command-error-param-format',param_name,'please make sure it is the correct type'} + end return end -- adds the param to the table to be passed to the command callback diff --git a/expcore/common_parse.lua b/expcore/common_parse.lua index d5c706bf..ffafe46f 100644 --- a/expcore/common_parse.lua +++ b/expcore/common_parse.lua @@ -94,7 +94,7 @@ end) Commands.add_parse('player',function(input,player,reject) if not input then return end -- nil check local input_player = Game.get_player_from_any(input) - if not player then + if not input_player then return reject{'expcore-commands.reject-player',input} else return input_player diff --git a/expcore/locale/en.cfg b/expcore/locale/en.cfg index 6486cb5b..46b051e4 100644 --- a/expcore/locale/en.cfg +++ b/expcore/locale/en.cfg @@ -10,5 +10,7 @@ reject-player-alive=Player is dead. reject-force=Invaild Force Name reject-surface=Invaild surface Name invalid-inputs=Invalid Input, /__1__ __2__ +invalid-param=Invalid Param "__1__"; __2__ command-ran=Command Complete -command-fail=Command failed to run: __1__ \ No newline at end of file +command-fail=Command failed to run: __1__ +command-error-log-format=[ERROR] command/__1__ :: __2__ \ No newline at end of file diff --git a/locale/en/expcore.cfg b/locale/en/expcore.cfg index 6486cb5b..46b051e4 100644 --- a/locale/en/expcore.cfg +++ b/locale/en/expcore.cfg @@ -10,5 +10,7 @@ reject-player-alive=Player is dead. reject-force=Invaild Force Name reject-surface=Invaild surface Name invalid-inputs=Invalid Input, /__1__ __2__ +invalid-param=Invalid Param "__1__"; __2__ command-ran=Command Complete -command-fail=Command failed to run: __1__ \ No newline at end of file +command-fail=Command failed to run: __1__ +command-error-log-format=[ERROR] command/__1__ :: __2__ \ No newline at end of file diff --git a/modules/commands/me.lua b/modules/commands/me.lua index 2d183216..43cd96b0 100644 --- a/modules/commands/me.lua +++ b/modules/commands/me.lua @@ -1,5 +1,4 @@ local Commands = require 'expcore.commands' -require 'expcore.common_parse' Commands.new_command('me','Sends an action message in the chat') :add_param('action',false) diff --git a/modules/commands/tag.lua b/modules/commands/tag.lua new file mode 100644 index 00000000..3cffcd15 --- /dev/null +++ b/modules/commands/tag.lua @@ -0,0 +1,27 @@ +local Commands = require 'expcore.commands' +require 'expcore.common_parse' + +Commands.new_command('tag','Sets your player tag.') +:add_param('tag',false,'string-max-length',20) +:enable_auto_concat() +:register(function(player,tag,raw) + player.tag = ' - '..tag +end) + +Commands.new_command('tag-clear','Clears your tag. Or another player if you are admin.') +:add_param('player',true,'player') +:add_defaults{player=function(player) + return player +end} +:register(function(player,action_player,raw) + if action_player.index == player.index then + -- no player given so removes your tag + action_player.tag = '' + elseif player.admin then + -- player given and user is admin so clears that player's tag + action_player.tag = '' + else + -- user is not admin and tried to clear another users tag + return Commands.error{'expcore-commands.unauthorized'} + end +end) \ No newline at end of file