1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-03-27 21:48:52 +02:00

Commands to locale

Change target_name refs to poll the player
This commit is contained in:
plague006 2019-03-02 12:20:56 -05:00
parent c4ea7c0fa3
commit fc48d264cf
13 changed files with 232 additions and 177 deletions

View File

@ -107,7 +107,9 @@ local function remove_regular(args)
if Rank.equal(target_name, Ranks.regular) then
local _, new_rank = Rank.reset_player_rank(target_name)
game.print({'admin_commands.regular_remove_success', Utils.get_actor(), target_name, new_rank}, Color.info)
target_player.print({'admin_commands.regular_remove_notify_target'}, Color.warning)
if target_player then
target_player.print({'admin_commands.regular_remove_notify_target'}, Color.warning)
end
else
local rank_name = Rank.get_player_rank_name(target_name)
Game.player_print({'admin_commands.regular_remove_fail', target_name, rank_name}, Color.fail)
@ -120,19 +122,23 @@ local function probation_add(args)
local target_player = game.players[target_name]
if not target_player or not target_player.valid then
Game.player_print({'common.warn_no_target', target_name}, Color.warning)
Game.player_print({'common.warn_no_target', target_name}, Color.warning)
end
if Rank.equal(target_name, Ranks.admin) then
target_player.print({'admin_commands.probation_warn_admin', Utils.get_actor()}, Color.warning)
Game.player_print({'admin_commands.probation_add_fail_admin'}, Color.fail)
if target_player then
target_player.print({'admin_commands.probation_warn_admin', Utils.get_actor()}, Color.warning)
end
return
end
local success = Rank.decrease_player_rank_to(target_name, Ranks.probation)
if success then
game.print({'admin_commands.probation_add_success', Utils.get_actor(), target_name}, Color.info)
target_player.print({'admin_commands.probation_add_notify_target'}, Color.warning)
if target_player then
target_player.print({'admin_commands.probation_add_notify_target'}, Color.warning)
end
else
Game.player_print({'admin_commands.probation_add_fail', target_name}, Color.fail)
end
@ -144,13 +150,15 @@ local function probation_remove(args)
local target_player = game.players[target_name]
if not target_player or not target_player.valid then
Game.player_print({'common.warn_no_target', target_name}, Color.warning)
Game.player_print({'common.warn_no_target', target_name}, Color.warning)
end
if Rank.equal(target_name, Ranks.probation) then
Rank.reset_player_rank(target_name)
game.print({'admin_commands.probation_remove_success', Utils.get_actor(), target_name}, Color.info)
target_player.print({'admin_commands.probation_remove_notify_target'}, Color.warning)
if target_player then
target_player.print({'admin_commands.probation_remove_notify_target'}, Color.warning)
end
else
Game.player_print({'admin_commands.probation_remove_fail', target_name}, Color.fail)
end
@ -201,11 +209,11 @@ local redmew_commands_untempban =
--- Gives a player a temporary ban
local function tempban(args, player)
local target_name = args.player
local target = game.players[target_name]
local target_ident = args.player
local target = game.players[target_ident]
local duration = args.minutes
if not target then
print_no_target(target_name)
print_no_target(target_ident)
return
end
if not tonumber(duration) then
@ -213,6 +221,7 @@ local function tempban(args, player)
return
end
local target_name = target.name
local group = get_tempban_group()
local actor
if player then
@ -242,10 +251,10 @@ end
--- Takes a target and teleports them to player
local function invoke(args, player)
local target_name = args.player
local target = game.players[target_name]
local target_ident = args.player
local target = game.players[target_ident]
if not target then
print_no_target(target_name)
print_no_target(target_ident)
return
end
local pos = player.surface.find_non_colliding_position('player', player.position, 50, 1)
@ -254,20 +263,22 @@ local function invoke(args, player)
return
end
target.teleport({pos.x, pos.y}, player.surface)
game.print(args.player .. ', get your ass over here!')
game.print(target.name .. ', get your ass over here!')
end
--- Takes a target and teleports player to target. (admin only)
local function teleport_player(args, player)
local target_name = args.player
local target_ident = args.player
local target
if target_name then
target = game.players[target_name]
if target_ident then
target = game.players[target_ident]
end
if not target then
print_no_target(target_name)
print_no_target(target_ident)
return
end
local target_name = target.name
local surface = target.surface
local pos = surface.find_non_colliding_position('player', target.position, 50, 1)
if not pos then
@ -503,11 +514,11 @@ Command.add(
Command.add(
'tp',
{
description = 'if blank, teleport to selected entity. mode = toggle tp mode where you can teleport to a placed ghost. player = teleport to player.',
description = {'command_description.tp'},
arguments = {'mode|player'},
default_values = {['mode|player'] = false},
required_rank = Ranks.admin,
custom_help_text = '<blank|mode|player> 3 different uses: "/tp" to tp to selected entity. "/tp mode" to toggle tp mode. "/tp Newcott" to tp to Newcott'
custom_help_text = {'command_custom_help.tp'},
},
teleport_command
)

View File

@ -71,7 +71,7 @@ local function create_camera(args, player)
mainframe.add {type = 'frame', name = 'cameraframe', style = 'captionless_frame'}
end
mainframe.add {type = 'label', caption = 'Following: ' .. args.target}
mainframe.add {type = 'label', caption = 'Following: ' .. target.name}
local close_button = mainframe.add {type = 'button', name = main_button_name, caption = 'Close'}
apply_button_style(close_button)
local target_index = target.index

View File

@ -37,12 +37,12 @@ end
--- Kill a player: admins and the server can kill others, non-admins can only kill themselves
local function kill(args, player)
local target_name = args.player
local target_ident = args.player
local target
if target_name then
target = game.players[target_name]
if target_ident then
target = game.players[target_ident]
if not target then
Game.player_print(format('Player %s was not found.', target_name))
Game.player_print(format('Player %s was not found.', target.name))
return
end
end
@ -108,21 +108,22 @@ end
--- Creates an alert for the player at the location of their target
local function find_player(args, player)
local name = args.player
local target_ident = args.player
local target = game.players[name]
local target = game.players[target_ident]
if not target then
Game.player_print('player ' .. name .. ' not found')
Game.player_print('player ' .. target_ident .. ' not found')
return
end
local target_name = target.name
target = target.character
if not target or not target.valid then
Game.player_print('player ' .. name .. ' does not have a character')
Game.player_print('player ' .. target_name .. ' does not have a character')
return
end
player.add_custom_alert(target, {type = 'virtual', name = 'signal-F'}, name, true)
player.add_custom_alert(target, {type = 'virtual', name = 'signal-F'}, target_name, true)
end
--- Turns on rail block visualization for player
@ -234,20 +235,21 @@ end
--- Prints information about the target player
local function print_player_info(args, player)
local name = args.player
local target = game.players[name]
local target_ident = args.player
local target = game.players[target_ident]
if not target then
Game.player_print('Target not found')
return
end
local target_name = target.name
local index = target.index
local info_t = {
'redmew_commands.whois_formatter',
{'format.1_colon_2', 'Name', name},
{'format.1_colon_2', 'Name', target_name},
{'format.single_item', target.connected and 'Online: yes' or 'Online: no'},
{'format.1_colon_2', 'Index', target.index},
{'format.1_colon_2', 'Rank', Rank.get_player_rank_name(name)},
{'format.1_colon_2', 'Rank', Rank.get_player_rank_name(target_name)},
{'format.single_item', Donator.is_donator(target.name) and 'Donator: yes' or 'Donator: no'},
{'format.1_colon_2', 'Time played', Utils.format_time(target.online_time)},
{'format.1_colon_2', 'AFK time', Utils.format_time(target.afk_time or 0)},

View File

@ -0,0 +1,7 @@
# This file holds all the locale strings for command help and descriptions
[command_description]
tp=If blank, teleport to selected entity. mode = toggle tp mode where you can teleport to a placed ghost. player = teleport to player.
[command_custom_help]
tp=<blank|mode|player> 3 different uses: "/tp" to tp to selected entity. "/tp mode" to toggle tp mode. "/tp Newcott" to tp to Newcott.

View File

@ -19,8 +19,6 @@ probation_warn_admin=__1__ hat versucht dich auf Bewährung zu setzen, kannst du
probation_remove_notify_target=Du bist nicht länger auf Bewährung.
probation_remove_success=__1__ hat __2__s Bewährung aufgehoben..
probation_remove_fail=__1__ ist nicht auf Bewährung.
tp_description=If blank, teleport to selected entity. mode = toggle tp mode where you can teleport to a placed ghost. player = teleport to player.
tp_custom_help=<blank|mode|player> 3 different uses: "/tp" to tp to selected entity. "/tp mode" to toggle tp mode. "/tp Newcott" to tp to Newcott.
[redmew_commands]
whois_formatter=__1__\n__2__\n__3__\n__4__\n__5__\n__6__\n__7__\n__8__\n__9__\n__10__\n__11__\n__12__\n__13__\n__14__\n__15__\n__16__\n

View File

@ -0,0 +1,7 @@
# This file holds all the locale strings for command help and descriptions
[command_description]
tp=If blank, teleport to selected entity. mode = toggle tp mode where you can teleport to a placed ghost. player = teleport to player.
[command_custom_help]
tp=<blank|mode|player> 3 different uses: "/tp" to tp to selected entity. "/tp mode" to toggle tp mode. "/tp Newcott" to tp to Newcott.

View File

@ -0,0 +1,18 @@
# This file is to aggregate all commonly-used or shared locale strings.
[common]
fail_no_target=No player found with name: __1__
warn_no_target=Warning: player __1__ is not found, but the command will still be executed.
[ranks]
probation=Probation
guest=Guest
auto_trusted=Auto Trusted
regular=Regular
admin=Admin
donator=Donator
donator_abbreviation=D
[format]
1_colon_2=__1__: __2__
single_item=__1__

View File

@ -1,19 +1,4 @@
[common]
fail_no_target=No player found with name: __1__
warn_no_target=Warning: player __1__ is not found, but the command will still be executed.
[ranks]
probation=Probation
guest=Guest
auto_trusted=Auto Trusted
regular=Regular
admin=Admin
donator=Donator
donator_abbreviation=D
[format]
1_colon_2=__1__: __2__
single_item=__1__
# This file holds all the locale strings for features
[donator]
death_message=__1__ has perished and will be missed by all, but wanted to share this last message:
@ -49,11 +34,6 @@ donator_message_wrong_arg1=Correct use: /donator-welcome-message add|delete|list
donator_welcome_message_help=Adds, deletes, or lists donator on-welcome messages.
donator_death_message_help=Adds, deletes, or lists donator on-death messages.
[command]
help_text_format=__1__ __2__ __3__
higher_rank_needed=The command __1__ requires __2__ rank or higher to be be executed.
required_rank= (Rank __1__ or above only)
[apocalypse]
run_twice=The game has been saved, run the command again to commence the apocalypse.
apocalypse_begins=The ground begins to rumble. It seems as if the world itself is coming to an end.
@ -69,6 +49,3 @@ train_warning=__1__ used a train to destroy another train and has been warned.
train_player_warning=You have destroyed another train with yours.\nRepeated infractions will be punished.
train_jailing=__1__ used a train to destroy another train and has been jailed.
multiple_passengers=Note: There were __1__ players in the train and any could have been controlling it: __2__
[utils_core]
print_admins=__1__(ADMIN) __2__: __3__

View File

@ -0,0 +1 @@
# This file holds all the locale strings for map_gen modules excluding maps

View File

@ -0,0 +1 @@
# This file holds all the locale strings for specific maps

View File

@ -0,0 +1,27 @@
# This file holds all the locale strings for the utils
[command]
help_text_format=__1__ __2__ __3__
log_entry=__1__(Map time: __2__) [__3__ Command] __4__, used: __5__ __6__
required_rank=(Rank __1__ or above only)
donator_only=(Donator only)
server_only=(Server only)
undocumented_command=[Undocumented command]
not_allowed_by_server=The command '__1__' is not allowed to be executed by the server.
not_allowed_by_players=The command '__1__' is not allowed to be executed by players.
higher_rank_needed=The command '__1__' requires __2__ rank or higher to be be executed.
not_allowed_by_non_donators=The command '__1__' is only allowed for donators.
fail_missing_argument=Argument "__1__" from command __2__ is missing.
warn_player_of_error=There was an error running __1__, it has been logged.
failed_command=Sorry there was an error running __1__
warn_deprecated_command=Warning! Usage of the command "/__1__" is deprecated. Please use "/__2__" instead.
error_bad_option=The following options were given to the command '__1__' but are invalid: __2__
error_no_player_no_server=The command '__1__' is not allowed by the server nor player, please enable at least one of them.
error_while_running_debug=__1__ triggered an error running a command and has been logged: '__2__' with arguments __3__
error_log=Error while running '__1__' with arguments __2__: __3__
[utils_core]
print_admins=__1__(ADMIN) __2__: __3__

View File

@ -45,7 +45,7 @@ local option_names = {
['allowed_by_player'] = 'Set to false to disable players from executing this command',
['log_command'] = 'Set to true to log commands. Always true when admin is required',
['capture_excess_arguments'] = 'Allows the last argument to be the remaining text in the command',
['custom_help_text'] = 'Sets a custom help text to override the auto-generated help',
['custom_help_text'] = 'Sets a custom help text to override the auto-generated help'
}
---Validates if there aren't any wrong fields in the options.
@ -60,7 +60,7 @@ local function assert_existing_options(command_name, options)
end
if next(invalid) then
error(format("The following options were given to the command '%s' but are invalid: %s", command_name, serialize(invalid)))
error(format("The following options were given to the command '%s' but are invalid: %s", command_name, serialize(invalid))) -- command.error_bad_option when bug fixed
end
end
@ -89,7 +89,7 @@ end
---@param options table
---@param callback function
function Command.add(command_name, options, callback)
local description = options.description or '[Undocumented command]'
local description = options.description or {'command.undocumented_command'}
local arguments = options.arguments or {}
local default_values = options.default_values or {}
local required_rank = options.required_rank or Ranks.guest
@ -113,9 +113,8 @@ function Command.add(command_name, options, callback)
if (not _DEBUG and debug_only) and (not _CHEATS and cheat_only) then
return
end
if not allowed_by_player and not allowed_by_server then
error(format("The command '%s' is not allowed by the server nor player, please enable at least one of them.", command_name))
error(format("The command %s is not allowed by the server nor player, please enable at least one of them.", command_name)) -- command.error_no_player_no_server when bug fixed
end
for index, argument_name in pairs(arguments) do
@ -134,145 +133,153 @@ function Command.add(command_name, options, callback)
argument_list = format('%s<%s> ', argument_list, argument_display)
end
local extra = ''
local extra = {''}
if allowed_by_server and not allowed_by_player then
extra = ' (Server only)'
extra = {'command.server_only'}
elseif allowed_by_player and (required_rank > Ranks.guest) then
extra = {'command.required_rank', get_rank_name(required_rank)}
elseif allowed_by_player and donator_only then
extra = ' (Donator only)'
extra = {'command.donator_only'}
end
local help_text = {'command.help_text_format',(custom_help_text or argument_list), description, extra}
local help_text = {'command.help_text_format', (custom_help_text or argument_list), description, extra}
commands.add_command(command_name, help_text, function (command)
local print -- custom print reference in case no player is present
local player = game.player
local player_name = player and player.valid and player.name or '<server>'
if not player or not player.valid then
print = log
commands.add_command(
command_name,
help_text,
function(command)
local print -- custom print reference in case no player is present
local player = game.player
local player_name = player and player.valid and player.name or '<server>'
if not player or not player.valid then
print = log
if not allowed_by_server then
print(format("The command '%s' is not allowed to be executed by the server.", command_name))
return
end
else
print = player.print
if not allowed_by_player then
print(format("The command '%s' is not allowed to be executed by players.", command_name))
return
end
if Rank.less_than(player_name, required_rank) then
print({'command.higher_rank_needed', command_name, get_rank_name(required_rank)})
return
end
if donator_only and not Donator.is_donator(player_name) then
print(format("The command '%s' is only allowed for donators.", command_name))
return
end
end
local named_arguments = {}
local from_command = {}
local raw_parameter_index = 1
for param in gmatch(command.parameter or '', '%S+') do
if capture_excess_arguments and raw_parameter_index == argument_list_size then
if not from_command[raw_parameter_index] then
from_command[raw_parameter_index] = param
else
from_command[raw_parameter_index] = from_command[raw_parameter_index] .. ' ' .. param
if not allowed_by_server then
print({'command.not_allowed_by_server', command_name})
return
end
else
from_command[raw_parameter_index] = param
raw_parameter_index = raw_parameter_index + 1
print = player.print
if not allowed_by_player then
print({'command.not_allowed_by_players', command_name})
return
end
if Rank.less_than(player_name, required_rank) then
print({'command.higher_rank_needed', command_name, get_rank_name(required_rank)})
return
end
if donator_only and not Donator.is_donator(player_name) then
print({'command.not_allowed_by_non_donators', command_name})
return
end
end
end
local errors = {}
local named_arguments = {}
local from_command = {}
local raw_parameter_index = 1
for param in gmatch(command.parameter or '', '%S+') do
if capture_excess_arguments and raw_parameter_index == argument_list_size then
if not from_command[raw_parameter_index] then
from_command[raw_parameter_index] = param
else
from_command[raw_parameter_index] = from_command[raw_parameter_index] .. ' ' .. param
end
else
from_command[raw_parameter_index] = param
raw_parameter_index = raw_parameter_index + 1
end
end
for index, argument in pairs(arguments) do
local parameter = from_command[index]
local errors = {}
if not parameter then
for default_value_name, default_value in pairs(default_values) do
if default_value_name == argument then
parameter = default_value
break
for index, argument in pairs(arguments) do
local parameter = from_command[index]
if not parameter then
for default_value_name, default_value in pairs(default_values) do
if default_value_name == argument then
parameter = default_value
break
end
end
end
if parameter == nil then
insert(errors, {'command.fail_missing_argument', argument, command_name})
else
named_arguments[argument] = parameter
end
end
if parameter == nil then
insert(errors, format('Argument "%s" from command %s is missing.', argument, command_name))
else
named_arguments[argument] = parameter
end
end
local return_early = false
local return_early = false
for _, error in pairs(errors) do
return_early = true
print(error)
end
if return_early then
return
end
if log_command then
local tick = 'pre-game'
if game then
tick = Utils.format_time(game.tick)
end
local server_time = Server.get_current_time()
if server_time then
server_time = format('(Server time: %s)', Timestamp.to_string(server_time))
else
server_time = ''
end
log(format('%s(Map time: %s) [%s Command] %s, used: %s %s', server_time, tick, (options.required_rank >= Ranks.admin) and 'Admin' or 'Player', player_name, command_name, serialize(named_arguments)))
end
local success, error = pcall(function ()
callback(named_arguments, player, command.tick)
end)
if not success then
local serialized_arguments = serialize(named_arguments)
if _DEBUG then
print(format("%s triggered an error running a command and has been logged: '%s' with arguments %s", player_name, command_name, serialized_arguments))
for _, error in pairs(errors) do
return_early = true
print(error)
ErrorLogging.generate_error_report(error)
end
if return_early then
return
end
print(format('There was an error running %s, it has been logged.', command_name))
local err = format("Error while running '%s' with arguments %s: %s", command_name, serialized_arguments, error)
log(err)
ErrorLogging.generate_error_report(err)
if log_command then
local tick = 'pre-game'
if game then
tick = Utils.format_time(game.tick)
end
local server_time = Server.get_current_time()
if server_time then
server_time = format('(Server time: %s)', Timestamp.to_string(server_time))
else
server_time = ''
end
log({'command.log_entry', server_time, tick, (options.required_rank >= Ranks.admin) and 'Admin' or 'Player', player_name, command_name, serialize(named_arguments)})
end
local success, error =
pcall(
function()
callback(named_arguments, player, command.tick)
end
)
if not success then
local serialized_arguments = serialize(named_arguments)
if _DEBUG then
print({'command.error_while_running_debug', player_name, command_name, serialized_arguments})
print(error)
ErrorLogging.generate_error_report(error)
return
end
print({'command.warn_player_of_error', command_name})
local err = {'command.error_log', command_name, serialized_arguments, error}
log(err)
ErrorLogging.generate_error_report(err)
end
end
end)
)
end
function Command.search(keyword)
local matches = {}
local count = 0
keyword = keyword:lower()
for name, description in pairs(commands.commands) do
local command = format('%s %s', name, description)
if match(command:lower(), keyword) then
-- commands use LocalisedString, which cannot be translated until player.print is called
for name in pairs(commands.commands) do
name = name
if match(name:lower(), keyword) then
count = count + 1
matches[count] = command
matches[count] = name
end
end
-- built-in commands use LocalisedString, which cannot be translated until player.print is called
for name in pairs(commands.game_commands) do
name = name
if match(name:lower(), keyword) then
@ -294,7 +301,7 @@ local function on_command(event)
if alternative then
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))
player.print({'command.warn_deprecated_command', event.command, alternative})
end
end
@ -310,8 +317,7 @@ end
--- Traps command errors if not in DEBUG.
if not _DEBUG then
local old_add_command = commands.add_command
commands.add_command = -- luacheck: ignore 122
function(name, desc, func)
commands.add_command = function(name, desc, func) -- luacheck: ignore 122
old_add_command(
name,
desc,
@ -319,7 +325,7 @@ if not _DEBUG then
local success, error = pcall(func, cmd)
if not success then
log(error)
Game.player_print('Sorry there was an error running ' .. cmd.name)
Game.player_print({'command.failed_command', cmd.name})
end
end
)

View File

@ -64,20 +64,20 @@ function Game.get_player_from_any(obj)
end
--- Prints to player or console.
-- @param str <string|table> table if locale is used
-- @param msg <string|table> table if locale is used
-- @param color <table> defaults to white
function Game.player_print(str, color)
function Game.player_print(msg, color)
color = color or Color.white
if game.player then
game.player.print(str, color)
game.player.print(msg, color)
else
print(str)
print(msg)
end
end
--[[
@param Position String to display at
@param text String to display
@param text <string|table> table if locale is used
@param color table in {r = 0~1, g = 0~1, b = 0~1}, defaults to white.
@param surface LuaSurface