1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-26 07:03:22 +00:00

Improve using /computercraft as a non-player

- Fix text table only showing the first row (fixes #40)
 - Do not emit alignment characters in monospace environments
 - Reduce padding in monospace environments

Also make the output of dump consistent with that of the profiler: we
provide tp and view shortcuts for each computer.
This commit is contained in:
SquidDev 2018-06-22 08:28:36 +01:00
parent a2083bcff1
commit 81aaead032
5 changed files with 69 additions and 53 deletions

View File

@ -20,7 +20,6 @@
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.minecraftforge.common.util.FakePlayer;
import javax.annotation.Nonnull;
import java.util.*;
@ -59,7 +58,7 @@ public void execute( @Nonnull CommandContext context, @Nonnull List<String> argu
{
if( arguments.size() == 0 )
{
TextTable table = new TextTable( DUMP_LIST_ID, "Instance", "Id", "On", "Position" );
TextTable table = new TextTable( DUMP_LIST_ID, "Computer", "On", "Position" );
List<ServerComputer> computers = new ArrayList<>( ComputerCraft.serverComputerRegistry.getComputers() );
@ -92,8 +91,7 @@ else if( b.getWorld() == world )
for( ServerComputer computer : computers )
{
table.addRow(
linkComputer( computer ),
text( Integer.toString( computer.getID() ) ),
linkComputer( context, computer, computer.getID() ),
bool( computer.isOn() ),
linkPosition( context, computer )
);
@ -406,13 +404,46 @@ public void execute( @Nonnull CommandContext context, @Nonnull List<String> argu
return root;
}
private static ITextComponent linkComputer( ServerComputer computer )
private static ITextComponent linkComputer( CommandContext context, ServerComputer serverComputer, int computerId )
{
return link(
text( Integer.toString( computer.getInstanceID() ) ),
"/computercraft dump " + computer.getInstanceID(),
"View more info about this computer"
);
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(),
"View more info about this computer"
) );
}
// 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(),
"Teleport to this computer"
) )
.appendText( " " )
.appendSibling( link(
text( "\u20e2" ),
"/computercraft view " + serverComputer.getInstanceID(),
"View this computer"
) );
}
return out;
}
private static ITextComponent linkPosition( CommandContext context, ServerComputer computer )
@ -459,7 +490,6 @@ private static void displayTimings( CommandContext context, List<ComputerTracker
}
ICommandSender sender = context.getSender();
boolean isPlayer = sender instanceof EntityPlayerMP && !(sender instanceof FakePlayer);
timings.sort( Comparator.<ComputerTracker, Long>comparing( x -> x.get( field ) ).reversed() );
@ -476,26 +506,7 @@ private static void displayTimings( CommandContext context, List<ComputerTracker
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"
) );
}
ITextComponent computerComponent = linkComputer( context, serverComputer, entry.getComputerId() );
if( defaultLayout )
{

View File

@ -46,7 +46,7 @@ public static ServerComputer getComputer( String selector ) throws CommandExcept
for( int i = 0; i < computers.size(); i++ )
{
if( i > 1 ) builder.append( ", " );
if( i > 0 ) builder.append( ", " );
builder.append( computers.get( i ).getInstanceID() );
}
builder.append( ")" );

View File

@ -2,7 +2,9 @@
import com.google.common.collect.Lists;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraftforge.common.util.FakePlayer;
import java.util.Collections;
import java.util.List;
@ -90,4 +92,9 @@ public ICommandSender getSender()
{
return sender;
}
public boolean fromPlayer()
{
return sender instanceof EntityPlayerMP && !(sender instanceof FakePlayer);
}
}

View File

@ -17,7 +17,6 @@
*/
public class TextFormatter
{
private static final int SPACE_WIDTH = 4;
private static final char PADDING_CHAR = '\u02cc';
/**
@ -54,7 +53,7 @@ public class TextFormatter
8, 4,
};
private static int getWidth( int codePoint )
public static int getWidth( int codePoint )
{
// Escape codes
if( codePoint == 167 ) return -1;
@ -73,7 +72,7 @@ private static int getWidth( int codePoint )
return 0;
}
private static int getWidth( ITextComponent component )
public static int getWidth( ITextComponent component )
{
int total = 0;
if( component instanceof TextComponentString )
@ -132,19 +131,17 @@ public static void appendFixedWidth( ITextComponent out, ICommandSender sender,
int width = getWidthFor( entry, sender );
int delta = maxWidth - width;
if( delta > 0 )
int spaceWidth = getWidthFor( ' ', sender );
int spaces = delta / spaceWidth;
int extra = delta % spaces;
// Append a fixed number of spaces
if( spaces > 0 ) out.appendSibling( new TextComponentString( StringUtils.repeat( ' ', spaces ) ) );
// Append several minor characters to pad to a full string
if( extra > 0 )
{
int spaces = delta / SPACE_WIDTH;
int extra = delta % SPACE_WIDTH;
// Append a fixed number of spaces
if( spaces > 0 ) out.appendSibling( new TextComponentString( StringUtils.repeat( ' ', spaces ) ) );
// Append several minor characters to pad to a full string
if( extra > 0 )
{
out.appendSibling( coloured( StringUtils.repeat( PADDING_CHAR, extra ), TextFormatting.GRAY ) );
}
out.appendSibling( coloured( StringUtils.repeat( PADDING_CHAR, extra ), TextFormatting.GRAY ) );
}
}
}

View File

@ -35,7 +35,7 @@ public TextTable( int id, @Nonnull ITextComponent... header )
this.columns = header.length;
}
public TextTable(int id)
public TextTable( int id )
{
this.id = id;
this.header = null;
@ -90,14 +90,15 @@ public void displayTo( ICommandSender sender )
ITextComponent[] row = rows.get( y );
for( int i = 0; i < row.length; i++ )
{
int width = getWidthFor( row[i], sender ) + 3;
int width = getWidthFor( row[i], sender );
if( width > maxWidths[i] ) maxWidths[i] = width;
}
}
// Add a small amount of extra padding. We include this here instead of the separator to allow
// for "extra" characters
for( int i = 0; i < maxWidths.length; i++ ) maxWidths[i] += 4;
// Add a small amount of extra padding. This defaults to 3 spaces for players
// and 1 for everyone else.
int padding = isPlayer( sender ) ? getWidth( ' ' ) * 3 : 1;
for( int i = 0; i < maxWidths.length; i++ ) maxWidths[i] += padding;
int totalWidth = (columns - 1) * getWidthFor( SEPARATOR, sender );
for( int x : maxWidths ) totalWidth += x;
@ -161,7 +162,7 @@ public void displayTo( ICommandSender sender )
for( int i = 0; i < out.size(); i++ )
{
if( i > 0 ) result.appendSibling( LINE );
result.appendSibling( out.get( 0 ) );
result.appendSibling( out.get( i ) );
}
sender.sendMessage( result );
}