1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-07-03 10:32:52 +00:00
SquidDev 9f8774960f Generate documentation stubs from Javadocs
illuaminate does not handle Java files, for obvious reasons. In order to
get around that, we have a series of stub files within /doc/stub which
mirrored the Java ones. While this works, it has a few problems:

 - The link to source code does not work - it just links to the stub
   file.
 - There's no guarantee that documentation remains consistent with the
   Java code. This change found several methods which were incorrectly
   documented beforehand.

We now replace this with a custom Java doclet[1], which extracts doc
comments from @LuaFunction annotated methods and generates stub-files
from them. These also contain a @source annotation, which allows us to
correctly link them back to the original Java code.

There's some issues with this which have yet to be fixed. However, I
don't think any of them are major blockers right now:

 - The custom doclet relies on Java 9 - I think it's /technically/
   possible to do this on Java 8, but the API is significantly uglier.
   This means that we need to run javadoc on a separate JVM.

   This is possible, and it works locally and on CI, but is definitely
   not a nice approach.

 - illuaminate now requires the doc stubs to be generated in order for
   the linter to pass, which does make running the linter locally much
   harder (especially given the above bullet point).

   We could notionally include the generated stubs (or at least a cut
   down version of them) in the repo, but I'm not 100% sure about that.

[1]: https://docs.oracle.com/javase/9/docs/api/jdk/javadoc/doclet/package-summary.html
2020-07-03 13:31:26 +01:00

129 lines
3.7 KiB
Java

/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2020. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.peripheral.monitor;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.LuaValues;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.apis.TermMethods;
import dan200.computercraft.core.terminal.Terminal;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* Monitors are a block which act as a terminal, displaying information on one side. This allows them to be read and
* interacted with in-world without opening a GUI.
*
* Monitors act as @{term.Redirect|terminal redirects} and so expose the same methods, as well as several additional
* ones, which are documented below.
*
* Like computers, monitors come in both normal (no colour) and advanced (colour) varieties.
*
* @cc.module monitor
* @cc.usage Write "Hello, world!" to an adjacent monitor:
*
* <pre>
* local monitor = peripheral.find("monitor")
* monitor.setCursorPos(1, 1)
* monitor.write("Hello, world!")
* </pre>
*/
public class MonitorPeripheral extends TermMethods implements IPeripheral
{
private final TileMonitor monitor;
public MonitorPeripheral( TileMonitor monitor )
{
this.monitor = monitor;
}
@Nonnull
@Override
public String getType()
{
return "monitor";
}
/**
* Set the scale of this monitor. A larger scale will result in the monitor having a lower resolution, but display
* text much larger.
*
* @param scaleArg The monitor's scale. This must be a multiple of 0.5 between 0.5 and 5.
* @throws LuaException If the scale is out of range.
* @see #getTextScale()
*/
@LuaFunction
public final void setTextScale( double scaleArg ) throws LuaException
{
int scale = (int) (LuaValues.checkFinite( 0, scaleArg ) * 2.0);
if( scale < 1 || scale > 10 ) throw new LuaException( "Expected number in range 0.5-5" );
getMonitor().setTextScale( scale );
}
/**
* Get the monitor's current text scale.
*
* @return The monitor's current scale.
* @throws LuaException If the monitor cannot be found.
*/
@LuaFunction
public final double getTextScale() throws LuaException
{
return getMonitor().getTextScale() / 2.0;
}
@Override
public void attach( @Nonnull IComputerAccess computer )
{
monitor.addComputer( computer );
}
@Override
public void detach( @Nonnull IComputerAccess computer )
{
monitor.removeComputer( computer );
}
@Override
public boolean equals( IPeripheral other )
{
return other instanceof MonitorPeripheral && monitor == ((MonitorPeripheral) other).monitor;
}
@Nonnull
private ServerMonitor getMonitor() throws LuaException
{
ServerMonitor monitor = this.monitor.getCachedServerMonitor();
if( monitor == null ) throw new LuaException( "Monitor has been detached" );
return monitor;
}
@Nonnull
@Override
public Terminal getTerminal() throws LuaException
{
Terminal terminal = getMonitor().getTerminal();
if( terminal == null ) throw new LuaException( "Monitor has been detached" );
return terminal;
}
@Override
public boolean isColour() throws LuaException
{
return getMonitor().isColour();
}
@Nullable
@Override
public Object getTarget()
{
return monitor;
}
}