mirror of
https://github.com/Refactorio/RedMew.git
synced 2024-12-12 10:04:40 +02:00
grilled's suggestions
This commit is contained in:
parent
c2b3fcc4f7
commit
7d7485e5b4
@ -8,6 +8,7 @@ local Event = require 'utils.event'
|
||||
local Command = require 'utils.command'
|
||||
|
||||
local format = string.format
|
||||
local loadstring = loadstring
|
||||
|
||||
--- A table of players with tpmode turned on
|
||||
global.tp_players = {}
|
||||
@ -29,12 +30,21 @@ local function silent_command(args, player)
|
||||
local func, err = loadstring(args.str)
|
||||
if not func then
|
||||
p(err)
|
||||
return
|
||||
end
|
||||
|
||||
local _, err2 = pcall(func)
|
||||
if err2 then
|
||||
local i = err2:find('\n')
|
||||
p(err2:sub(1, i))
|
||||
if i then
|
||||
p(err2:sub(1, i))
|
||||
return
|
||||
end
|
||||
|
||||
i = err2:find('%s')
|
||||
if i then
|
||||
p(err2:sub(i + 1))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -398,7 +398,8 @@ Command.add('redmew-setting-set', {
|
||||
end)
|
||||
|
||||
if not success then
|
||||
player.print(data.message)
|
||||
local i = data:find('%s')
|
||||
player.print(data:sub(i + 1))
|
||||
return
|
||||
end
|
||||
|
||||
@ -416,7 +417,8 @@ Command.add('redmew-setting-get', {
|
||||
end)
|
||||
|
||||
if not success then
|
||||
player.print(data.message)
|
||||
local i = data:find('%s')
|
||||
player.print(data:sub(i + 1))
|
||||
return
|
||||
end
|
||||
|
||||
|
@ -74,6 +74,8 @@ Global.register(memory, function (tbl) memory = tbl end)
|
||||
|
||||
local Public = {}
|
||||
|
||||
Public.types = {fraction = 'fraction', string = 'string', boolean = 'boolean'}
|
||||
|
||||
---Register a specific setting with a sensitization setting type.
|
||||
---
|
||||
--- Available setting types:
|
||||
@ -81,23 +83,23 @@ local Public = {}
|
||||
--- - string a string or anything that can be cast to a string
|
||||
--- - boolean, 1, 0, yes, no, true, false or an empty string for false
|
||||
---
|
||||
--- This function can only be called before the game is initialized.
|
||||
--- This function must be called in the control stage, i.e. not inside an event.
|
||||
---
|
||||
---@param name string
|
||||
---@param setting_type string
|
||||
---@param default mixed
|
||||
function Public.register(name, setting_type, default)
|
||||
if game then
|
||||
error(format('You can only register setting names before the game is initialized. Tried setting "%s" with type "%s".', name, setting_type))
|
||||
error(format('You can only register setting names in the control stage, i.e. not inside events. Tried setting "%s" with type "%s".', name, setting_type), 2)
|
||||
end
|
||||
|
||||
if settings[name] then
|
||||
error(format('Trying to register setting for "%s" while it has already been registered.', name))
|
||||
error(format('Trying to register setting for "%s" while it has already been registered.', name), 2)
|
||||
end
|
||||
|
||||
local callback = settings_type[setting_type]
|
||||
if not callback then
|
||||
error(format('Trying to register setting for "%s" with type "%s" while this type does not exist.', name, setting_type))
|
||||
error(format('Trying to register setting for "%s" with type "%s" while this type does not exist.', name, setting_type), 2)
|
||||
end
|
||||
|
||||
local setting = {
|
||||
@ -120,13 +122,13 @@ end
|
||||
function Public.set(player_index, name, value)
|
||||
local setting = settings[name]
|
||||
if not setting then
|
||||
return error({message = format('Setting "%s" does not exist.', name)})
|
||||
return error(format('Setting "%s" does not exist.', name), 2)
|
||||
end
|
||||
|
||||
local success, sanitized_value = setting.callback(value)
|
||||
|
||||
if not success then
|
||||
error({message = format('Setting "%s" failed: %s', name, sanitized_value)})
|
||||
error(format('Setting "%s" failed: %s', name, sanitized_value), 2)
|
||||
end
|
||||
|
||||
local player_settings = memory[player_index]
|
||||
@ -149,7 +151,7 @@ end
|
||||
function Public.get(player_index, name)
|
||||
local setting = settings[name]
|
||||
if not setting then
|
||||
return error({message = format('Setting "%s" does not exist.', name)})
|
||||
return error(format('Setting "%s" does not exist.', name), 2)
|
||||
end
|
||||
|
||||
local player_settings = memory[player_index]
|
||||
|
Loading…
Reference in New Issue
Block a user