mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-27 11:35:22 +09:00
Update all code styles
This commit is contained in:
@@ -45,7 +45,7 @@ end)
|
||||
add("string-options", function(input, _, options)
|
||||
local option = ExpUtil.auto_complete(options, input)
|
||||
if option == nil then
|
||||
return invalid{"exp-commands-parse.string-options", table.concat(options, ", ")}
|
||||
return invalid{ "exp-commands-parse.string-options", table.concat(options, ", ") }
|
||||
else
|
||||
return valid(option)
|
||||
end
|
||||
@@ -55,7 +55,7 @@ end)
|
||||
add("string-key", function(input, _, map)
|
||||
local option = ExpUtil.auto_complete(map, input, true)
|
||||
if option == nil then
|
||||
return invalid{"exp-commands-parse.string-options", table.concat(table.get_keys(map), ", ")}
|
||||
return invalid{ "exp-commands-parse.string-options", table.concat(table.get_keys(map), ", ") }
|
||||
else
|
||||
return valid(option)
|
||||
end
|
||||
@@ -64,7 +64,7 @@ end)
|
||||
--- A string with a maximum length, takes one argument which is the maximum length of a string
|
||||
add("string-max-length", function(input, _, maximum)
|
||||
if input:len() > maximum then
|
||||
return invalid{"exp-commands-parse.string-max-length", maximum}
|
||||
return invalid{ "exp-commands-parse.string-max-length", maximum }
|
||||
else
|
||||
return valid(input)
|
||||
end
|
||||
@@ -74,7 +74,7 @@ end)
|
||||
add("number", function(input)
|
||||
local number = tonumber(input)
|
||||
if number == nil then
|
||||
return invalid{"exp-commands-parse.number"}
|
||||
return invalid{ "exp-commands-parse.number" }
|
||||
else
|
||||
return valid(number)
|
||||
end
|
||||
@@ -84,7 +84,7 @@ end)
|
||||
add("integer", function(input)
|
||||
local number = tonumber(input)
|
||||
if number == nil then
|
||||
return invalid{"exp-commands-parse.number"}
|
||||
return invalid{ "exp-commands-parse.number" }
|
||||
else
|
||||
return valid(math.floor(number))
|
||||
end
|
||||
@@ -96,7 +96,7 @@ add("number-range", function(input, _, minimum, maximum)
|
||||
if not success then
|
||||
return status, number
|
||||
elseif number < minimum or number > maximum then
|
||||
return invalid{"exp-commands-parse.number-range", minimum, maximum}
|
||||
return invalid{ "exp-commands-parse.number-range", minimum, maximum }
|
||||
else
|
||||
return valid(number)
|
||||
end
|
||||
@@ -108,7 +108,7 @@ add("integer-range", function(input, _, minimum, maximum)
|
||||
if not success then
|
||||
return status, number
|
||||
elseif number < minimum or number > maximum then
|
||||
return invalid{"exp-commands-parse.number-range", minimum, maximum}
|
||||
return invalid{ "exp-commands-parse.number-range", minimum, maximum }
|
||||
else
|
||||
return valid(number)
|
||||
end
|
||||
@@ -118,7 +118,7 @@ end)
|
||||
add("player", function(input)
|
||||
local player = game.get_player(input)
|
||||
if player == nil then
|
||||
return invalid{"exp-commands-parse.player", input}
|
||||
return invalid{ "exp-commands-parse.player", input }
|
||||
else
|
||||
return valid(player)
|
||||
end
|
||||
@@ -130,7 +130,7 @@ add("player-online", function(input)
|
||||
if not success then
|
||||
return status, player
|
||||
elseif player.connected == false then
|
||||
return invalid{"exp-commands-parse.player-online"}
|
||||
return invalid{ "exp-commands-parse.player-online" }
|
||||
else
|
||||
return valid(player)
|
||||
end
|
||||
@@ -142,7 +142,7 @@ add("player-alive", function(input)
|
||||
if not success then
|
||||
return status, player
|
||||
elseif player.character == nil or player.character.health <= 0 then
|
||||
return invalid{"exp-commands-parse.player-alive"}
|
||||
return invalid{ "exp-commands-parse.player-alive" }
|
||||
else
|
||||
return valid(player)
|
||||
end
|
||||
@@ -152,7 +152,7 @@ end)
|
||||
add("force", function(input)
|
||||
local force = game.forces[input]
|
||||
if force == nil then
|
||||
return invalid{"exp-commands-parse.force"}
|
||||
return invalid{ "exp-commands-parse.force" }
|
||||
else
|
||||
return valid(force)
|
||||
end
|
||||
@@ -162,7 +162,7 @@ end)
|
||||
add("surface", function(input)
|
||||
local surface = game.surfaces[input]
|
||||
if surface == nil then
|
||||
return invalid{"exp-commands-parse.surface"}
|
||||
return invalid{ "exp-commands-parse.surface" }
|
||||
else
|
||||
return valid(surface)
|
||||
end
|
||||
@@ -172,7 +172,7 @@ end)
|
||||
add("color", function(input)
|
||||
local color = ExpUtil.auto_complete(Commands.color, input, true)
|
||||
if color == nil then
|
||||
return invalid{"exp-commands-parse.color"}
|
||||
return invalid{ "exp-commands-parse.color" }
|
||||
else
|
||||
return valid(color)
|
||||
end
|
||||
|
||||
@@ -34,7 +34,7 @@ local function format_as_pages(commands, page_size)
|
||||
page_length = 1
|
||||
end
|
||||
|
||||
local aliases = #command.aliases > 0 and {"exp-commands-help.aliases", table.concat(command.aliases, ", ")} or ""
|
||||
local aliases = #command.aliases > 0 and { "exp-commands-help.aliases", table.concat(command.aliases, ", ") } or ""
|
||||
pages[current_page][page_length] = { "exp-commands-help.format", command.name, command.description, command.help, aliases }
|
||||
end
|
||||
|
||||
@@ -42,40 +42,41 @@ local function format_as_pages(commands, page_size)
|
||||
end
|
||||
|
||||
Commands.new("commands", "List and search all commands for a keyword")
|
||||
:add_aliases{ "chelp", "helpp" }
|
||||
:argument("keyword", "string")
|
||||
:optional("page", "integer")
|
||||
:defaults{ page = 1 }
|
||||
:register(function(player, keyword, page)
|
||||
keyword = keyword:lower()
|
||||
local pages, found
|
||||
local cache = search_cache[player.index]
|
||||
if cache and cache.keyword == keyword then
|
||||
-- Cached value found, no search is needed
|
||||
pages = cache.pages
|
||||
found = cache.found
|
||||
else
|
||||
-- No cached value, so a search needs to be done
|
||||
local commands = Commands.search_for_player(keyword, player)
|
||||
pages, found = format_as_pages(commands, PAGE_SIZE)
|
||||
search_cache[player.index] = { keyword = keyword, pages = pages, found = found }
|
||||
end
|
||||
:add_aliases{ "chelp", "helpp" }
|
||||
:argument("keyword", "string")
|
||||
:optional("page", "integer")
|
||||
:defaults{ page = 1 }
|
||||
:register(function(player, keyword, page)
|
||||
keyword = keyword:lower()
|
||||
local pages, found
|
||||
local cache = search_cache[player.index]
|
||||
if cache and cache.keyword == keyword then
|
||||
-- Cached value found, no search is needed
|
||||
pages = cache.pages
|
||||
found = cache.found
|
||||
else
|
||||
-- No cached value, so a search needs to be done
|
||||
local commands = Commands.search_for_player(keyword, player)
|
||||
pages, found = format_as_pages(commands, PAGE_SIZE)
|
||||
search_cache[player.index] = { keyword = keyword, pages = pages, found = found }
|
||||
end
|
||||
|
||||
-- Error if no pages found
|
||||
if found == 0 then
|
||||
return Commands.status.success{ "exp-commands-help.no-results" }
|
||||
end
|
||||
-- Error if no pages found
|
||||
if found == 0 then
|
||||
return Commands.status.success{ "exp-commands-help.no-results" }
|
||||
end
|
||||
|
||||
local page_data = pages[page]
|
||||
if page_data == nil then
|
||||
-- Page number was out of range for this search
|
||||
return Commands.status.invalid_input{"exp-commands-help.out-of-range", page, #pages }
|
||||
end
|
||||
local page_data = pages[page]
|
||||
if page_data == nil then
|
||||
-- Page number was out of range for this search
|
||||
return Commands.status.invalid_input{ "exp-commands-help.out-of-range", page, #pages }
|
||||
end
|
||||
|
||||
-- Print selected page to the player
|
||||
Commands.print{ "exp-commands-help.header", keyword == '' and '<all>' or keyword }
|
||||
for _, command in pairs(page_data) do
|
||||
Commands.print(command)
|
||||
end
|
||||
return Commands.status.success{ "exp-commands-help.footer", found, page, #pages }
|
||||
end)
|
||||
-- Print selected page to the player
|
||||
Commands.print{ "exp-commands-help.header", keyword == "" and "<all>" or keyword }
|
||||
for _, command in pairs(page_data) do
|
||||
Commands.print(command)
|
||||
end
|
||||
|
||||
return Commands.status.success{ "exp-commands-help.footer", found, page, #pages }
|
||||
end)
|
||||
|
||||
@@ -10,16 +10,16 @@ local Commands = require("modules/exp_commands")
|
||||
local Clustorio = require("modules/clusterio/api")
|
||||
|
||||
Commands.new("_ipc", "Send an IPC message on the selected channel")
|
||||
:add_flags{ "system_only" }
|
||||
:enable_auto_concatenation()
|
||||
:argument("channel", "string")
|
||||
:argument("message", "string")
|
||||
:register(function(_, channel, message)
|
||||
local tbl = game.json_to_table(message)
|
||||
if tbl == nil then
|
||||
return Commands.status.invalid_input("Invalid json string")
|
||||
else
|
||||
Clustorio.send_json(channel, tbl)
|
||||
return Commands.status.success()
|
||||
end
|
||||
end)
|
||||
:add_flags{ "system_only" }
|
||||
:enable_auto_concatenation()
|
||||
:argument("channel", "string")
|
||||
:argument("message", "string")
|
||||
:register(function(_, channel, message)
|
||||
local tbl = game.json_to_table(message)
|
||||
if tbl == nil then
|
||||
return Commands.status.invalid_input("Invalid json string")
|
||||
else
|
||||
Clustorio.send_json(channel, tbl)
|
||||
return Commands.status.success()
|
||||
end
|
||||
end)
|
||||
|
||||
@@ -60,32 +60,32 @@ end
|
||||
|
||||
--- If a command has the flag "admin_only" then only admins can use the command#
|
||||
permission_authorities.admin_only =
|
||||
add(function(player, command)
|
||||
if command.flags.admin_only and not player.admin then
|
||||
return deny{"exp-commands-permissions.admin-only"}
|
||||
else
|
||||
return allow()
|
||||
end
|
||||
end)
|
||||
add(function(player, command)
|
||||
if command.flags.admin_only and not player.admin then
|
||||
return deny{ "exp-commands-permissions.admin-only" }
|
||||
else
|
||||
return allow()
|
||||
end
|
||||
end)
|
||||
|
||||
--- If a command has the flag "system_only" then only rcon connections can use the command
|
||||
permission_authorities.system_only =
|
||||
add(function(player, command)
|
||||
if command.flags.system_only and not system_players[player.name] then
|
||||
return deny{"exp-commands-permissions.system-only"}
|
||||
else
|
||||
return allow()
|
||||
end
|
||||
end)
|
||||
add(function(player, command)
|
||||
if command.flags.system_only and not system_players[player.name] then
|
||||
return deny{ "exp-commands-permissions.system-only" }
|
||||
else
|
||||
return allow()
|
||||
end
|
||||
end)
|
||||
|
||||
--- If Commands.disable was called then no one can use the command
|
||||
permission_authorities.disabled =
|
||||
add(function(_, command)
|
||||
if disabled_commands[command.name] then
|
||||
return deny{"exp-commands-permissions.disabled"}
|
||||
else
|
||||
return allow()
|
||||
end
|
||||
end)
|
||||
add(function(_, command)
|
||||
if disabled_commands[command.name] then
|
||||
return deny{ "exp-commands-permissions.disabled" }
|
||||
else
|
||||
return allow()
|
||||
end
|
||||
end)
|
||||
|
||||
return permission_authorities
|
||||
|
||||
@@ -31,10 +31,15 @@ rcon_statics.ipc = Clustorio.send_json
|
||||
|
||||
--- Some common callback values which are useful when a player uses the command
|
||||
function rcon_callbacks.player(player) return player end
|
||||
|
||||
function rcon_callbacks.surface(player) return player and player.surface end
|
||||
|
||||
function rcon_callbacks.force(player) return player and player.force end
|
||||
|
||||
function rcon_callbacks.position(player) return player and player.position end
|
||||
|
||||
function rcon_callbacks.entity(player) return player and player.selected end
|
||||
|
||||
function rcon_callbacks.tile(player) return player and player.surface.get_tile(player.position) end
|
||||
|
||||
--- The rcon env is saved between command runs to prevent desyncs
|
||||
@@ -55,28 +60,28 @@ function Commands.add_rcon_callback(name, callback)
|
||||
end
|
||||
|
||||
Commands.new("_rcon", "Execute arbitrary code within a custom environment")
|
||||
:add_flags{ "system_only" }
|
||||
:enable_auto_concatenation()
|
||||
:argument("invocation", "string")
|
||||
:register(function(player, invocation_string)
|
||||
-- Construct the environment the command will run within
|
||||
local env = setmetatable({}, { __index = rcon_env, __newindex = rcon_env })
|
||||
for name, callback in pairs(rcon_callbacks) do
|
||||
local _, rtn = pcall(callback, player.index > 0 and player or nil)
|
||||
rawset(env, name, rtn)
|
||||
end
|
||||
|
||||
-- Compile and run the invocation string
|
||||
local invocation, compile_error = load(invocation_string, "rcon-invocation", "t", env)
|
||||
if compile_error then
|
||||
return Commands.status.invalid_input(compile_error)
|
||||
else
|
||||
local success, rtn = xpcall(invocation, debug.traceback)
|
||||
if success == false then
|
||||
local err = rtn:gsub('%.%.%..-/temp/currently%-playing/', '')
|
||||
return Commands.status.error(err)
|
||||
else
|
||||
return Commands.status.success(rtn)
|
||||
:add_flags{ "system_only" }
|
||||
:enable_auto_concatenation()
|
||||
:argument("invocation", "string")
|
||||
:register(function(player, invocation_string)
|
||||
-- Construct the environment the command will run within
|
||||
local env = setmetatable({}, { __index = rcon_env, __newindex = rcon_env })
|
||||
for name, callback in pairs(rcon_callbacks) do
|
||||
local _, rtn = pcall(callback, player.index > 0 and player or nil)
|
||||
rawset(env, name, rtn)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
-- Compile and run the invocation string
|
||||
local invocation, compile_error = load(invocation_string, "rcon-invocation", "t", env)
|
||||
if compile_error then
|
||||
return Commands.status.invalid_input(compile_error)
|
||||
else
|
||||
local success, rtn = xpcall(invocation, debug.traceback)
|
||||
if success == false then
|
||||
local err = rtn:gsub("%.%.%..-/temp/currently%-playing/", "")
|
||||
return Commands.status.error(err)
|
||||
else
|
||||
return Commands.status.success(rtn)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
@@ -10,16 +10,16 @@ System command to execute a command as another player using their permissions (e
|
||||
local Commands = require("modules/exp_commands")
|
||||
|
||||
Commands.new("_sudo", "Run a command as another player")
|
||||
:add_flags{ "system_only" }
|
||||
:enable_auto_concatenation()
|
||||
:argument("player", "player")
|
||||
:argument("command", "string-key", Commands.registered_commands)
|
||||
:argument("arguments", "string")
|
||||
:register(function(_, player, command, parameter)
|
||||
return Commands._event_handler{
|
||||
name = command.name,
|
||||
tick = game.tick,
|
||||
player_index = player.index,
|
||||
parameter = parameter
|
||||
}
|
||||
end)
|
||||
:add_flags{ "system_only" }
|
||||
:enable_auto_concatenation()
|
||||
:argument("player", "player")
|
||||
:argument("command", "string-key", Commands.registered_commands)
|
||||
:argument("arguments", "string")
|
||||
:register(function(_, player, command, parameter)
|
||||
return Commands._event_handler{
|
||||
name = command.name,
|
||||
tick = game.tick,
|
||||
player_index = player.index,
|
||||
parameter = parameter,
|
||||
}
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user