From 950cfe10d3caee3225b8e8b71115b164f26a5f5c Mon Sep 17 00:00:00 2001 From: plague006 Date: Fri, 1 Feb 2019 20:46:12 -0500 Subject: [PATCH 1/3] Add command notifications --- utils/command.lua | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/utils/command.lua b/utils/command.lua index c68c5d44..1a764336 100644 --- a/utils/command.lua +++ b/utils/command.lua @@ -19,6 +19,12 @@ local deprecated_command_alternatives = { ['color-redmew'] = 'redmew-color' } +local notify_on_commands = { + ['version'] = 'RedMew has a version as well, accessible via /redmew-version', + ['color'] = 'RedMew allows color saving and a color randomizer: check out /redmew-color', + ['ban'] = 'In case your forgot: please remember to include a message on how to appeal a ban' +} + local option_names = { ['description'] = 'A description of the command', ['arguments'] = 'A table of arguments, example: {"foo", "bar"} would map the first 2 arguments to foo and bar', @@ -256,8 +262,8 @@ function Command.search(keyword) return matches end ---- Warns in-game players of deprecated commands, ignores the server -local function notify_deprecated(event) +--- Trigger messages on deprecated or defined commands, ignores the server +local function on_command(event) local alternative = deprecated_command_alternatives[event.command] if alternative then if event.player_index then @@ -265,6 +271,14 @@ local function notify_deprecated(event) player.print(format('Warning! Usage of the command "/%s" is deprecated. Please use "/%s" instead.', event.command, alternative)) end end + + local notification = notify_on_commands[event.command] + if notification then + if event.player_index then + local player = Game.get_player_by_index(event.player_index) + player.print(notification) + end + end end --- Traps command errors if not in DEBUG. @@ -286,6 +300,6 @@ if not _DEBUG then end end -Event.add(defines.events.on_console_command, notify_deprecated) +Event.add(defines.events.on_console_command, on_command) return Command From 0705a76ee065aa0d9161feeaa28ae21039a8acef Mon Sep 17 00:00:00 2001 From: plague006 Date: Sat, 2 Feb 2019 12:11:31 -0500 Subject: [PATCH 2/3] Fix nested ifs --- utils/command.lua | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/utils/command.lua b/utils/command.lua index 1a764336..88fd0874 100644 --- a/utils/command.lua +++ b/utils/command.lua @@ -273,11 +273,9 @@ local function on_command(event) end local notification = notify_on_commands[event.command] - if notification then - if event.player_index then - local player = Game.get_player_by_index(event.player_index) - player.print(notification) - end + if notification and event.player_index then + local player = Game.get_player_by_index(event.player_index) + player.print(notification) end end From 50e686e77565bd3a0e9011a7ba250b4ba43c00a9 Mon Sep 17 00:00:00 2001 From: plague006 Date: Sun, 3 Feb 2019 01:01:32 -0500 Subject: [PATCH 3/3] Cleanup, check for player --- utils/command.lua | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/utils/command.lua b/utils/command.lua index 88fd0874..3ab79ef2 100644 --- a/utils/command.lua +++ b/utils/command.lua @@ -264,10 +264,14 @@ end --- Trigger messages on deprecated or defined commands, ignores the server local function on_command(event) + if not event.player_index then + return + end + local alternative = deprecated_command_alternatives[event.command] if alternative then - if event.player_index then - local player = Game.get_player_by_index(event.player_index) + local player = Game.get_player_by_index(event.player_index) + if player then player.print(format('Warning! Usage of the command "/%s" is deprecated. Please use "/%s" instead.', event.command, alternative)) end end @@ -275,7 +279,9 @@ local function on_command(event) local notification = notify_on_commands[event.command] if notification and event.player_index then local player = Game.get_player_by_index(event.player_index) - player.print(notification) + if player then + player.print(notification) + end end end