label )
{
diff --git a/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java b/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java
index ee37d3ba4..b132ad48c 100644
--- a/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java
+++ b/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java
@@ -22,6 +22,12 @@
import javax.annotation.Nullable;
import java.util.*;
+/**
+ * CC's "native" peripheral API. This is wrapped within CraftOS to provide a version which works with modems.
+ *
+ * @cc.module peripheral
+ * @hidden
+ */
public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChangeListener
{
private class PeripheralWrapper extends ComputerAccess
diff --git a/src/main/java/dan200/computercraft/core/apis/RedstoneAPI.java b/src/main/java/dan200/computercraft/core/apis/RedstoneAPI.java
index ce70fc5d2..ab096fd08 100644
--- a/src/main/java/dan200/computercraft/core/apis/RedstoneAPI.java
+++ b/src/main/java/dan200/computercraft/core/apis/RedstoneAPI.java
@@ -10,6 +10,48 @@
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.core.computer.ComputerSide;
+/**
+ * Interact with redstone attached to this computer.
+ *
+ * The {@link RedstoneAPI} library exposes three "types" of redstone control:
+ * - Binary input/output ({@link #setOutput}/{@link #getInput}): These simply check if a redstone wire has any input or
+ * output. A signal strength of 1 and 15 are treated the same.
+ * - Analogue input/output ({@link #setAnalogOutput}/{@link #getAnalogInput}): These work with the actual signal
+ * strength of the redstone wired, from 0 to 15.
+ * - Bundled cables ({@link #setBundledOutput}/{@link #getBundledInput}): These interact with "bundled" cables, such
+ * as those from Project:Red. These allow you to send 16 separate on/off signals. Each channel corresponds to a
+ * colour, with the first being @{colors.white} and the last @{colors.black}.
+ *
+ * Whenever a redstone input changes, a {@code redstone} event will be fired. This may be used instead of repeativly
+ * polling.
+ *
+ * This module may also be referred to as {@code rs}. For example, one may call {@code rs.getSides()} instead of
+ * {@link #getSides}.
+ *
+ * @cc.usage Toggle the redstone signal above the computer every 0.5 seconds.
+ *
+ *
+ * while true do
+ * redstone.setOutput("top", not redstone.getOutput("top"))
+ * sleep(0.5)
+ * end
+ *
+ * @cc.usage Mimic a redstone comparator in [subtraction mode][comparator].
+ *
+ *
+ * while true do
+ * local rear = rs.getAnalogueInput("back")
+ * local sides = math.max(rs.getAnalogueInput("left"), rs.getAnalogueInput("right"))
+ * rs.setAnalogueOutput("front", math.max(rear - sides, 0))
+ *
+ * os.pullEvent("redstone") -- Wait for a change to inputs.
+ * end
+ *
+ *
+ * [comparator]: https://minecraft.gamepedia.com/Redstone_Comparator#Subtract_signal_strength "Redstone Comparator on
+ * the Minecraft wiki."
+ * @cc.module redstone
+ */
public class RedstoneAPI implements ILuaAPI
{
private final IAPIEnvironment environment;
@@ -25,67 +67,145 @@ public String[] getNames()
return new String[] { "rs", "redstone" };
}
+ /**
+ * Returns a table containing the six sides of the computer. Namely, "top", "bottom", "left", "right", "front" and
+ * "back".
+ *
+ * @return A table of valid sides.
+ */
@LuaFunction
public final String[] getSides()
{
return ComputerSide.NAMES;
}
+ /**
+ * Turn the redstone signal of a specific side on or off.
+ *
+ * @param side The side to set.
+ * @param on Whether the redstone signal should be on or off. When on, a signal strength of 15 is emitted.
+ */
@LuaFunction
- public final void setOutput( ComputerSide side, boolean output )
+ public final void setOutput( ComputerSide side, boolean on )
{
- environment.setOutput( side, output ? 15 : 0 );
+ environment.setOutput( side, on ? 15 : 0 );
}
+ /**
+ * Get the current redstone output of a specific side.
+ *
+ * @param side The side to get.
+ * @return Whether the redstone output is on or off.
+ * @see #setOutput
+ */
@LuaFunction
public final boolean getOutput( ComputerSide side )
{
return environment.getOutput( side ) > 0;
}
+ /**
+ * Get the current redstone input of a specific side.
+ *
+ * @param side The side to get.
+ * @return Whether the redstone input is on or off.
+ */
@LuaFunction
public final boolean getInput( ComputerSide side )
{
return environment.getInput( side ) > 0;
}
+ /**
+ * Set the redstone signal strength for a specific side.
+ *
+ * @param side The side to set.
+ * @param value The signal strength between 0 and 15.
+ * @throws LuaException If {@code value} is not betwene 0 and 15.
+ */
@LuaFunction( { "setAnalogOutput", "setAnalogueOutput" } )
- public final void setAnalogOutput( ComputerSide side, int output ) throws LuaException
+ public final void setAnalogOutput( ComputerSide side, int value ) throws LuaException
{
- if( output < 0 || output > 15 ) throw new LuaException( "Expected number in range 0-15" );
- environment.setOutput( side, output );
+ if( value < 0 || value > 15 ) throw new LuaException( "Expected number in range 0-15" );
+ environment.setOutput( side, value );
}
+ /**
+ * Get the redstone output signal strength for a specific side.
+ *
+ * @param side The side to get.
+ * @return The output signal strength, between 0 and 15.
+ * @see #setAnalogOutput
+ */
@LuaFunction( { "getAnalogOutput", "getAnalogueOutput" } )
public final int getAnalogOutput( ComputerSide side )
{
return environment.getOutput( side );
}
+ /**
+ * Get the redstone input signal strength for a specific side.
+ *
+ * @param side The side to get.
+ * @return The input signal strength, between 0 and 15.
+ */
@LuaFunction( { "getAnalogInput", "getAnalogueInput" } )
public final int getAnalogInput( ComputerSide side )
{
return environment.getInput( side );
}
+ /**
+ * Set the bundled cable output for a specific side.
+ *
+ * @param side The side to set.
+ * @param output The colour bitmask to set.
+ * @cc.see colors.subtract For removing a colour from the bitmask.
+ * @cc.see colors.combine For adding a color to the bitmask.
+ */
@LuaFunction
public final void setBundledOutput( ComputerSide side, int output )
{
environment.setBundledOutput( side, output );
}
+ /**
+ * Get the bundled cable output for a specific side.
+ *
+ * @param side The side to get.
+ * @return The bundle cable's output.
+ */
@LuaFunction
public final int getBundledOutput( ComputerSide side )
{
return environment.getBundledOutput( side );
}
+ /**
+ * Get the bundled cable input for a specific side.
+ *
+ * @param side The side to get.
+ * @return The bundle cable's input.
+ * @see #testBundledInput To determine if a specific colour is set.
+ */
@LuaFunction
public final int getBundledInput( ComputerSide side )
{
return environment.getBundledOutput( side );
}
+ /**
+ * Determine if a specific combination of colours are on for the given side.
+ *
+ * @param side The side to test.
+ * @param mask The mask to test.
+ * @return If the colours are on.
+ * @cc.usage Check if @{colors.white} and @{colors.black} are on above the computer.
+ *
+ * print(redstone.testBundledInput("top", colors.combine(colors.white, colors.black)))
+ *
+ * @see #getBundledInput
+ */
@LuaFunction
public final boolean testBundledInput( ComputerSide side, int mask )
{
diff --git a/src/main/java/dan200/computercraft/core/apis/TermAPI.java b/src/main/java/dan200/computercraft/core/apis/TermAPI.java
index 195085331..ed05df142 100644
--- a/src/main/java/dan200/computercraft/core/apis/TermAPI.java
+++ b/src/main/java/dan200/computercraft/core/apis/TermAPI.java
@@ -14,6 +14,11 @@
import javax.annotation.Nonnull;
+/**
+ * The Terminal API provides functions for writing text to the terminal and monitors, and drawing ASCII graphics.
+ *
+ * @cc.module term
+ */
public class TermAPI extends TermMethods implements ILuaAPI
{
private final Terminal terminal;
@@ -31,11 +36,21 @@ public String[] getNames()
return new String[] { "term" };
}
+ /**
+ * Get the default palette value for a colour.
+ *
+ * @param colour The colour whose palette should be fetched.
+ * @return The RGB values.
+ * @throws LuaException When given an invalid colour.
+ * @cc.treturn number The red channel, will be between 0 and 1.
+ * @cc.treturn number The green channel, will be between 0 and 1.
+ * @cc.treturn number The blue channel, will be between 0 and 1.
+ */
@LuaFunction( { "nativePaletteColour", "nativePaletteColor" } )
- public final Object[] nativePaletteColour( int colourArg ) throws LuaException
+ public final Object[] nativePaletteColour( int colour ) throws LuaException
{
- int colour = 15 - parseColour( colourArg );
- Colour c = Colour.fromInt( colour );
+ int actualColour = 15 - parseColour( colour );
+ Colour c = Colour.fromInt( actualColour );
float[] rgb = c.getRGB();
diff --git a/src/main/java/dan200/computercraft/core/apis/TermMethods.java b/src/main/java/dan200/computercraft/core/apis/TermMethods.java
index d7674ef3f..c55c59f7a 100644
--- a/src/main/java/dan200/computercraft/core/apis/TermMethods.java
+++ b/src/main/java/dan200/computercraft/core/apis/TermMethods.java
@@ -18,6 +18,9 @@
/**
* A base class for all objects which interact with a terminal. Namely the {@link TermAPI} and monitors.
+ *
+ * @cc.module term.Redirect
+ * @hidden
*/
public abstract class TermMethods
{
diff --git a/src/main/java/dan200/computercraft/core/apis/http/websocket/WebsocketHandle.java b/src/main/java/dan200/computercraft/core/apis/http/websocket/WebsocketHandle.java
index a6436a785..ce8684002 100644
--- a/src/main/java/dan200/computercraft/core/apis/http/websocket/WebsocketHandle.java
+++ b/src/main/java/dan200/computercraft/core/apis/http/websocket/WebsocketHandle.java
@@ -25,6 +25,12 @@
import static dan200.computercraft.core.apis.http.websocket.Websocket.CLOSE_EVENT;
import static dan200.computercraft.core.apis.http.websocket.Websocket.MESSAGE_EVENT;
+/**
+ * A websocket, which can be used to send an receive messages with a web server.
+ *
+ * @cc.module http.Websocket
+ * @see dan200.computercraft.core.apis.HTTPAPI#websocket On how to open a websocket.
+ */
public class WebsocketHandle implements Closeable
{
private final Websocket websocket;
@@ -40,8 +46,18 @@ public WebsocketHandle( Websocket websocket, Options options, Channel channel )
this.channel = channel;
}
+ /**
+ * Wait for a message from the server.
+ *
+ * @param timeout The number of seconds to wait if no message is received.
+ * @return The result of receiving.
+ * @throws LuaException If the websocket has been closed.
+ * @cc.treturn [1] string The received message.
+ * @cc.treturn boolean If this was a binary message.
+ * @cc.treturn [2] nil If the websocket was closed while waiting, or if we timed out.
+ */
@LuaFunction
- public final MethodResult result( Optional timeout ) throws LuaException
+ public final MethodResult receive( Optional timeout ) throws LuaException
{
checkOpen();
int timeoutId = timeout.isPresent()
@@ -51,29 +67,40 @@ public final MethodResult result( Optional timeout ) throws LuaException
return new ReceiveCallback( timeoutId ).pull;
}
+ /**
+ * Send a websocket message to the connected server.
+ *
+ * @param message The message to send.
+ * @param binary Whether this message should be treated as a
+ * @throws LuaException If the message is too large.
+ * @throws LuaException If the websocket has been closed.
+ */
@LuaFunction
- public final void send( IArguments args ) throws LuaException
+ public final void send( Object message, Optional binary ) throws LuaException
{
checkOpen();
- String text = StringUtil.toString( args.get( 0 ) );
+ String text = StringUtil.toString( message );
if( options.websocketMessage != 0 && text.length() > options.websocketMessage )
{
throw new LuaException( "Message is too large" );
}
- boolean binary = args.optBoolean( 1, false );
websocket.environment().addTrackingChange( TrackingField.WEBSOCKET_OUTGOING, text.length() );
Channel channel = this.channel;
if( channel != null )
{
- channel.writeAndFlush( binary
+ channel.writeAndFlush( binary.orElse( false )
? new BinaryWebSocketFrame( Unpooled.wrappedBuffer( LuaValues.encode( text ) ) )
: new TextWebSocketFrame( text ) );
}
}
+ /**
+ * Close this websocket. This will terminate the connection, meaning messages can no longer be sent or received
+ * along it.
+ */
@LuaFunction( "close" )
public final void doClose()
{
diff --git a/src/main/java/dan200/computercraft/core/asm/Generator.java b/src/main/java/dan200/computercraft/core/asm/Generator.java
index 0456cc51a..2e80cd86c 100644
--- a/src/main/java/dan200/computercraft/core/asm/Generator.java
+++ b/src/main/java/dan200/computercraft/core/asm/Generator.java
@@ -194,9 +194,9 @@ private Optional build( Method method )
if( bytes == null ) return Optional.empty();
Class> klass = DeclaringClassLoader.INSTANCE.define( className, bytes, method.getDeclaringClass().getProtectionDomain() );
- return Optional.of( klass.asSubclass( base ).newInstance() );
+ return Optional.of( klass.asSubclass( base ).getDeclaredConstructor().newInstance() );
}
- catch( InstantiationException | IllegalAccessException | ClassFormatError | RuntimeException e )
+ catch( ReflectiveOperationException | ClassFormatError | RuntimeException e )
{
ComputerCraft.log.error( "Error generating wrapper for {}.", name, e );
return Optional.empty();
diff --git a/src/main/java/dan200/computercraft/shared/computer/apis/CommandAPI.java b/src/main/java/dan200/computercraft/shared/computer/apis/CommandAPI.java
index 53e8fbc21..fd310da23 100644
--- a/src/main/java/dan200/computercraft/shared/computer/apis/CommandAPI.java
+++ b/src/main/java/dan200/computercraft/shared/computer/apis/CommandAPI.java
@@ -23,6 +23,9 @@
import java.util.*;
+/**
+ * @cc.module commands
+ */
public class CommandAPI implements ILuaAPI
{
private final TileCommandComputer computer;
@@ -78,18 +81,62 @@ private Object[] doCommand( String command )
return table;
}
+ /**
+ * Execute a specific command.
+ *
+ * @param command The command to execute.
+ * @return See {@code cc.treturn}.
+ * @cc.treturn boolean Whether the command executed successfully.
+ * @cc.treturn { string... } The output of this command, as a list of lines.
+ * @cc.treturn number|nil The number of "affected" objects, or `nil` if the command failed. The definition of this
+ * varies from command to command.
+ * @cc.usage Set the block above the command computer to stone.
+ *
+ * commands.exec("setblock ~ ~1 ~ minecraft:stone")
+ *
+ */
@LuaFunction( mainThread = true )
public final Object[] exec( String command )
{
return doCommand( command );
}
+ /**
+ * Asynchronously execute a command.
+ *
+ * Unlike {@link #exec}, this will immediately return, instead of waiting for the
+ * command to execute. This allows you to run multiple commands at the same
+ * time.
+ *
+ * When this command has finished executing, it will queue a `task_complete`
+ * event containing the result of executing this command (what {@link #exec} would
+ * return).
+ *
+ * @param context The context this command executes under.
+ * @param command The command to execute.
+ * @return The "task id". When this command has been executed, it will queue a `task_complete` event with a matching id.
+ * @throws LuaException (hidden) If the task cannot be created.
+ * @cc.tparam string command The command to execute.
+ * @cc.usage Asynchronously sets the block above the computer to stone.
+ *
+ * commands.execAsync("~ ~1 ~ minecraft:stone")
+ *
+ * @cc.see parallel One may also use the parallel API to run multiple commands at once.
+ */
@LuaFunction
public final long execAsync( ILuaContext context, String command ) throws LuaException
{
return context.issueMainThreadTask( () -> doCommand( command ) );
}
+ /**
+ * List all available commands which the computer has permission to execute.
+ *
+ * @param args Arguments to this function.
+ * @return A list of all available commands
+ * @throws LuaException (hidden) On non-string arguments.
+ * @cc.tparam string ... The sub-command to complete.
+ */
@LuaFunction( mainThread = true )
public final List list( IArguments args ) throws LuaException
{
@@ -112,6 +159,15 @@ public final List list( IArguments args ) throws LuaException
return result;
}
+ /**
+ * Get the position of the current command computer.
+ *
+ * @return The block's position.
+ * @cc.treturn number This computer's x position.
+ * @cc.treturn number This computer's y position.
+ * @cc.treturn number This computer's z position.
+ * @cc.see gps.locate To get the position of a non-command computer.
+ */
@LuaFunction
public final Object[] getBlockPosition()
{
@@ -120,6 +176,25 @@ public final Object[] getBlockPosition()
return new Object[] { pos.getX(), pos.getY(), pos.getZ() };
}
+ /**
+ * Get information about a range of blocks.
+ *
+ * This returns the same information as @{getBlockInfo}, just for multiple
+ * blocks at once.
+ *
+ * Blocks are traversed by ascending y level, followed by z and x - the returned
+ * table may be indexed using `x + z*width + y*depth*depth`.
+ *
+ * @param minX The start x coordinate of the range to query.
+ * @param minY The start y coordinate of the range to query.
+ * @param minZ The start z coordinate of the range to query.
+ * @param maxX The end x coordinate of the range to query.
+ * @param maxY The end y coordinate of the range to query.
+ * @param maxZ The end z coordinate of the range to query.
+ * @return A list of information about each block.
+ * @throws LuaException If the coordinates are not within the world.
+ * @throws LuaException If trying to get information about more than 4096 blocks.
+ */
@LuaFunction( mainThread = true )
public final List