1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-07-17 09:22:53 +00:00
Jonathan Coates ef8da8054f
An initial stab at documentation generation (#360)
This adds documentation comments to many of CC's Lua APIs, and
a couple of the Java ones, through the use of stubs. We then
export these to HTML using illuaminate [1] and upload them to our
documentation site [2].

Uploads currently occur on pushes to master and any release/tag. The
site is entirely static - there is no way to switch between versions,
etc... but hopefully we can improve this in the future.

[1]: github.com/SquidDev/illuaminate/
[2]: https://tweaked.cc/
2020-04-10 10:27:53 +01:00

69 lines
2.2 KiB
Lua

--- The commands API allows your system to directly execute [Minecraft
-- commands][mc] and gather data from the results.
--
-- While one may use @{commands.exec} directly to execute a command, the
-- commands API also provides helper methods to execute every command. For
-- instance, `commands.say("Hi!")` is equivalent to `commands.exec("say Hi!")`.
--
-- @{commands.async} provides a similar interface to execute asynchronous
-- commands. `commands.async.say("Hi!")` is equivalent to
-- `commands.execAsync("Hi!")`.
--
-- [mc]: https://minecraft.gamepedia.com/Commands
--
-- @module commands
if not commands then
error( "Cannot load command API on normal computer", 2 )
end
--- The builtin commands API, without any generated command helper functions
--
-- This may be useful if a built-in function (such as @{commands.list}) has been
-- overwritten by a command.
native = commands.native or commands
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 )
else
error( "Expected string, number, boolean or table", 3 )
end
end
return table.concat(args, " ")
end
-- Put native functions into the environment
local env = _ENV
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,
}
local tCommands = native.list()
for _, sCommandName in ipairs(tCommands) do
if env[ sCommandName ] == nil then
local bJSONIsNBT = tNonNBTJSONCommands[ sCommandName ] == nil
env[ sCommandName ] = function( ... )
local sCommand = collapseArgs( bJSONIsNBT, sCommandName, ... )
return native.exec( sCommand )
end
tAsync[ sCommandName ] = function( ... )
local sCommand = collapseArgs( bJSONIsNBT, sCommandName, ... )
return native.execAsync( sCommand )
end
end
end
env.async = tAsync