2019-01-01 01:10:18 +00:00
|
|
|
/*
|
|
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
|
|
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
|
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
|
|
*/
|
|
|
|
|
2017-12-06 15:51:02 +00:00
|
|
|
package dan200.computercraft.shared.command;
|
|
|
|
|
2018-02-13 11:45:13 +00:00
|
|
|
import com.google.common.collect.Sets;
|
2017-12-06 15:51:02 +00:00
|
|
|
import dan200.computercraft.ComputerCraft;
|
|
|
|
import dan200.computercraft.api.peripheral.IPeripheral;
|
2019-02-11 16:21:51 +00:00
|
|
|
import dan200.computercraft.core.apis.IAPIEnvironment;
|
2017-12-06 15:51:02 +00:00
|
|
|
import dan200.computercraft.core.computer.Computer;
|
2018-04-16 20:06:16 +00:00
|
|
|
import dan200.computercraft.core.tracking.ComputerTracker;
|
|
|
|
import dan200.computercraft.core.tracking.Tracking;
|
|
|
|
import dan200.computercraft.core.tracking.TrackingContext;
|
|
|
|
import dan200.computercraft.core.tracking.TrackingField;
|
2019-01-05 20:12:02 +00:00
|
|
|
import dan200.computercraft.shared.Config;
|
2017-12-06 15:51:02 +00:00
|
|
|
import dan200.computercraft.shared.command.framework.*;
|
2018-12-29 12:18:05 +00:00
|
|
|
import dan200.computercraft.shared.command.text.TableBuilder;
|
2018-11-29 11:57:52 +00:00
|
|
|
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
2017-12-06 15:51:02 +00:00
|
|
|
import dan200.computercraft.shared.computer.core.ServerComputer;
|
2018-04-16 20:06:16 +00:00
|
|
|
import net.minecraft.command.CommandBase;
|
2017-12-06 15:51:02 +00:00
|
|
|
import net.minecraft.command.CommandException;
|
|
|
|
import net.minecraft.command.ICommandSender;
|
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
2018-01-20 11:07:09 +00:00
|
|
|
import net.minecraft.server.MinecraftServer;
|
2017-12-06 15:51:02 +00:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.text.ITextComponent;
|
2018-02-02 13:34:27 +00:00
|
|
|
import net.minecraft.util.text.TextComponentString;
|
2017-12-06 15:51:02 +00:00
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
2018-02-02 13:34:27 +00:00
|
|
|
import java.util.*;
|
2018-08-04 09:39:44 +00:00
|
|
|
import java.util.function.Consumer;
|
2017-12-06 15:51:02 +00:00
|
|
|
|
2018-12-29 12:18:05 +00:00
|
|
|
import static dan200.computercraft.shared.command.text.ChatHelpers.*;
|
2017-12-06 15:51:02 +00:00
|
|
|
|
|
|
|
public final class CommandComputerCraft extends CommandDelegate
|
|
|
|
{
|
2018-04-16 20:06:16 +00:00
|
|
|
public static final UUID SYSTEM_UUID = new UUID( 0, 0 );
|
|
|
|
|
2018-05-15 10:44:23 +00:00
|
|
|
private static final int DUMP_LIST_ID = 5373952;
|
|
|
|
private static final int DUMP_SINGLE_ID = 1844510720;
|
|
|
|
private static final int TRACK_ID = 373882880;
|
|
|
|
|
2017-12-06 15:51:02 +00:00
|
|
|
public CommandComputerCraft()
|
|
|
|
{
|
|
|
|
super( create() );
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ISubCommand create()
|
|
|
|
{
|
2019-03-16 10:26:40 +00:00
|
|
|
CommandRoot root = new CommandRoot( "computercraft" );
|
2017-12-06 15:51:02 +00:00
|
|
|
|
2019-03-16 10:26:40 +00:00
|
|
|
root.register( new SubCommandBase( "dump", UserLevel.OWNER_OP )
|
2017-12-06 15:51:02 +00:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
|
|
|
{
|
|
|
|
if( arguments.size() == 0 )
|
|
|
|
{
|
2018-12-29 12:18:05 +00:00
|
|
|
TableBuilder table = new TableBuilder( DUMP_LIST_ID, "Computer", "On", "Position" );
|
2017-12-06 15:51:02 +00:00
|
|
|
|
2018-01-20 11:07:09 +00:00
|
|
|
List<ServerComputer> computers = new ArrayList<>( ComputerCraft.serverComputerRegistry.getComputers() );
|
|
|
|
|
|
|
|
// Unless we're on a server, limit the number of rows we can send.
|
|
|
|
if( !(context.getSender() instanceof MinecraftServer) )
|
|
|
|
{
|
|
|
|
World world = context.getSender().getEntityWorld();
|
|
|
|
BlockPos pos = context.getSender().getPosition();
|
|
|
|
|
|
|
|
computers.sort( ( a, b ) -> {
|
|
|
|
if( a.getWorld() == b.getWorld() && a.getWorld() == world )
|
|
|
|
{
|
|
|
|
return Double.compare( a.getPosition().distanceSq( pos ), b.getPosition().distanceSq( pos ) );
|
|
|
|
}
|
|
|
|
else if( a.getWorld() == world )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if( b.getWorld() == world )
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Integer.compare( a.getInstanceID(), b.getInstanceID() );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ServerComputer computer : computers )
|
2017-12-06 15:51:02 +00:00
|
|
|
{
|
2018-12-29 12:18:05 +00:00
|
|
|
table.row(
|
2018-06-22 07:28:36 +00:00
|
|
|
linkComputer( context, computer, computer.getID() ),
|
2017-12-06 15:51:02 +00:00
|
|
|
bool( computer.isOn() ),
|
|
|
|
linkPosition( context, computer )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-29 12:18:05 +00:00
|
|
|
table.display( context.getSender() );
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|
|
|
|
else if( arguments.size() == 1 )
|
|
|
|
{
|
|
|
|
ServerComputer computer = ComputerSelector.getComputer( arguments.get( 0 ) );
|
|
|
|
|
2018-12-29 12:18:05 +00:00
|
|
|
TableBuilder table = new TableBuilder( DUMP_SINGLE_ID );
|
|
|
|
table.row( header( "Instance" ), text( Integer.toString( computer.getInstanceID() ) ) );
|
|
|
|
table.row( header( "Id" ), text( Integer.toString( computer.getID() ) ) );
|
|
|
|
table.row( header( "Label" ), text( computer.getLabel() ) );
|
|
|
|
table.row( header( "On" ), bool( computer.isOn() ) );
|
|
|
|
table.row( header( "Position" ), linkPosition( context, computer ) );
|
|
|
|
table.row( header( "Family" ), text( computer.getFamily().toString() ) );
|
2017-12-06 15:51:02 +00:00
|
|
|
|
|
|
|
for( int i = 0; i < 6; i++ )
|
|
|
|
{
|
|
|
|
IPeripheral peripheral = computer.getPeripheral( i );
|
|
|
|
if( peripheral != null )
|
|
|
|
{
|
2019-02-11 16:21:51 +00:00
|
|
|
table.row( header( "Peripheral " + IAPIEnvironment.SIDE_NAMES[i] ), text( peripheral.getType() ) );
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 12:18:05 +00:00
|
|
|
table.display( context.getSender() );
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new CommandException( context.getFullUsage() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public List<String> getCompletion( @Nonnull CommandContext context, @Nonnull List<String> arguments )
|
|
|
|
{
|
|
|
|
return arguments.size() == 1
|
|
|
|
? ComputerSelector.completeComputer( arguments.get( 0 ) )
|
2018-02-13 11:45:13 +00:00
|
|
|
: Collections.emptyList();
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2019-03-16 10:26:40 +00:00
|
|
|
root.register( new SubCommandBase( "shutdown", UserLevel.OWNER_OP )
|
2017-12-06 15:51:02 +00:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
|
|
|
{
|
2018-08-04 09:39:44 +00:00
|
|
|
withComputers( arguments, computers -> {
|
|
|
|
int shutdown = 0;
|
|
|
|
for( ServerComputer computer : computers )
|
2017-12-06 15:51:02 +00:00
|
|
|
{
|
2018-08-04 09:39:44 +00:00
|
|
|
if( computer.isOn() ) shutdown++;
|
2019-02-27 13:47:50 +00:00
|
|
|
computer.shutdown();
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|
2019-03-16 10:26:40 +00:00
|
|
|
context.getSender().sendMessage( translate( "commands.computercraft.shutdown.done", shutdown, computers.size() ) );
|
2018-08-04 09:39:44 +00:00
|
|
|
} );
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public List<String> getCompletion( @Nonnull CommandContext context, @Nonnull List<String> arguments )
|
|
|
|
{
|
|
|
|
return arguments.size() == 0
|
2018-02-13 11:45:13 +00:00
|
|
|
? Collections.emptyList()
|
|
|
|
: ComputerSelector.completeComputer( arguments.get( arguments.size() - 1 ) );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2019-03-16 10:26:40 +00:00
|
|
|
root.register( new SubCommandBase( "turn-on", UserLevel.OWNER_OP )
|
2018-02-13 11:45:13 +00:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
|
|
|
{
|
2018-08-04 09:39:44 +00:00
|
|
|
withComputers( arguments, computers -> {
|
|
|
|
int on = 0;
|
|
|
|
for( ServerComputer computer : computers )
|
2018-02-13 11:45:13 +00:00
|
|
|
{
|
2018-08-04 09:39:44 +00:00
|
|
|
if( !computer.isOn() ) on++;
|
|
|
|
computer.turnOn();
|
2018-02-13 11:45:13 +00:00
|
|
|
}
|
2019-03-16 10:26:40 +00:00
|
|
|
context.getSender().sendMessage( translate( "commands.computercraft.turn_on.done", on, computers.size() ) );
|
2018-08-04 09:39:44 +00:00
|
|
|
} );
|
2018-02-13 11:45:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public List<String> getCompletion( @Nonnull CommandContext context, @Nonnull List<String> arguments )
|
|
|
|
{
|
|
|
|
return arguments.size() == 0
|
|
|
|
? Collections.emptyList()
|
2017-12-06 15:51:02 +00:00
|
|
|
: ComputerSelector.completeComputer( arguments.get( arguments.size() - 1 ) );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2019-03-16 10:26:40 +00:00
|
|
|
root.register( new SubCommandBase( "tp", UserLevel.OP )
|
2017-12-06 15:51:02 +00:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
|
|
|
{
|
|
|
|
if( arguments.size() != 1 ) throw new CommandException( context.getFullUsage() );
|
|
|
|
|
|
|
|
ServerComputer computer = ComputerSelector.getComputer( arguments.get( 0 ) );
|
|
|
|
World world = computer.getWorld();
|
|
|
|
BlockPos pos = computer.getPosition();
|
|
|
|
|
2019-03-16 10:26:40 +00:00
|
|
|
if( world == null || pos == null ) throw new CommandException( "commands.computercraft.tp.not_there" );
|
2017-12-06 15:51:02 +00:00
|
|
|
|
|
|
|
ICommandSender sender = context.getSender();
|
2019-03-16 10:26:40 +00:00
|
|
|
if( !(sender instanceof Entity) ) throw new CommandException( "commands.computercraft.tp.not_entity" );
|
2017-12-06 15:51:02 +00:00
|
|
|
|
|
|
|
if( sender instanceof EntityPlayerMP )
|
|
|
|
{
|
|
|
|
EntityPlayerMP entity = (EntityPlayerMP) sender;
|
|
|
|
if( entity.getEntityWorld() != world )
|
|
|
|
{
|
|
|
|
context.getServer().getPlayerList().changePlayerDimension( entity, world.provider.getDimension() );
|
|
|
|
}
|
|
|
|
|
|
|
|
entity.setPositionAndUpdate( pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Entity entity = (Entity) sender;
|
|
|
|
if( entity.getEntityWorld() != world )
|
|
|
|
{
|
|
|
|
entity.changeDimension( world.provider.getDimension() );
|
|
|
|
}
|
|
|
|
|
|
|
|
entity.setLocationAndAngles(
|
|
|
|
pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5,
|
|
|
|
entity.rotationYaw, entity.rotationPitch
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public List<String> getCompletion( @Nonnull CommandContext context, @Nonnull List<String> arguments )
|
|
|
|
{
|
|
|
|
return arguments.size() == 1
|
|
|
|
? ComputerSelector.completeComputer( arguments.get( 0 ) )
|
2018-02-13 11:45:13 +00:00
|
|
|
: Collections.emptyList();
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2019-03-16 10:26:40 +00:00
|
|
|
root.register( new SubCommandBase( "view", UserLevel.OP )
|
2018-01-20 11:07:09 +00:00
|
|
|
{
|
2017-12-06 15:51:02 +00:00
|
|
|
@Override
|
2018-01-20 11:07:09 +00:00
|
|
|
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
|
|
|
{
|
|
|
|
if( arguments.size() != 1 ) throw new CommandException( context.getFullUsage() );
|
2017-12-06 15:51:02 +00:00
|
|
|
|
|
|
|
ICommandSender sender = context.getSender();
|
2018-01-20 11:07:09 +00:00
|
|
|
if( !(sender instanceof EntityPlayerMP) )
|
|
|
|
{
|
2019-03-16 10:26:40 +00:00
|
|
|
throw new CommandException( "commands.computercraft.view.not_player" );
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 11:07:09 +00:00
|
|
|
ServerComputer computer = ComputerSelector.getComputer( arguments.get( 0 ) );
|
2017-12-06 15:51:02 +00:00
|
|
|
ComputerCraft.openComputerGUI( (EntityPlayerMP) sender, computer );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
2018-01-20 11:07:09 +00:00
|
|
|
public List<String> getCompletion( @Nonnull CommandContext context, @Nonnull List<String> arguments )
|
|
|
|
{
|
2017-12-06 15:51:02 +00:00
|
|
|
return arguments.size() == 1
|
|
|
|
? ComputerSelector.completeComputer( arguments.get( 0 ) )
|
|
|
|
: Collections.emptyList();
|
|
|
|
}
|
2018-01-20 11:07:09 +00:00
|
|
|
} );
|
2017-12-06 15:51:02 +00:00
|
|
|
|
2019-03-16 10:26:40 +00:00
|
|
|
root.register( new CommandRoot( "track" ).register( new SubCommandBase( "start", UserLevel.OWNER_OP )
|
2018-02-02 13:34:27 +00:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments )
|
|
|
|
{
|
2018-04-16 20:06:16 +00:00
|
|
|
getTimingContext( context ).start();
|
2018-02-02 13:34:27 +00:00
|
|
|
|
|
|
|
String stopCommand = "/" + context.parent().getFullPath() + " stop";
|
|
|
|
context.getSender().sendMessage( list(
|
2019-03-16 10:26:40 +00:00
|
|
|
translate( "commands.computercraft.track.start.stop",
|
|
|
|
link( text( stopCommand ), stopCommand, translate( "commands.computercraft.track.stop.action" ) ) )
|
2018-02-02 13:34:27 +00:00
|
|
|
) );
|
|
|
|
}
|
2019-03-16 10:26:40 +00:00
|
|
|
} ).register( new SubCommandBase( "stop", UserLevel.OWNER_OP )
|
2018-02-02 13:34:27 +00:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
|
|
|
{
|
2018-04-16 20:06:16 +00:00
|
|
|
TrackingContext timings = getTimingContext( context );
|
2019-03-16 10:26:40 +00:00
|
|
|
if( !timings.stop() ) throw new CommandException( "commands.computercraft.track.stop.not_enabled" );
|
2018-04-17 08:22:26 +00:00
|
|
|
displayTimings( context, timings.getImmutableTimings(), TrackingField.AVERAGE_TIME );
|
2018-02-02 13:34:27 +00:00
|
|
|
}
|
2019-03-16 10:26:40 +00:00
|
|
|
} ).register( new SubCommandBase( "dump", UserLevel.OWNER_OP )
|
2018-02-02 13:34:27 +00:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
|
|
|
{
|
2018-04-16 20:06:16 +00:00
|
|
|
TrackingField field = TrackingField.AVERAGE_TIME;
|
|
|
|
if( arguments.size() >= 1 )
|
|
|
|
{
|
|
|
|
field = TrackingField.fields().get( arguments.get( 0 ) );
|
2019-03-16 10:26:40 +00:00
|
|
|
if( field == null )
|
|
|
|
{
|
|
|
|
throw new CommandException( "commands.computercraft.track.dump.no_field", arguments.get( 0 ) );
|
|
|
|
}
|
2018-04-16 20:06:16 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 08:22:26 +00:00
|
|
|
displayTimings( context, getTimingContext( context ).getImmutableTimings(), field );
|
2018-04-16 20:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public List<String> getCompletion( @Nonnull CommandContext context, @Nonnull List<String> arguments )
|
|
|
|
{
|
|
|
|
if( arguments.size() == 1 )
|
|
|
|
{
|
|
|
|
String match = arguments.get( 0 );
|
|
|
|
|
|
|
|
List<String> out = new ArrayList<>();
|
|
|
|
for( String key : TrackingField.fields().keySet() )
|
|
|
|
{
|
|
|
|
if( CommandBase.doesStringStartWith( match, key ) ) out.add( key );
|
|
|
|
}
|
|
|
|
|
|
|
|
out.sort( Comparator.naturalOrder() );
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return super.getCompletion( context, arguments );
|
|
|
|
}
|
2018-02-02 13:34:27 +00:00
|
|
|
}
|
2019-03-16 10:26:40 +00:00
|
|
|
} ) );
|
2018-04-16 20:06:16 +00:00
|
|
|
|
2019-03-16 10:26:40 +00:00
|
|
|
root.register( new SubCommandBase( "reload", UserLevel.OWNER_OP )
|
2018-02-16 09:33:40 +00:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments )
|
|
|
|
{
|
2019-01-05 20:12:02 +00:00
|
|
|
Config.reload();
|
2019-03-16 10:26:40 +00:00
|
|
|
context.getSender().sendMessage( translate( "commands.computercraft.reload.done" ) );
|
2018-02-16 09:33:40 +00:00
|
|
|
}
|
|
|
|
} );
|
2018-02-02 13:34:27 +00:00
|
|
|
|
2019-03-16 10:26:40 +00:00
|
|
|
root.register( new SubCommandBase( "queue", UserLevel.ANYONE )
|
2018-11-29 11:57:52 +00:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
|
|
|
{
|
|
|
|
if( arguments.size() < 1 ) throw new CommandException( context.getFullUsage() );
|
|
|
|
|
|
|
|
String selector = arguments.get( 0 );
|
|
|
|
Object[] rest = arguments.subList( 1, arguments.size() ).toArray();
|
|
|
|
|
|
|
|
boolean found = false;
|
|
|
|
for( ServerComputer computer : ComputerSelector.getComputers( selector ) )
|
|
|
|
{
|
|
|
|
if( computer.getFamily() != ComputerFamily.Command || !computer.isOn() ) continue;
|
|
|
|
found = true;
|
|
|
|
computer.queueEvent( "computer_command", rest );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !found )
|
|
|
|
{
|
2019-03-16 10:26:40 +00:00
|
|
|
throw new CommandException( "commands.computercraft.argument.no_matching", selector );
|
2018-11-29 11:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
2017-12-06 15:51:02 +00:00
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2018-06-22 07:28:36 +00:00
|
|
|
private static ITextComponent linkComputer( CommandContext context, ServerComputer serverComputer, int computerId )
|
2017-12-06 15:51:02 +00:00
|
|
|
{
|
2018-06-22 07:28:36 +00:00
|
|
|
ITextComponent out = new TextComponentString( "" );
|
|
|
|
|
|
|
|
// Append the computer instance
|
|
|
|
if( serverComputer == null )
|
|
|
|
{
|
|
|
|
out.appendSibling( text( "?" ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out.appendSibling( link(
|
|
|
|
text( Integer.toString( serverComputer.getInstanceID() ) ),
|
|
|
|
"/computercraft dump " + serverComputer.getInstanceID(),
|
2019-03-16 10:26:40 +00:00
|
|
|
translate( "commands.computercraft.dump.action" )
|
2018-06-22 07:28:36 +00:00
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// And ID
|
|
|
|
out.appendText( " (id " + computerId + ")" );
|
|
|
|
|
|
|
|
// And, if we're a player, some useful links
|
|
|
|
if( serverComputer != null && UserLevel.OP.canExecute( context ) && context.fromPlayer() )
|
|
|
|
{
|
|
|
|
out
|
|
|
|
.appendText( " " )
|
|
|
|
.appendSibling( link(
|
|
|
|
text( "\u261b" ),
|
|
|
|
"/computercraft tp " + serverComputer.getInstanceID(),
|
2019-03-16 10:26:40 +00:00
|
|
|
translate( "commands.computercraft.tp.action" )
|
2018-06-22 07:28:36 +00:00
|
|
|
) )
|
|
|
|
.appendText( " " )
|
|
|
|
.appendSibling( link(
|
|
|
|
text( "\u20e2" ),
|
|
|
|
"/computercraft view " + serverComputer.getInstanceID(),
|
2019-03-16 10:26:40 +00:00
|
|
|
translate( "commands.computercraft.view.action" )
|
2018-06-22 07:28:36 +00:00
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static ITextComponent linkPosition( CommandContext context, ServerComputer computer )
|
|
|
|
{
|
|
|
|
if( UserLevel.OP.canExecute( context ) )
|
|
|
|
{
|
|
|
|
return link(
|
|
|
|
position( computer.getPosition() ),
|
|
|
|
"/computercraft tp " + computer.getInstanceID(),
|
2019-03-16 10:26:40 +00:00
|
|
|
translate( "commands.computercraft.tp.action" )
|
2017-12-06 15:51:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return position( computer.getPosition() );
|
|
|
|
}
|
|
|
|
}
|
2018-02-02 13:34:27 +00:00
|
|
|
|
2018-04-16 20:06:16 +00:00
|
|
|
private static TrackingContext getTimingContext( CommandContext context )
|
2018-02-02 13:34:27 +00:00
|
|
|
{
|
2018-04-16 20:06:16 +00:00
|
|
|
Entity entity = context.getSender().getCommandSenderEntity();
|
|
|
|
if( entity instanceof EntityPlayerMP )
|
|
|
|
{
|
|
|
|
return Tracking.getContext( entity.getUniqueID() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Tracking.getContext( SYSTEM_UUID );
|
|
|
|
}
|
|
|
|
}
|
2018-02-02 13:34:27 +00:00
|
|
|
|
2018-04-16 20:06:16 +00:00
|
|
|
private static void displayTimings( CommandContext context, List<ComputerTracker> timings, TrackingField field ) throws CommandException
|
|
|
|
{
|
2019-03-16 10:26:40 +00:00
|
|
|
if( timings.isEmpty() ) throw new CommandException( "commands.computercraft.track.dump.no_timings" );
|
2018-02-02 13:34:27 +00:00
|
|
|
|
|
|
|
Map<Computer, ServerComputer> lookup = new HashMap<>();
|
|
|
|
int maxId = 0, maxInstance = 0;
|
|
|
|
for( ServerComputer server : ComputerCraft.serverComputerRegistry.getComputers() )
|
|
|
|
{
|
|
|
|
lookup.put( server.getComputer(), server );
|
|
|
|
|
|
|
|
if( server.getInstanceID() > maxInstance ) maxInstance = server.getInstanceID();
|
|
|
|
if( server.getID() > maxId ) maxId = server.getID();
|
|
|
|
}
|
|
|
|
|
2018-04-16 20:06:16 +00:00
|
|
|
timings.sort( Comparator.<ComputerTracker, Long>comparing( x -> x.get( field ) ).reversed() );
|
|
|
|
|
|
|
|
boolean defaultLayout = field == TrackingField.TASKS || field == TrackingField.TOTAL_TIME
|
|
|
|
|| field == TrackingField.AVERAGE_TIME || field == TrackingField.MAX_TIME;
|
|
|
|
|
|
|
|
|
2019-03-16 10:26:40 +00:00
|
|
|
TableBuilder table = defaultLayout ? new TableBuilder(
|
|
|
|
TRACK_ID,
|
|
|
|
translate( "commands.computercraft.track.dump.computer" ),
|
|
|
|
translate( TrackingField.TASKS.translationKey() ),
|
|
|
|
translate( TrackingField.TOTAL_TIME.translationKey() ),
|
|
|
|
translate( TrackingField.AVERAGE_TIME.translationKey() ),
|
|
|
|
translate( TrackingField.MAX_TIME.translationKey() )
|
|
|
|
) : new TableBuilder(
|
|
|
|
TRACK_ID,
|
|
|
|
translate( "commands.computercraft.track.dump.computer" ),
|
|
|
|
translate( field.translationKey() )
|
|
|
|
);
|
2018-04-16 20:06:16 +00:00
|
|
|
|
|
|
|
for( ComputerTracker entry : timings )
|
2018-02-02 13:34:27 +00:00
|
|
|
{
|
|
|
|
Computer computer = entry.getComputer();
|
|
|
|
ServerComputer serverComputer = computer == null ? null : lookup.get( computer );
|
|
|
|
|
2018-06-22 07:28:36 +00:00
|
|
|
ITextComponent computerComponent = linkComputer( context, serverComputer, entry.getComputerId() );
|
2018-02-02 13:34:27 +00:00
|
|
|
|
2018-04-16 20:06:16 +00:00
|
|
|
if( defaultLayout )
|
|
|
|
{
|
2018-12-29 12:18:05 +00:00
|
|
|
table.row(
|
2018-04-16 20:06:16 +00:00
|
|
|
computerComponent,
|
|
|
|
text( entry.getFormatted( TrackingField.TASKS ) ),
|
|
|
|
text( entry.getFormatted( TrackingField.TOTAL_TIME ) ),
|
|
|
|
text( entry.getFormatted( TrackingField.AVERAGE_TIME ) ),
|
|
|
|
text( entry.getFormatted( TrackingField.MAX_TIME ) )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-29 12:18:05 +00:00
|
|
|
table.row( computerComponent, text( entry.getFormatted( field ) ) );
|
2018-04-16 20:06:16 +00:00
|
|
|
}
|
2018-02-02 13:34:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-29 12:18:05 +00:00
|
|
|
table.display( context.getSender() );
|
2018-02-02 13:34:27 +00:00
|
|
|
}
|
2018-08-04 09:39:44 +00:00
|
|
|
|
|
|
|
private static void withComputers( List<String> selectors, Consumer<Collection<ServerComputer>> action ) throws CommandException
|
|
|
|
{
|
|
|
|
Set<ServerComputer> computers = Sets.newHashSet();
|
|
|
|
List<String> failed = new ArrayList<>();
|
|
|
|
if( selectors.isEmpty() )
|
|
|
|
{
|
|
|
|
computers.addAll( ComputerCraft.serverComputerRegistry.getComputers() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( String selector : selectors )
|
|
|
|
{
|
|
|
|
List<ServerComputer> selected = ComputerSelector.getComputers( selector );
|
|
|
|
computers.addAll( selected );
|
|
|
|
if( selected.isEmpty() ) failed.add( selector );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
action.accept( computers );
|
|
|
|
|
|
|
|
if( !failed.isEmpty() )
|
|
|
|
{
|
2019-03-16 10:26:40 +00:00
|
|
|
throw new CommandException( "commands.computercraft.argument.no_matching", String.join( ", ", failed ) );
|
2018-08-04 09:39:44 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|