mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-07-03 10:32:52 +00:00

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
139 lines
4.2 KiB
Java
139 lines
4.2 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.commandblock;
|
|
|
|
import dan200.computercraft.ComputerCraft;
|
|
import dan200.computercraft.api.lua.LuaFunction;
|
|
import dan200.computercraft.api.peripheral.IPeripheral;
|
|
import dan200.computercraft.shared.computer.apis.CommandAPI;
|
|
import dan200.computercraft.shared.util.CapabilityUtil;
|
|
import net.minecraft.tileentity.CommandBlockTileEntity;
|
|
import net.minecraft.tileentity.TileEntity;
|
|
import net.minecraft.util.Direction;
|
|
import net.minecraft.util.ResourceLocation;
|
|
import net.minecraftforge.common.capabilities.Capability;
|
|
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
|
import net.minecraftforge.common.util.LazyOptional;
|
|
import net.minecraftforge.event.AttachCapabilitiesEvent;
|
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
|
import net.minecraftforge.fml.common.Mod;
|
|
|
|
import javax.annotation.Nonnull;
|
|
import javax.annotation.Nullable;
|
|
|
|
import static dan200.computercraft.shared.Capabilities.CAPABILITY_PERIPHERAL;
|
|
|
|
/**
|
|
* This peripheral allows you to interact with command blocks.
|
|
*
|
|
* Command blocks are only wrapped as peripherals if the {@literal enable_command_block} option is true within the
|
|
* config.
|
|
*
|
|
* This API is <em>not</em> the same as the {@link CommandAPI} API, which is exposed on command computers.
|
|
*
|
|
* @cc.module command
|
|
*/
|
|
@Mod.EventBusSubscriber
|
|
public class CommandBlockPeripheral implements IPeripheral, ICapabilityProvider
|
|
{
|
|
private static final ResourceLocation CAP_ID = new ResourceLocation( ComputerCraft.MOD_ID, "command_block" );
|
|
|
|
private final CommandBlockTileEntity commandBlock;
|
|
private LazyOptional<IPeripheral> self;
|
|
|
|
public CommandBlockPeripheral( CommandBlockTileEntity commandBlock )
|
|
{
|
|
this.commandBlock = commandBlock;
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public String getType()
|
|
{
|
|
return "command";
|
|
}
|
|
|
|
/**
|
|
* Get the command this command block will run.
|
|
*
|
|
* @return The current command.
|
|
*/
|
|
@LuaFunction( mainThread = true )
|
|
public final String getCommand()
|
|
{
|
|
return commandBlock.getCommandBlockLogic().getCommand();
|
|
}
|
|
|
|
/**
|
|
* Set the command block's command.
|
|
*
|
|
* @param command The new command.
|
|
*/
|
|
@LuaFunction( mainThread = true )
|
|
public final void setCommand( String command )
|
|
{
|
|
commandBlock.getCommandBlockLogic().setCommand( command );
|
|
commandBlock.getCommandBlockLogic().updateCommand();
|
|
}
|
|
|
|
/**
|
|
* Execute the command block once.
|
|
*
|
|
* @return The result of executing.
|
|
* @cc.treturn boolean If the command completed successfully.
|
|
* @cc.treturn string|nil A failure message.
|
|
*/
|
|
@LuaFunction( mainThread = true )
|
|
public final Object[] runCommand()
|
|
{
|
|
commandBlock.getCommandBlockLogic().trigger( commandBlock.getWorld() );
|
|
int result = commandBlock.getCommandBlockLogic().getSuccessCount();
|
|
return result > 0 ? new Object[] { true } : new Object[] { false, "Command failed" };
|
|
}
|
|
|
|
@Override
|
|
public boolean equals( IPeripheral other )
|
|
{
|
|
return other != null && other.getClass() == getClass();
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public Object getTarget()
|
|
{
|
|
return commandBlock;
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public <T> LazyOptional<T> getCapability( @Nonnull Capability<T> cap, @Nullable Direction side )
|
|
{
|
|
if( cap == CAPABILITY_PERIPHERAL )
|
|
{
|
|
if( self == null ) self = LazyOptional.of( () -> this );
|
|
return self.cast();
|
|
}
|
|
return LazyOptional.empty();
|
|
}
|
|
|
|
private void invalidate()
|
|
{
|
|
self = CapabilityUtil.invalidate( self );
|
|
}
|
|
|
|
@SubscribeEvent
|
|
public static void onCapability( AttachCapabilitiesEvent<TileEntity> event )
|
|
{
|
|
TileEntity tile = event.getObject();
|
|
if( tile instanceof CommandBlockTileEntity )
|
|
{
|
|
CommandBlockPeripheral peripheral = new CommandBlockPeripheral( (CommandBlockTileEntity) tile );
|
|
event.addCapability( CAP_ID, peripheral );
|
|
event.addListener( peripheral::invalidate );
|
|
}
|
|
}
|
|
}
|