mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-11-17 13:47:11 +00:00
Some cleanup to the Lua programs
This commit is contained in:
@@ -14,7 +14,7 @@
|
||||
-- @module commands
|
||||
|
||||
if not commands then
|
||||
error( "Cannot load command API on normal computer", 2 )
|
||||
error("Cannot load command API on normal computer", 2)
|
||||
end
|
||||
|
||||
--- The builtin commands API, without any generated command helper functions
|
||||
@@ -23,16 +23,16 @@ end
|
||||
-- overwritten by a command.
|
||||
local native = commands.native or commands
|
||||
|
||||
local function collapseArgs( bJSONIsNBT, ... )
|
||||
local function collapseArgs(bJSONIsNBT, ...)
|
||||
local args = table.pack(...)
|
||||
for i = 1, #args do
|
||||
local arg = args[i]
|
||||
if type(arg) == "boolean" or type(arg) == "number" or type(arg) == "string" then
|
||||
args[i] = tostring(arg)
|
||||
elseif type(arg) == "table" then
|
||||
args[i] = textutils.serialiseJSON( arg, bJSONIsNBT )
|
||||
args[i] = textutils.serialiseJSON(arg, bJSONIsNBT)
|
||||
else
|
||||
error( "Expected string, number, boolean or table", 3 )
|
||||
error("Expected string, number, boolean or table", 3)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -42,22 +42,22 @@ end
|
||||
-- Put native functions into the environment
|
||||
local env = _ENV
|
||||
env.native = native
|
||||
for k,v in pairs( native ) do
|
||||
for k, v in pairs(native) do
|
||||
env[k] = v
|
||||
end
|
||||
|
||||
-- Create wrapper functions for all the commands
|
||||
local tAsync = {}
|
||||
local tNonNBTJSONCommands = {
|
||||
[ "tellraw" ] = true,
|
||||
[ "title" ] = true
|
||||
["tellraw"] = true,
|
||||
["title"] = true,
|
||||
}
|
||||
|
||||
local command_mt = {}
|
||||
function command_mt.__call(self, ...)
|
||||
local meta = self[command_mt]
|
||||
local sCommand = collapseArgs( meta.json, table.concat(meta.name, " "), ... )
|
||||
return meta.func( sCommand )
|
||||
local sCommand = collapseArgs(meta.json, table.concat(meta.name, " "), ...)
|
||||
return meta.func(sCommand)
|
||||
end
|
||||
|
||||
function command_mt.__tostring(self)
|
||||
@@ -65,6 +65,16 @@ function command_mt.__tostring(self)
|
||||
return ("command %q"):format("/" .. table.concat(meta.name, " "))
|
||||
end
|
||||
|
||||
local function mk_command(name, json, func)
|
||||
return setmetatable({
|
||||
[command_mt] = {
|
||||
name = name,
|
||||
func = func,
|
||||
json = json,
|
||||
},
|
||||
}, command_mt)
|
||||
end
|
||||
|
||||
function command_mt.__index(self, key)
|
||||
local meta = self[command_mt]
|
||||
if meta.children then return nil end
|
||||
@@ -74,31 +84,17 @@ function command_mt.__index(self, key)
|
||||
for _, child in ipairs(native.list(table.unpack(name))) do
|
||||
local child_name = { table.unpack(name) }
|
||||
child_name[#child_name + 1] = child
|
||||
|
||||
self[child] = setmetatable({ [command_mt] = {
|
||||
name = child_name,
|
||||
func = meta.func,
|
||||
json = meta.json
|
||||
} }, command_mt)
|
||||
self[child] = mk_command(child_name, meta.json, meta.func)
|
||||
end
|
||||
|
||||
return self[key]
|
||||
end
|
||||
|
||||
for _, sCommandName in ipairs(native.list()) do
|
||||
if env[ sCommandName ] == nil then
|
||||
local bJSONIsNBT = tNonNBTJSONCommands[ sCommandName ] == nil
|
||||
env[ sCommandName ] = setmetatable({ [command_mt] = {
|
||||
name = { sCommandName },
|
||||
func = native.exec,
|
||||
json = bJSONIsNBT
|
||||
} }, command_mt)
|
||||
|
||||
tAsync[ sCommandName ] = setmetatable({ [command_mt] = {
|
||||
name = { sCommandName },
|
||||
func = native.execAsync,
|
||||
json = bJSONIsNBT
|
||||
} }, command_mt)
|
||||
if env[sCommandName] == nil then
|
||||
local bJSONIsNBT = tNonNBTJSONCommands[sCommandName] == nil
|
||||
env[sCommandName] = mk_command({ sCommandName }, bJSONIsNBT, native.exec)
|
||||
tAsync[sCommandName] = mk_command({ sCommandName }, bJSONIsNBT, native.execAsync)
|
||||
end
|
||||
end
|
||||
env.async = tAsync
|
||||
|
||||
@@ -131,7 +131,7 @@ tKeys[346] = 'rightAlt'
|
||||
-- tKeys[348] = 'menu'
|
||||
|
||||
local keys = _ENV
|
||||
for nKey, sKey in pairs( tKeys ) do
|
||||
for nKey, sKey in pairs(tKeys) do
|
||||
keys[sKey] = nKey
|
||||
end
|
||||
|
||||
@@ -145,7 +145,7 @@ keys.cimcumflex = keys.circumflex --- @local
|
||||
--
|
||||
-- @tparam number code The key code to look up.
|
||||
-- @treturn string|nil The name of the key, or `nil` if not a valid key code.
|
||||
function getName( _nKey )
|
||||
function getName(_nKey)
|
||||
expect(1, _nKey, "number")
|
||||
return tKeys[ _nKey ]
|
||||
return tKeys[_nKey]
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user