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;
|
|
|
|
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;
|
2017-12-06 15:51:02 +00:00
|
|
|
import dan200.computercraft.shared.command.framework.*;
|
|
|
|
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;
|
2018-02-02 13:34:27 +00:00
|
|
|
import net.minecraftforge.common.util.FakePlayer;
|
2017-12-06 15:51:02 +00:00
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
2018-02-02 13:34:27 +00:00
|
|
|
import java.util.*;
|
2017-12-06 15:51:02 +00:00
|
|
|
|
|
|
|
import static dan200.computercraft.shared.command.framework.ChatHelpers.*;
|
|
|
|
|
|
|
|
public final class CommandComputerCraft extends CommandDelegate
|
|
|
|
{
|
2018-04-16 20:06:16 +00:00
|
|
|
public static final UUID SYSTEM_UUID = new UUID( 0, 0 );
|
|
|
|
|
2017-12-06 15:51:02 +00:00
|
|
|
public CommandComputerCraft()
|
|
|
|
{
|
|
|
|
super( create() );
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ISubCommand create()
|
|
|
|
{
|
|
|
|
CommandRoot root = new CommandRoot(
|
|
|
|
"computercraft", "Various commands for controlling computers.",
|
|
|
|
"The /computercraft command provides various debugging and administrator tools for controlling and " +
|
|
|
|
"interacting with computers."
|
|
|
|
);
|
|
|
|
|
|
|
|
root.register( new SubCommandBase(
|
|
|
|
"dump", "[id]", "Display the status of computers.", UserLevel.OWNER_OP,
|
2018-02-13 11:45:13 +00:00
|
|
|
"Display the status of all computers or specific information about one computer. You can specify the " +
|
|
|
|
"computer's instance id (e.g. 123), computer id (e.g #123) or label (e.g. \"@My Computer\")."
|
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 )
|
|
|
|
{
|
|
|
|
TextTable table = new TextTable( "Instance", "Id", "On", "Position" );
|
|
|
|
|
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
|
|
|
{
|
|
|
|
table.addRow(
|
|
|
|
linkComputer( computer ),
|
|
|
|
text( Integer.toString( computer.getID() ) ),
|
|
|
|
bool( computer.isOn() ),
|
|
|
|
linkPosition( context, computer )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
table.displayTo( context.getSender() );
|
|
|
|
}
|
|
|
|
else if( arguments.size() == 1 )
|
|
|
|
{
|
|
|
|
ServerComputer computer = ComputerSelector.getComputer( arguments.get( 0 ) );
|
|
|
|
|
|
|
|
TextTable table = new TextTable();
|
|
|
|
table.addRow( header( "Instance" ), text( Integer.toString( computer.getInstanceID() ) ) );
|
|
|
|
table.addRow( header( "Id" ), text( Integer.toString( computer.getID() ) ) );
|
|
|
|
table.addRow( header( "Label" ), text( computer.getLabel() ) );
|
|
|
|
table.addRow( header( "On" ), bool( computer.isOn() ) );
|
|
|
|
table.addRow( header( "Position" ), linkPosition( context, computer ) );
|
|
|
|
table.addRow( header( "Family" ), text( computer.getFamily().toString() ) );
|
|
|
|
|
|
|
|
for( int i = 0; i < 6; i++ )
|
|
|
|
{
|
|
|
|
IPeripheral peripheral = computer.getPeripheral( i );
|
|
|
|
if( peripheral != null )
|
|
|
|
{
|
2018-01-20 11:07:09 +00:00
|
|
|
table.addRow( header( "Peripheral " + Computer.s_sideNames[i] ), text( peripheral.getType() ) );
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
table.displayTo( context.getSender() );
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
root.register( new SubCommandBase(
|
|
|
|
"shutdown", "[ids...]", "Shutdown computers remotely.", UserLevel.OWNER_OP,
|
2018-02-13 11:45:13 +00:00
|
|
|
"Shutdown the listed computers or all if none are specified. You can specify the computer's instance id " +
|
|
|
|
"(e.g. 123), computer id (e.g #123) or label (e.g. \"@My Computer\")."
|
2017-12-06 15:51:02 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
|
|
|
{
|
2018-02-13 11:45:13 +00:00
|
|
|
Set<ServerComputer> computers = Sets.newHashSet();
|
2017-12-06 15:51:02 +00:00
|
|
|
if( arguments.size() > 0 )
|
|
|
|
{
|
|
|
|
for( String arg : arguments )
|
|
|
|
{
|
2018-02-13 11:45:13 +00:00
|
|
|
computers.addAll( ComputerSelector.getComputers( arg ) );
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
computers.addAll( ComputerCraft.serverComputerRegistry.getComputers() );
|
|
|
|
}
|
|
|
|
|
|
|
|
int shutdown = 0;
|
|
|
|
for( ServerComputer computer : computers )
|
|
|
|
{
|
|
|
|
if( computer.isOn() ) shutdown++;
|
|
|
|
computer.unload();
|
|
|
|
}
|
|
|
|
context.getSender().sendMessage( text( "Shutdown " + shutdown + " / " + computers.size() + " computers" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
@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 ) );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
root.register( new SubCommandBase(
|
|
|
|
"turn-on", "ids...", "Turn computers on remotely.", UserLevel.OWNER_OP,
|
|
|
|
"Turn on the listed computers. You can specify the computer's instance id (e.g. 123), computer id (e.g #123) " +
|
|
|
|
"or label (e.g. \"@My Computer\")."
|
|
|
|
)
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
|
|
|
{
|
|
|
|
Set<ServerComputer> computers = Sets.newHashSet();
|
|
|
|
if( arguments.size() > 0 )
|
|
|
|
{
|
|
|
|
for( String arg : arguments )
|
|
|
|
{
|
|
|
|
computers.addAll( ComputerSelector.getComputers( arg ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
computers.addAll( ComputerCraft.serverComputerRegistry.getComputers() );
|
|
|
|
}
|
|
|
|
|
|
|
|
int on = 0;
|
|
|
|
for( ServerComputer computer : computers )
|
|
|
|
{
|
|
|
|
if( !computer.isOn() ) on++;
|
|
|
|
computer.turnOn();
|
|
|
|
}
|
|
|
|
context.getSender().sendMessage( text( "Turned on " + on + " / " + computers.size() + " computers" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
@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 ) );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
root.register( new SubCommandBase(
|
|
|
|
"tp", "<id>", "Teleport to a specific computer.", UserLevel.OP,
|
|
|
|
"Teleport to the location of a computer. You can either specify the computer's instance " +
|
|
|
|
"id (e.g. 123) or computer id (e.g #123)."
|
|
|
|
)
|
|
|
|
{
|
|
|
|
@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();
|
|
|
|
|
|
|
|
if( world == null || pos == null ) throw new CommandException( "Cannot locate computer in world" );
|
|
|
|
|
|
|
|
ICommandSender sender = context.getSender();
|
|
|
|
if( !(sender instanceof Entity) ) throw new CommandException( "Sender is not an entity" );
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2018-01-20 11:07:09 +00:00
|
|
|
root.register( new SubCommandBase(
|
2017-12-06 15:51:02 +00:00
|
|
|
"view", "<id>", "View the terminal of a computer.", UserLevel.OP,
|
|
|
|
"Open the terminal of a computer, allowing remote control of a computer. This does not provide access to " +
|
|
|
|
"turtle's inventories. You can either specify the computer's instance id (e.g. 123) or computer id (e.g #123)."
|
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) )
|
|
|
|
{
|
|
|
|
throw new CommandException( "Cannot open terminal for non-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
|
|
|
|
2018-02-02 13:34:27 +00:00
|
|
|
CommandRoot track = new CommandRoot( "track", "Track execution times for computers.",
|
|
|
|
"Track how long computers execute for, as well as how many events they handle. This presents information in " +
|
|
|
|
"a similar way to /forge track and can be useful for diagnosing lag." );
|
|
|
|
root.register( track );
|
|
|
|
|
|
|
|
track.register( new SubCommandBase(
|
|
|
|
"start", "Start tracking all computers", UserLevel.OWNER_OP,
|
|
|
|
"Start tracking all computers' execution times and event counts. This will discard the results of previous runs."
|
|
|
|
)
|
|
|
|
{
|
|
|
|
@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(
|
|
|
|
text( "Run " ),
|
|
|
|
link( text( stopCommand ), stopCommand, "Click to stop tracking" ),
|
|
|
|
text( " to stop tracking and view the results" )
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
track.register( new SubCommandBase(
|
|
|
|
"stop", "Stop tracking all computers", UserLevel.OWNER_OP,
|
|
|
|
"Stop tracking all computers' events and execution times"
|
|
|
|
)
|
|
|
|
{
|
|
|
|
@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 );
|
|
|
|
if( !timings.stop() ) throw new CommandException( "Tracking not enabled" );
|
|
|
|
displayTimings( context, timings.getTimings(), TrackingField.AVERAGE_TIME );
|
2018-02-02 13:34:27 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
track.register( new SubCommandBase(
|
2018-04-16 20:06:16 +00:00
|
|
|
"dump", "[kind]", "Dump the latest track results", UserLevel.OWNER_OP,
|
2018-02-02 13:34:27 +00:00
|
|
|
"Dump the latest results of computer tracking."
|
|
|
|
)
|
|
|
|
{
|
|
|
|
@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 ) );
|
|
|
|
if( field == null ) throw new CommandException( "Unknown field '" + arguments.get( 0 ) + "'" );
|
|
|
|
}
|
|
|
|
|
|
|
|
displayTimings( context, getTimingContext( context ).getTimings(), field );
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
}
|
|
|
|
} );
|
2018-04-16 20:06:16 +00:00
|
|
|
|
2018-02-16 09:33:40 +00:00
|
|
|
root.register( new SubCommandBase(
|
|
|
|
"reload", "Reload the ComputerCraft config file", UserLevel.OWNER_OP,
|
|
|
|
"Reload the ComputerCraft config file"
|
|
|
|
)
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments )
|
|
|
|
{
|
|
|
|
ComputerCraft.loadConfig();
|
|
|
|
ComputerCraft.syncConfig();
|
|
|
|
context.getSender().sendMessage( new TextComponentString( "Reloaded config" ) );
|
|
|
|
}
|
|
|
|
} );
|
2018-02-02 13:34:27 +00:00
|
|
|
|
2017-12-06 15:51:02 +00:00
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ITextComponent linkComputer( ServerComputer computer )
|
|
|
|
{
|
|
|
|
return link(
|
|
|
|
text( Integer.toString( computer.getInstanceID() ) ),
|
|
|
|
"/computercraft dump " + computer.getInstanceID(),
|
|
|
|
"View more info about this computer"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ITextComponent linkPosition( CommandContext context, ServerComputer computer )
|
|
|
|
{
|
|
|
|
if( UserLevel.OP.canExecute( context ) )
|
|
|
|
{
|
|
|
|
return link(
|
|
|
|
position( computer.getPosition() ),
|
|
|
|
"/computercraft tp " + computer.getInstanceID(),
|
|
|
|
"Teleport to this computer"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
{
|
|
|
|
if( timings.isEmpty() ) throw new CommandException( "No timings available" );
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
ICommandSender sender = context.getSender();
|
|
|
|
boolean isPlayer = sender instanceof EntityPlayerMP && !(sender instanceof FakePlayer);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
TextTable table = defaultLayout
|
|
|
|
? new TextTable( "Computer", "Tasks", "Total", "Average", "Maximum" )
|
|
|
|
: new TextTable( "Computer", field.displayName() );
|
|
|
|
|
|
|
|
for( ComputerTracker entry : timings )
|
2018-02-02 13:34:27 +00:00
|
|
|
{
|
|
|
|
Computer computer = entry.getComputer();
|
|
|
|
ServerComputer serverComputer = computer == null ? null : lookup.get( computer );
|
|
|
|
|
|
|
|
ITextComponent computerComponent = new TextComponentString( "" )
|
|
|
|
.appendSibling( serverComputer == null ? text( "?" ) : linkComputer( serverComputer ) )
|
|
|
|
.appendText( " (id " + entry.getComputerId() + ")" );
|
|
|
|
|
|
|
|
if( serverComputer != null && UserLevel.OP.canExecute( context ) && isPlayer )
|
|
|
|
{
|
|
|
|
computerComponent
|
|
|
|
.appendText( " " )
|
|
|
|
.appendSibling( link(
|
|
|
|
text( "\u261b" ),
|
|
|
|
"/computercraft tp " + serverComputer.getInstanceID(),
|
|
|
|
"Teleport to this computer"
|
|
|
|
) )
|
|
|
|
.appendText( " " )
|
|
|
|
.appendSibling( link(
|
|
|
|
text( "\u20e2" ),
|
|
|
|
"/computercraft view " + serverComputer.getInstanceID(),
|
|
|
|
"View this computer"
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
2018-04-16 20:06:16 +00:00
|
|
|
if( defaultLayout )
|
|
|
|
{
|
|
|
|
table.addRow(
|
|
|
|
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
|
|
|
|
{
|
|
|
|
table.addRow( computerComponent, text( entry.getFormatted( field ) ) );
|
|
|
|
}
|
2018-02-02 13:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
table.displayTo( context.getSender() );
|
|
|
|
}
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|